optimizationhaskelltail-recursioncombinatorsfold

foldl is tail recursive, so how come foldr runs faster than foldl?


I wanted to test foldl vs foldr. From what I've seen you should use foldl over foldr when ever you can due to tail reccursion optimization.

This makes sense. However, after running this test I am confused:

foldr (takes 0.057s when using time command):

a::a -> [a] -> [a]
a x = ([x] ++ )

main = putStrLn(show ( sum (foldr a [] [0.. 100000])))

foldl (takes 0.089s when using time command):

b::[b] -> b -> [b]
b xs = ( ++ xs). (\y->[y])

main = putStrLn(show ( sum (foldl b [] [0.. 100000])))

It's clear that this example is trivial, but I am confused as to why foldr is beating foldl. Shouldn't this be a clear case where foldl wins?


Solution

  • Welcome to the world of lazy evaluation.

    When you think about it in terms of strict evaluation, foldl looks "good" and foldr looks "bad" because foldl is tail recursive, but foldr would have to build a tower in the stack so it can process the last item first.

    However, lazy evaluation turns the tables. Take, for example, the definition of the map function:

    map :: (a -> b) -> [a] -> [b]
    map _ []     = []
    map f (x:xs) = f x : map f xs
    

    This wouldn't be too good if Haskell used strict evaluation, since it would have to compute the tail first, then prepend the item (for all items in the list). The only way to do it efficiently would be to build the elements in reverse, it seems.

    However, thanks to Haskell's lazy evaluation, this map function is actually efficient. Lists in Haskell can be thought of as generators, and this map function generates its first item by applying f to the first item of the input list. When it needs a second item, it just does the same thing again (without using extra space).

    It turns out that map can be described in terms of foldr:

    map f xs = foldr (\x ys -> f x : ys) [] xs
    

    It's hard to tell by looking at it, but lazy evaluation kicks in because foldr can give f its first argument right away:

    foldr f z []     = z
    foldr f z (x:xs) = f x (foldr f z xs)
    

    Because the f defined by map can return the first item of the result list using solely the first parameter, the fold can operate lazily in constant space.

    Now, lazy evaluation does bite back. For instance, try running sum [1..1000000]. It yields a stack overflow. Why should it? It should just evaluate from left to right, right?

    Let's look at how Haskell evaluates it:

    foldl f z []     = z
    foldl f z (x:xs) = foldl f (f z x) xs
    
    sum = foldl (+) 0
    
    sum [1..1000000] = foldl (+) 0 [1..1000000]
                     = foldl (+) ((+) 0 1) [2..1000000]
                     = foldl (+) ((+) ((+) 0 1) 2) [3..1000000]
                     = foldl (+) ((+) ((+) ((+) 0 1) 2) 3) [4..1000000]
                       ...
                     = (+) ((+) ((+) (...) 999999) 1000000)
    

    Haskell is too lazy to perform the additions as it goes. Instead, it ends up with a tower of unevaluated thunks that have to be forced to get a number. The stack overflow occurs during this evaluation, since it has to recurse deeply to evaluate all the thunks.

    Fortunately, there is a special function in Data.List called foldl' that operates strictly. foldl' (+) 0 [1..1000000] will not stack overflow. (Note: I tried replacing foldl with foldl' in your test, but it actually made it run slower.)