go-ify foldr2
authorJoachim Breitner <mail@joachim-breitner.de>
Tue, 28 Jan 2014 18:09:13 +0000 (18:09 +0000)
committerJoachim Breitner <mail@joachim-breitner.de>
Tue, 28 Jan 2014 18:09:13 +0000 (18:09 +0000)
This helps with the changes in #7994, but might also generally be a good
idea (ignore the runtime):

--------------------------------------------------------------------------------
        Program           Size    Allocs   Runtime   Elapsed  TotalMem
           fft2          -0.1%     -1.5%      0.07      0.07     +0.0%
       fibheaps          +0.0%    -17.2%      0.03      0.03     +0.0%
          fluid          +0.5%     -0.7%      0.01      0.01     +0.0%
      integrate          +0.0%     -0.9%      0.16      0.16     +0.0%
        rewrite          +0.0%     -1.1%      0.02      0.02     +0.0%
--------------------------------------------------------------------------------
            Min          -0.1%    -17.2%     -1.6%     +0.0%     +0.0%
            Max          +0.5%     +0.0%   +107.7%   +106.2%    +11.3%
 Geometric Mean          +0.0%     -0.2%    +23.7%    +23.9%     +0.1%

GHC/List.lhs

index 5358f15..b7b78c7 100644 (file)
@@ -633,11 +633,12 @@ xs !! (I# n0) | isTrue# (n0 <# 0#) =  error "Prelude.(!!): negative index\n"
 
 \begin{code}
 foldr2 :: (a -> b -> c -> c) -> c -> [a] -> [b] -> c
-foldr2 _k z []    _ys    = z
-foldr2 _k z _xs   []     = z
-foldr2 k z (x:xs) (y:ys) = k x y (foldr2 k z xs ys)
-
-{-# NOINLINE [1] foldr2 #-}
+foldr2 k z = go
+  where
+       go []    _ys     = z
+       go _xs   []      = z
+       go (x:xs) (y:ys) = k x y (go xs ys)
+{-# INLINE [0] foldr2 #-}
 
 foldr2_left :: (a -> b -> c -> d) -> d -> a -> ([b] -> c) -> [b] -> d
 foldr2_left _k  z _x _r []     = z