
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 (:), (++), (&&), (||) ...