The reason for MonadState get and put?

Posted by CiscoIPPhone on Stack Overflow See other posts from Stack Overflow or by CiscoIPPhone
Published on 2010-04-04T15:02:53Z Indexed on 2010/04/04 15:13 UTC
Read the original article Hit count: 269

I'm reading the Monads chapter in Real World Haskell (chapter 14). A function is defined as follows:

type RandomState a = State StdGen a
getRandom :: Random a => RandomState a
getRandom =
    get >>= \gen ->
    let (val, gen')= random gen in
    put gen' >>
    return val

I don't really understand the purpose of the get and put functions here. I rewrote the function as following which seems to do the same thing and is more concise:

getRandom2 :: Random a => RandomState a 
getRandom2= State $ \ s -> random s

So my question is: What is the purpose of get and put?

© Stack Overflow or respective owner

Related posts about haskell

Related posts about monad