expanding do notation / StateMonad example from tutorial all about monads

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

expanding do notation / StateMonad example from tutorial all about monads

Marc Weber
Hi.. I'm still struggling with monads ;-)

In the tutorial "All about monads" there is the function getOne used:

getOne:: (Random a) => (a,a) -> State StdGen a
getOne bound = do g <-get
                  (x,g')= return $ randomR bounds g
                  put g'
                  return x

In the haskell "Yet another Haskell tutorial" by Hal Daume it is said
that
    do  {fp <- e; es}

is translated to:
  let ok p = do  {es} ;
      ok _ = fail "..."
  in e >>= ok

Using this rule I was able to expand the first "<-" of getOne:
  getOne2 :: (Random a) => (a,a) -> State StdGen a
  getOne2 bounds = let ok g = do  (x,g') <- return $ randomR bounds g
                                  put g'
                                  return x
                       ok _ = fail "asd"
                   in get >>= ok

but I couldn't manage to also expand the second.
My attempt is:
  getOne3 :: (Random a) => (a,a) -> State StdGen a
  getOne3 bounds = let ok g = do  let ok2 (x,g') = do { put g'; return x } --
                                      ok2 _ = fail "2"
                                  in (return $ randomR bounds g >>= ok2
                       ok _ = fail "1"
                   in get >>= ok
But I get this compile error in at the line marked by a trailing --:
 "The last statement in a 'do' construct must be an expression"

Can you help me?

Sincerly Marc
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: expanding do notation / StateMonad example from tutorial all about monads

Lemmih
On 12/31/05, Marc Weber <[hidden email]> wrote:

> Hi.. I'm still struggling with monads ;-)
>
> In the tutorial "All about monads" there is the function getOne used:
>
> getOne:: (Random a) => (a,a) -> State StdGen a
> getOne bound = do g <-get
>                   (x,g')= return $ randomR bounds g
>                   put g'
>                   return x
>
> In the haskell "Yet another Haskell tutorial" by Hal Daume it is said
> that
>     do  {fp <- e; es}
>
> is translated to:
>   let ok p = do  {es} ;
>       ok _ = fail "..."
>   in e >>= ok
>
> Using this rule I was able to expand the first "<-" of getOne:
>   getOne2 :: (Random a) => (a,a) -> State StdGen a
>   getOne2 bounds = let ok g = do  (x,g') <- return $ randomR bounds g
>                                   put g'
>                                   return x
>                        ok _ = fail "asd"
>                    in get >>= ok
>
> but I couldn't manage to also expand the second.
> My attempt is:
>   getOne3 :: (Random a) => (a,a) -> State StdGen a
>   getOne3 bounds = let ok g = do  let ok2 (x,g') = do { put g'; return x } --
>                                       ok2 _ = fail "2"
>                                   in (return $ randomR bounds g >>= ok2
>                        ok _ = fail "1"
>                    in get >>= ok
> But I get this compile error in at the line marked by a trailing --:
>  "The last statement in a 'do' construct must be an expression"
>
> Can you help me?

Omit the 'do' in 'do let ok2'.

--
Friendly,
  Lemmih
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe