169

Firstly, Real World Haskell, which I am reading, says to never use foldl and instead use foldl'. So I trust it.

But I'm hazy on when to use foldr vs. foldl'. Though I can see the structure of how they work differently laid out in front of me, I'm too stupid to understand when "which is better." I guess it seems to me like it shouldn't really matter which is used, as they both produce the same answer (don't they?). In fact, my previous experience with this construct is from Ruby's inject and Clojure's reduce, which don't seem to have "left" and "right" versions. (Side question: which version do they use?)

Any insight that can help a smarts-challenged sort like me would be much appreciated!

MicroVirus
  • 5,324
  • 2
  • 28
  • 53
J Cooper
  • 16,891
  • 12
  • 65
  • 110

7 Answers7

187

The recursion for foldr f x ys where ys = [y1,y2,...,yk] looks like

f y1 (f y2 (... (f yk x) ...))

whereas the recursion for foldl f x ys looks like

f (... (f (f x y1) y2) ...) yk

An important difference here is that if the result of f x y can be computed using only the value of x, then foldr doesn't' need to examine the entire list. For example

foldr (&&) False (repeat False)

returns False whereas

foldl (&&) False (repeat False)

never terminates. (Note: repeat False creates an infinite list where every element is False.)

On the other hand, foldl' is tail recursive and strict. If you know that you'll have to traverse the whole list no matter what (e.g., summing the numbers in a list), then foldl' is more space- (and probably time-) efficient than foldr.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Chris Conway
  • 55,321
  • 43
  • 129
  • 155
  • 2
    In foldr it evaluates as f y1 thunk, so it returns False, however in foldl, f can't know either of it's parameter.In Haskell, no matter whether it's tail recursion or not, it both can cause thunks overflow, i.e. thunk is too big. foldl' can reduce thunk immediately along the execution. – Sawyer Sep 28 '11 at 22:51
  • 32
    To avoid confusion, note that the parentheses do not show the actual order of evaluation. Since Haskell is lazy the outermost expressions will be evaluated first. – Lii Oct 29 '13 at 16:29
  • Greate answer. I would like to add that if you want a fold which can stop part way through a list, you have to use foldr; unless I'm mistaken, left folds can't be stopped. (You hint this when you say "if you know ... you'll ... traverse the whole list"). Also, the typo "using only on the value" should be changed to "using only the value". I.e. remove the word "on". (Stackoverflow wouldn't let me submit a 2 char change!). – Lqueryvg Nov 26 '14 at 22:34
  • @Lqueryvg two ways to stop left folds: 1. code it with a right fold (see [`fodlWhile`](https://wiki.haskell.org/Foldl_as_foldr_alternative)) ; 2. convert it into a left scan (`scanl`) and stop that with `last . takeWhile p` or similar. Uh, and 3. use `mapAccumL`. :) – Will Ness Jan 03 '16 at 10:07
  • @WillNess I just noticed that `scanl` and `scanl'` are fine with infinite lists, but `foldl`/`foldl'` both enter an infinite loop. Why doesn't `scanl` have the same problem? – Desty Mar 24 '17 at 00:18
  • 1
    @Desty because it produces new part of its overall result on each step -- unlike `foldl`, which collects its overall result and produces it only after all the work is finished and there are no more steps to perform. So e.g. `foldl (flip (:)) [] [1..3] == [3,2,1]`, so `scanl (flip(:)) [] [1..] = [[],[1],[2,1],[3,2,1],...]`... IOW, `foldl f z xs = last (scanl f z xs)` and *infinite lists have no last element* (which, in the example above, would itself be an infinite list, from INF down to 1). – Will Ness Mar 24 '17 at 07:37
60

foldr looks like this:

Right-fold visualization

foldl looks like this:

Left-fold visualization

Context: Fold on the Haskell wiki

Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
  • 3
    My preferred way to distinguish between the two is like this: `foldl` stacks parentheses on the left, `foldr` stacks parentheses on the right: `(((0+1)+2)+3)` versus `(1+(2+(3+0)))` – guido Feb 28 '21 at 10:35
34

Their semantics differ so you can't just interchange foldl and foldr. The one folds the elements up from the left, the other from the right. That way, the operator gets applied in a different order. This matters for all non-associative operations, such as subtraction.

Haskell.org has an interesting article on the subject.

MicroVirus
  • 5,324
  • 2
  • 28
  • 53
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Their semantics only differ in a trivial way, that is meaningless in practice: The order of arguments of the used function. So interface-wise they still count as exchangeable. The real difference is, it seems, only the optimization/implementation. – Evi1M4chine Jan 07 '18 at 01:40
  • 4
    @Evi1M4chine None of the differences are trivial. On the contrary, they are substantial (and, yes, meaningful in practice). In fact, if I were to write this answer today it would emphasise this difference even more. – Konrad Rudolph Jan 07 '18 at 10:07
23

Shortly, foldr is better when the accumulator function is lazy on its second argument. Read more at Haskell wiki's Stack Overflow (pun intended).

stites
  • 4,903
  • 5
  • 32
  • 43
mattiast
  • 1,934
  • 12
  • 18
20

The reason foldl' is preferred to foldl for 99% of all uses is that it can run in constant space for most uses.

Take the function sum = foldl['] (+) 0. When foldl' is used, the sum is immediately calculated, so applying sum to an infinite list will just run forever, and most likely in constant space (if you’re using things like Ints, Doubles, Floats. Integers will use more than constant space if the number becomes larger than maxBound :: Int).

With foldl, a thunk is built up (like a recipe of how to get the answer, which can be evaluated later, rather than storing the answer). These thunks can take up a lot of space, and in this case, it’s much better to evaluate the expression than to store the thunk (leading to a stack overflow… and leading you to… oh never mind)

Hope that helps.

Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
Axman6
  • 909
  • 5
  • 16
  • 1
    The big exception is if the function passed to `foldl` does nothing but apply constructors to one or more of its arguments. – dfeuer Mar 08 '16 at 02:03
  • Is there a general pattern to when `foldl` is actually the best choice? (Like infinite lists when `foldr` is the wrong choice, optimization-wise.?) – Evi1M4chine Jan 07 '18 at 01:42
  • @Evi1M4chine not sure what you mean by `foldr` being the wrong choice for infinite lists. In fact, you shouldn't use `foldl` or `foldl'` for infinite lists. See the [Haskell wiki on stack overflows](https://wiki.haskell.org/Stack_overflow#Folds) – KevinOrr Feb 06 '20 at 19:51
14

By the way, Ruby's inject and Clojure's reduce are foldl (or foldl1, depending on which version you use). Usually, when there is only one form in a language, it is a left fold, including Python's reduce, Perl's List::Util::reduce, C++'s accumulate, C#'s Aggregate, Smalltalk's inject:into:, PHP's array_reduce, Mathematica's Fold, etc. Common Lisp's reduce defaults to left fold but there's an option for right fold.

newacct
  • 119,665
  • 29
  • 163
  • 224
8

As Konrad points out, their semantics are different. They don't even have the same type:

ghci> :t foldr
foldr :: (a -> b -> b) -> b -> [a] -> b
ghci> :t foldl
foldl :: (a -> b -> a) -> a -> [b] -> a
ghci> 

For example, the list append operator (++) can be implemented with foldr as

(++) = flip (foldr (:))

while

(++) = flip (foldl (:))

will give you a type error.

Community
  • 1
  • 1
Jonas
  • 19,422
  • 10
  • 54
  • 67
  • Their type is the same, just switched around, which is irrelevant. Their interface and results are the same, except for the nasty P/NP problem (read: infinite lists). ;) The optimization due to the implementation is the only difference in practice, as far as I can tell. – Evi1M4chine Jan 07 '18 at 01:45
  • 1
    @Evi1M4chine This is incorrect, look at this example: `foldl subtract 0 [1, 2, 3, 4]` evaluates to `-10`, while `foldr subtract 0 [1, 2, 3, 4]` evaluates to `-2`. `foldl` is actually `0 - 1 - 2 - 3 - 4` while `foldr` is `4 - 3 - 2 - 1 - 0`. – krapht Mar 01 '18 at 01:07
  • @krapht, `foldr (-) 0 [1, 2, 3, 4]` is `-2` and `foldl (-) 0 [1, 2, 3, 4]` is `-10`. On the other hand, `subtract` is backwards from what you might expect (`subtract 10 14` is `4`), so `foldr subtract 0 [1, 2, 3, 4]` is `-10` and `foldl subtract 0 [1, 2, 3, 4]` is `2` (positive). – pianoJames Oct 03 '18 at 17:52