Re: [Haskell-cafe] Generating a random list

2008-03-01 Thread Milos Hasan
Hi, thanks for the reply.. 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

Re: [Haskell-cafe] Generating a random list

2008-03-01 Thread Milos Hasan
Bryan O'Sullivan wrote: Milos Hasan wrote: 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

Re: [Haskell-cafe] Generating a random list

2008-03-01 Thread Milos Hasan
So, I did one more experiment, and the following overflows too: import System.Random import Data.List randFloats :: [Float] randFloats = randoms (mkStdGen 0) main = print $ sum $ sort $ take 100 randFloats Could it be that Data.List.sort is the culprit that uses O(n) stack space here? If

Re: [Haskell-cafe] Generating a random list

2008-03-01 Thread Kalman Noel
Milos Hasan wrote: Here's a minimal summing example that illustrates the difference. The following works fine, since the elements are generated lazily and summed on the fly, as expected: randFloats :: [Float] randFloats = randoms (mkStdGen 0) main = do let xs = take 100

Re: [Haskell-cafe] Generating a random list

2008-03-01 Thread Luke Palmer
On Sat, Mar 1, 2008 at 8:18 AM, Milos Hasan [EMAIL PROTECTED] wrote: Here's a minimal summing example that illustrates the difference. The following works fine, since the elements are generated lazily and summed on the fly, as expected: randFloats :: [Float] randFloats = randoms

Re: [Haskell-cafe] Generating a random list

2008-03-01 Thread Albert Y. C. Lai
The following is in ghci 6.8.2 with default options (e.g., default heap and stack). G denotes the ghci prompt. At some points ghci will use 500MB of memory. Be sure you have enough physical memory. G :m + Data.List System.Random G let f n = take n randoms (mkStdGen 0)) :: [Float] I define f

Re: [Haskell-cafe] Generating a random list

2008-03-01 Thread Milos Hasan
It is definitely the strictness analyzer biting you here. In ghci, the behavior of these two programs is identical (stack overflow). As kalman said, if you replate sum with foldl' (+) 0 in each of these programs, the behavior is still identical (correct). OK, I could replicate that

Re: [Haskell-cafe] Generating a random list

2008-03-01 Thread Chaddaï Fouché
2008/3/1, Milos Hasan [EMAIL PROTECTED]: OK, thanks, this is an important point. So maybe I should have done this? main = print $ foldl1' (+) $! take 100 randFloats My intuition tells me that the $! (and `seq`) just reduces one level (to WHNF?). If so, is there a way to force complete

[Haskell-cafe] Generating a random list

2008-02-29 Thread Milos Hasan
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,

Re: [Haskell-cafe] Generating a random list

2008-02-29 Thread Luke Palmer
On Sat, Mar 1, 2008 at 6:50 AM, Milos Hasan [EMAIL PROTECTED] wrote: 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

Re: [Haskell-cafe] Generating a random list

2008-02-29 Thread Bryan O'Sullivan
Milos Hasan wrote: 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