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
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
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
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
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
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
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
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
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,
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
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
11 matches
Mail list logo