Hi Alex.
If you use a different (but still lazy) definition of foldl:
myFoldl f s [] = s
myFoldl f s (x:xs) = f s (foldl f x xs)
Unless I'm more asleep than I realise, this is just foldr, isn't it?
(To wit, "fold to the right", rather than "fold to the left".)
In fact, foldr is the
From: "S. Alexander Jacobson" [EMAIL PROTECTED]
Subject: Re: what is leaking?
Date: Fri, 26 Jun 1998 16:38:12 -0400 ()
Wow! If Ralf is right, then foldl in a lazy language always generates a
space leak and should be avoided in favor of strictFoldl.
However, I still am not
As an aside, I think strictFoldl is not automatically paralellizable, but
myFoldl may be. [...]
If dreams of automatic parallelisation of lazy functional programs were
pennies, I'd have $2.35.
Alastair
On Fri, 26 Jun 1998, S. Alexander Jacobson wrote:
| myFoldl f s [] = s
| myFoldl f s (x:xs) = f s (foldl f x xs)
| total = 3 + (myFoldl (+) 3 [4..10])
| total = 3 + 4 + (myFoldl (+) 4 [5..10])
The problem is parenthesis; the last expression should have been:
total = 3 + (4
When I try to execute this:
result = foldl (+) 0 [1..10]
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
Wow! If Ralf is right, then foldl in a lazy language always generates a
space leak and should be avoided in favor of strictFoldl.
However, I still am not clear why explicity strictness is required for
foldl to behave properly. In fact it seems like the problem is
insufficient laziness.
If