I am trying to understand State monad example15 at:<br><a href="http://www.haskell.org/all_about_monads/html/statemonad.html" target="_blank">http://www.haskell.org/all_about_monads/html/statemonad.html</a><br><br>Example 15 uses getAny that I don't understand at all how it works:<br>
<br>getAny :: (Random a) => State StdGen a<br>getAny = do g <- get<br> (x,g') <- return $ random g<br> put g'<br> return x<br><br><br>Questions:<br>1) random has type:<br>
random :: (Random a, RandomGen g) => g -> (a, g)<br><br>and for State monad:<br><br>return a = State (\s -> (a, s))<br><br>then:<br>return (random g) = State (\s -> ((a,g), s))<br><br>Is it correct?<br><br>2) What x and g' will match to in:<br>
do ...<br> (x,g') <- return $ random g<br><br>which, as I understand equals to: <br> do ...<br> (x,g') <- State (\s -> ((a,g), s))<br><br>What x and g' will match to in the last expression?<br>
<br>3) In general, in do expression (pseudo):<br> do { x <- State (\s -> (a, s)); ...}<br><br>What x will refer to? Will x stand for a whole lambda function: \s -> (a, s) ?<br><br>4) How 'g <- get' works in this function (getAny) ?<br>
5) Why we need 'put g'?<br><br>Thanks!<br> <br>