[Haskell-beginners] Random Numbers with the State Monad
Thomas Jakway
tjakway at nyu.edu
Sat Feb 13 15:37:50 UTC 2016
Thanks! That makes sense
On 2/12/16 8:26 AM, Marcin Mrotek wrote:
> Hello,
>
> You have this type error:
>
> Couldn't match expected type ‘StateT
> StdGen
> Data.Functor.Identity.Identity Integer’
> with actual type ‘g1 -> (Integer, g1)’
>
> It's because you're trying to use a `(s -> (a,s))` function
> "unwrapped" where GHC expects it "wrapped" in a StateT.
> The type of `state` from Control.Monad.State (mtl package) is:
>
> state :: (s -> (a, s)) -> m a
>
> so it could solve your mismatch, turning `g1 -> (Integer, g1)` into
> `StateT StdGen Identity Integer`, like:
>
> np <- state (randomR (1, maxRandPlayers))
>
> Alternatively, if you don't want to use mtl, you can use `StateT`s
> constructor directly. It's type is:
>
> StateT :: (s -> m (a, s)) -> StateT s m a
>
> so you'd have to compose `randomR` with `return` (or `pure`) first:
>
> np <- StateT (return . randomR (1, maxRandPlayers))
>
> Best regards,
> Marcin Mrotek
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
More information about the Beginners
mailing list