listArray and stack overflow (Re: Help: Stack-overflow and tail-recursive functions)

Koji Nakahara
Fri, 20 Jun 2003 13:39:00 +0900

I did some experiments and now suspect that the culprit is the infinite list.

When I replace rmat with
> rmat n = listArray ((1,1),(n,n)) [1..] -- no longer random
> print (m ! (300, 300)) where m = rmat 800
fails again. 

However, if I use a finite list as the second argument of listArray:
> rmat n = listArray ((1,1),(n,n)) [1..(n*n)]
, then the program runs without problems.

This definition works as well as oleg's.
>rmat n =    listArray ((1,1),(n,n)) $!! (take (n*n) $ map ct (randoms (mkStdGen 1) ::[Bool]) )
>            where   ct True = Unknown
>                    ct False = Dead 

I think:

	If the list is infinite, listArray doesn't return an array as data.
	Each time an element of the array is used, the list is evaluated.
	This lazy evaluation require some stack(and the access time cannot be constant?).
Is it correct?

Thank you.