[Haskell-cafe] Generating a random list

Milos Hasan mhasan at cs.cornell.edu
Sat Mar 1 01:50:55 EST 2008


Hi,

so let's say I want to generate a list of N random floats. The elegant 
way of doing it would be to create an infinite lazy list of floats and 
take the first N, but for N = 1,000,000 or more, this overflows the 
stack. The reason is apparently that the take function is not 
tail-recursive, and so it uses O(N) stack space..

What is the right way to do this? Sure, I could write my own 
tail-recursive generator function. But this seems to be an instance of a 
more general problem - how to avoid algorithms linear in stack space 
when dealing with large lists.

Thanks a lot!
Milos



More information about the Haskell-Cafe mailing list