[Haskell-cafe] Why doesn't laziness save the day here?

Luke Palmer lrpalmer at gmail.com
Tue Jan 5 03:03:33 EST 2010

On Mon, Jan 4, 2010 at 6:31 PM, Dale Jordan <dalej at alum.mit.edu> wrote:
> Can anyone explain why this is looping or point out a better way to
> generate an arbitrary-length random list while still being able to
> reuse the generator?  (I'd rather not use split since this generator
> doesn't support it and its of dubious soundness.)

Well there is more than one way to split.  You don't have to split the
generator -- if you act on a stream of random numbers, you can split
the stream also:

split :: [a] -> ([a],[a])
split (x:xs) = (x:bs,as)
    where (as,bs) = split xs

However too much splitting in this case causes a performance penalty,
since you start discarding large portions of the stream.  If you don't
want to split the generator, this is the only way I can think of that
is deterministic in the random seed.

If determinism is not required, as many times it is not with *random*
computations, you can use the value-supply package on hackage, which
uses a bit of unsafePerformIO magic to turn a serial stream into a
splittable stream.  But you lose composable repeatability.


More information about the Haskell-Cafe mailing list