[Haskell-beginners] Circular Linked Lists

Dave Bayer bayer at cpw.math.columbia.edu
Tue Feb 3 10:26:12 EST 2009


On Feb 3, 2009, at 10:15 AM, Dave Bayer wrote:
>
> The following takes forever, but it doesn't consume memory:
>
>> Prelude> :m Data.List
>> Prelude Data.List> genericIndex (zip (cycle [1..3]) (cycle [1..4]))  
>> (1000^1000)
>
> So zip is doing something smart here with cyclic lists.

No, I just wasn't saving the head. This burns memory:

> Prelude Data.List> let a = zip (cycle [1..3]) (cycle [1..4])
> Prelude Data.List> head a
> (1,1)
> Prelude Data.List> genericIndex a (1000^1000)
> <interactive>: memory allocation failed (requested 2097152 bytes)


More information about the Beginners mailing list