Fergus Henderson notes:
> The query quoted below, about heap usage in a 5-line Haskell program,
> has remained unanswered for two weeks.
Ok, here's an answer. Firstly, foldl' is not in the Standard Prelude,
so I'm assuming the definition is as follows (from hugs 1.3):
foldl' :: Eval a => (a -> b -> a) -> a -> [b] -> a
foldl' f a [] = a
foldl' f a (x:xs) = strict (foldl' f) (f a x) xs
Secondly, using nhc13, there is no space leak. You should therefore
report this example as a bug in ghc. :-)
original code with overloaded numbers:
num1 10000: 2074 byte seconds (flat profile, constant 350 bytes)
num2 10000: 2606 byte seconds (irregular profile, average 370 bytes)
code specialised to Int:
num1 10000: 802 byte seconds (irregular profile, average 240 bytes)
num2 10000: 822 byte seconds (irregular profile, average 240 bytes)
> If even simple examples like this one are too hard for the experts that
> read this mailing list, how are ordinary programmers supposed to reason
> about heap usage in more complicated programs?
Perhaps the recent confluence of IFL, the Glasgow FP Workshop, and ICFP
have conspired to rob the experts of any time they might have had to answer?
Regards,
Malcolm
----original question----
On 14-Sep-1998, Martin Stein <[EMAIL PROTECTED]> wrote:
> > though I think there was written enough about this theme, I can't still
> > understand the following problem (foldl' from the Prelude.hs):
> >
> > > seq1 x = (3-x):seq1 x
> > > seq2 x = (3-x):seq2 1
> > >
> > > countOne xs = foldl' (\c x -> if x==1 then c+1 else c) 0 xs
> > >
> > > num1 n = countOne (take n (seq1 1))
> > > num2 n = countOne (take n (seq2 1))
> >
> > num1 needs lots of heap space, num2 needs very few. Why?
> > (I checked it using ghc and '+RTS -s')
----