[Haskell-cafe] Decision procedure for foldr/foldl/foldl'?

Daniel Fischer daniel.is.fischer at googlemail.com
Sun Nov 20 17:58:24 CET 2011

On Sunday 20 November 2011, 17:28:43, David Fox wrote:
> Does anyone have a quick way to decide which of the fold functions to
> use in a given situation?  There are times when I would like to find
> out which to use in the quickest way possible, rather than reading a
> long explanation of why each one behaves the way it does.

- foldl: In the rare cases where you need this, you'll probably know (I'm 
not aware of any real-world case where foldl is the correct choice)

Rule of thumb:

Can the result be determined/constructed (at least partially) before the 
end of the list has been reached?[*]
Then foldr.
Otherwise foldl'.

Exceptions to the rule may exist.

[*] That typically means the folded function is lazy in its second 
argument, like (:), (++), (&&), (||) ...

More information about the Haskell-Cafe mailing list