Hi,

Rex Page  ([EMAIL PROTECTED])  had made the following
observation on Haskell  (ghc, Hugs):

> length[1 .. n] seems to run in constant space (that is, space
> independent of n), as expected.
> However, length[1 ..] runs out of space.
> This doesn't seem reasonable to me.



If it was     genericLength [1 ..]  

and if, say, it had to be printed out, then its digits would overflow 
the memory naturally.
For Haskell should advance through the list and increment the current
length  k.  Haskell evaluates lazily, but it cannot start printing the 
initial part of  k  because even the first digit is not defined until 
the end of the list.  So this  k  would run out of space.

As to  length :: [a] -> Int,

I think, Haskell could do all bad things with small integers when they
overflow.
But maybe, Haskell keeps on "incrementing" k wrongly,  k  keeping
between  -5*10^9 and +5*10^9.  
Further, if the  Integer  values in  [1..]  are represented as the 
regular lists of digits, then it would *not* run out of space, because 
only the current head node has to be allocated each time, and the
unnecessary (for the result) nodes go to the garbage, and the garbage
has to be "collected" here infinitely many times.
But if it allocates *all* the digits of the current number  
i :: Integer  (this depend on the impelmentation technique)  from 
[1..],  then of course, this would break the memory.

This is my theory.

And what the Haskell experts say ?



Sergei Mechveliani.      [EMAIL PROTECTED]



Reply via email to