Andrew Wagner wagner.andrew at gmail.com
Mon May 19 18:25:40 EDT 2008

```Dmitri,
Excellent questions. There's one step you're missing. Most of your
questions revolve around 'foo <- bar' constructs within a monad. I
would suggest that you review the de-sugaring rules at
and see if that helps you out some. The best process would be for you
to 1.) De-sugar this function completely and 2.) look at bind (denoted
as >>=), and substitute it in.

Hope this helps!

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