Very informative. The list is in the heap but the lazy sum of foldl is in
the stack. ok.I suppose that all tail recursive functions are detected by
the strictness analysis.
2009/6/18 Chaddaï Fouché
On Thu, Jun 18, 2009 at 6:38 PM, Alberto G. Corona
wrote: My question is: Why the process does not grow aleso in the lazy case and instead produces a stack overflow inmediately?
This question is answered in detail on the Wiki http://www.haskell.org/haskellwiki/Foldr_Foldl_Foldl%27 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] [1000000,999999,999998,999997,999996,999995,999994,999993,999992,999991]
-- Jedaï