[Haskell-cafe] rand* why not of type State g a

Tomasz Zielonka tomasz.zielonka at gmail.com
Sun Aug 20 03:32:19 EDT 2006


On Sun, Aug 20, 2006 at 12:32:05AM +0200, Henning Thielemann wrote:
> 
> On Sun, 13 Aug 2006, Marc Weber wrote:
> 
> > the rand* function are examples for a typical state usage,  arent' they?
> > Is there any reasoon why they are not defined 
> >   RandomGen g => State g a
> > rather than
> >   RandomGen g => (a,a) -> g -> (a,g)
> > ?
> 
> It's probably because Control.Monad.State belongs to the MTL which is
> outside Prelude. Nonetheless I find it also more convenient to use the
> State monad for random number generators:
>  http://darcs.haskell.org/htam/src/Stochastic.hs
>  http://www.haskell.org/pipermail/haskell-cafe/2005-May/009775.html

Exactly, and thanks to the right order of parameters and the result
pair, you can easily use random* functions in a State monad:

    do
        x <- State random
        y <- State (randomR (0, 100))
        ...

Best regards
Tomasz


More information about the Haskell-Cafe mailing list