[Haskell-cafe] expanding do notation / StateMonad example from tutorial all about monads

Marc Weber marco-oweber at gmx.de
Sat Dec 31 14:37:17 EST 2005


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


More information about the Haskell-Cafe mailing list