Confusion regarding laziness
Posted
by Abhinav Kaushik
on Stack Overflow
See other posts from Stack Overflow
or by Abhinav Kaushik
Published on 2010-05-25T11:14:39Z
Indexed on
2010/05/25
11:31 UTC
Read the original article
Hit count: 373
haskell
|functional-programming
I have a function
myLength = foldl (\ x _ -> x + 1) 0
which fails with stack overflow with input around 10^6 elements (myLength [1..1000000] fails). I believe that is due to the thunk build up since when I replace foldl with foldl', it works. So far so good.
But now I have another function to reverse a list :
myReverse = foldl (\ acc x -> x : acc) []
which uses the lazy version foldl (instead of foldl')
When I do
myLength . myReverse $ [1.1000000]
.
This time it works fine. I fail to understand why foldl works for the later case and not for former?
© Stack Overflow or respective owner