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

后端 未结 7 1282
無奈伤痛
無奈伤痛 2020-11-27 10:42

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. Howeve

相关标签:
7条回答
  • 2020-11-27 11:25

    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.)

    0 讨论(0)
提交回复
热议问题