> 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.

`foldl' constructs the expression ... 3+(2+(1+0)) but does not evaluate
it. A common space leak with lazy evalution, see Paulson, ML for the
working programmer, p.47. Try this one 

> strictFoldl                   :: (Eval b) => (b -> a -> b) -> b -> [a] -> b
> strictFoldl (*)               =  f
>     where f e []              =  e
>           f e (a:x)           =  strict f (e * a) x

> result                        =  strictFoldl (+) 0 [1..100000]
> main                          =  putStr $ show (result `mod` 10)

Cheers, Ralf


Reply via email to