[Haskell-cafe] Decision procedure for foldr/foldl/foldl'?
jerzy.karczmarczuk at unicaen.fr
Mon Nov 21 13:44:27 CET 2011
> if *f* is lazy in its second argument, then use foldr. Everything is
> lazy, you build a very small thunk since nothing is evaluated.
> In the rare cases where*f *is (also) lazy in its first argument, you
> can use foldl.
I have the impression that this is not the most useful advice possible.
1. "Nothing is evaluated"?? Look, foldr is designed to consume
incrementally the list argument, and to produce, also incrementally the
result ; it may stop in the middle if f is lazy, but if you organize
badly your program, you will pollute your memory. You might wish to
process the whole of the list as fast as possible, and then foldr may be
dangerous. You may build a veeery big thunk before its reduction.
2. This is not only the issue: "f x z" versus "f z x". foldl is
iterative (tail-recursive) and the reduction proceeds until all the
source list is consumed. foldl works better with strict functions, not lazy.
(of course, unless I am mistaken...)
In general, sorry for the cynism, but when I read:
"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" of David Fox, I compare it with a
question of a young army officer, addressed to his elders:
"Tell me how to win the war in the quickest way possible, rather than
boring me with the explanations behind all those complicated strategies".
Caen, Normandy, France
(William the Conqueror, who lived here, had a reputation of a strategist
who tried to understand his enemies. 350 years later, the French didn't
try to understand anything, they just wanted to win the battle of
Azincourt as quick as possible).
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe