[Haskell-beginners] theStdGen unsafePerformIO
Brent Yorgey
byorgey at seas.upenn.edu
Sun Jan 9 17:16:47 CET 2011
On Sun, Jan 09, 2011 at 05:55:41PM +0200, John Smith wrote:
> Why does theStdGen require unsafePerformIO? I recompiled the Random module with
>
> theStdGen :: IO (IORef StdGen)
> theStdGen = do
> rng <- mkStdRNG 0
> newIORef rng
>
> The implementations of a few functions needed to change slightly (to
> extract the IORef from IO), but no other type signatures needed
> changing, and nothing blew up.
With your definition, theStdGen is a computation that gives you a
*new*, *different* IORef (containing yet another generator initialized
with a seed of zero) every time you call it. The original definition
with unsafePerformIO generates a single, global IORef which is
accessed by every subsequent call to getStdGen.
-Brent
More information about the Beginners
mailing list