Actually, here's a better example:

Prelude> foldl (+) 0 [1..1000000]
*** Exception: stack overflow

Prelude Data.List> foldl' (+) 0 [1..1000000]
500000500000

The explanation to what happens here is trivial to Haskell gurus but
completely baffling to newbies. It is difficult to explain to someone
why the first example doesn't work, although at a glance it should. The
virtue of laziness is that allows constructs like [1..1000000], but the
downside is that it can bite you on the backside when you least expect it.

Niko

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to