[Haskell-cafe] Shouldnt this be lazy too?

Vimal j.vimal at gmail.com
Mon Sep 24 13:01:35 EDT 2007


>From the wiki:
> If you write it, you force Haskell to create all list nodes. ...

Alright.
Now, lets look at the definition again:

> length [] = 0
> length (x:xs) = 1 + length xs

We see that the value of *x* isnt needed at all. So, why does GHC
allocate so much memory creating all those *x*s? because, if we have:

length [1..] = 1 + length [2...] = 1 + 1 + length [3...]

This should go on, into an infinite loop. GHC doesnt need to create
all nodes! The wiki also claims that *x* isnt evaluated. So, why
allocate space for it when its not evaluated?


More information about the Haskell-Cafe mailing list