Reputation: 1047
I'm learning State Monad and cannot understand one example in Wiki (http://en.wikibooks.org/wiki/Haskell/Understanding_monads/State)
rollDie :: GeneratorState Int
rollDie = do generator <- get
let (value, newGenerator) = randomR (1,6) generator
put newGenerator
return value
the put
has definition
put newState = State $ \_ -> ((), newState)
It seems put
just create a new State
, what the real usage of this line? If want to use the value maybe should use <-
to extract, and if want to use the state
again should use get
. It makes no difference if delete this line (or am i missing anything? ), So, what does this line really mean ?
Upvotes: 2
Views: 161
Reputation: 2376
Delete that put
and get some random values. I predict you will always get the same "random value". That is how the random Generator in haskell works: It (i.e. randomR
) is a pure function - given the same generator, it will always return the same result. You have to feed the newGenerator to the next call. This is done via put
.
Upvotes: 7