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

wren ng thornton wren at freegeek.org
Mon Nov 21 01:23:22 CET 2011


On 11/20/11 11:58 AM, Daniel Fischer wrote:
> 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)

If your folding function is a constructor, then the result will already 
be in WHNF, therefore foldl' is doing extra work (checking for WHNF) 
that it doesn't need to.

If your folding function is (.), the foldl variant is superior to foldl' 
because it avoids making a bunch of unnecessary intermediate 
functions/closures.

Those are the only notable real-world examples I can recall.

-- 
Live well,
~wren



More information about the Haskell-Cafe mailing list