Partially ordered collections (revisited)
Tom Pledger
Tom.Pledger at peace.com
Fri Nov 21 08:48:28 EST 2003
Graham Klyne writes:
:
| Ah, yes. I've grown a little wary of foldl, but I see it's
| appropriate in this case. As it's part of the standard prelude, I
| personally have no qualms about depending upon it ... or should I?
(Hope you don't mind my returning this to haskell-cafe.)
Most of foldl's bad reputation comes from stack overflows, because it
builds deeply nested suspensions such as
foldl f z [1..99]
--> f (... (f (f z 1) 2) ...) 99
*regardless* of the strictness of f.
Contrast that with foldr
foldr f z [1..99]
--> f 1 (foldr f z [2..99])
which enters f straight away.
Your particular f, i.e. add, is strict in both its parameters, so I
don't think the choice between foldl and foldr will affect whether you
get stack overflows. However, if you do get stack overflows, try a
strict folding function, such as a copy of Hugs's Prelude.foldl' (not
exported).
- Tom
More information about the Haskell-Cafe
mailing list