[Haskell-cafe] Re: curious about sum

Chaddaï Fouché chaddai.fouche at gmail.com
Thu Jun 18 12:58:08 EDT 2009

On Thu, Jun 18, 2009 at 6:38 PM, Alberto G. Corona<agocorona at gmail.com> wrote:
> My question is: Why the process does not grow also in the lazy case and
> instead produces a stack overflow inmediately?

This question is answered in detail on the Wiki
As you thought, it is not the evaluation of foldl that overflow the
stack, since foldl is terminally recursive it won't ever overflow the
stack. On the other hand when the thunk created by foldl is finally
evaluated, if the argument function is strict in its first argument it
will overflow the stack if the list was too long.

It is important to note that the size of the list itself or even the
thunk doesn't guarantee a stack overflow : both of those structure are
in the heap and if the argument function can produce output before
evaluating its first argument, there will be no stack overflow,
whatever the size of the thunk.

As evidenced by this expression :
ghci> take 10 . foldl (flip (:)) [] $ [1..1000000]


More information about the Haskell-Cafe mailing list