From: "S. Alexander Jacobson" <[EMAIL PROTECTED]>
Subject: what is leaking?
Date: Fri, 26 Jun 1998 14:06:53 -0400 ()
> When I try to execute this:
>
> > result = foldl (+) 0 [1..100000]
> > main = putStr $show (result `mod` 10)
>
> Hugs gives: ERROR: Garbage collection fails to reclaim sufficient space
> GHC gives: Stack space overflow: current size 262152 bytes.
>
> Why would this have an error? The list should be constructed lazily as it
> is consumed by the sum. I assume I have a space leak but I can't figure
> out why.
Evaluation of `foldl (+) ...` constructs a closure with all your
additions, but *without* performing any
evaluation of these additions. So you are building a very large
closure. The application of `mod` demands result to be in NF (here: =
WHNF) so only then is your closure reduced.
Use foldl', which evaluates every addition thus constructed to WHNF
before going into the recursion.
Marko