[Haskell-cafe] Newbie: State monad example questions

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
http://en.wikibooks.org/wiki/Haskell/Syntactic_sugar#Do_and_proc_notation
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:
> http://www.haskell.org/all_about_monads/html/statemonad.html
>
> 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)
>
> and for State monad:
>
> 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!
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>


More information about the Haskell-Cafe mailing list