[Haskell-beginners] Example needed

Yitzchak Gale gale at sefer.org
Wed Jun 15 08:29:41 CEST 2011


Zhi-Qiang Lei wrote:
> I wonder why haskell do not take foldl' as default foldl if foldl is not used in production often.

In the Haskell standard, which is not tied to any particular compiler,
foldl is defined in the simplest way. The problem of stack overflows is
left to be solved by each compiler.

And in fact, with optimization turned on, GHC does figure out the right
thing to do in many of the common cases.

That is the history and the logic; but I agree with you that it would have
made more sense for what we call foldl' to have been the default.

> Can you give me a example about how foldl can work on an infinite list?

Prelude Data.List> foldl (const last) 0 [[3..], [7,8,9]]
9
Prelude Data.List> foldl' (const last) 0 [[3..], [7,8,9]]
<hangs until you interrupt with ^C>

Regards,
Yitz



More information about the Beginners mailing list