[Haskell-cafe] State Monad

Georg Martius mai99dgf at studserv.uni-leipzig.de
Wed Mar 3 14:44:58 EST 2004


Hi folks,

I have a question to the State Monad or Monads in general - I'am not sure. 
Lets assume I have functions to transform strings. I can use the State 
Monad as follows:

strTrans :: String -> String
strTrans s = s ++ s

f :: State String ()
f = do put "hallo"	
	 modify strTrans
	 modify strTrans'
	 ...

Up to this point everything is clear.
Now I have also functions to map from (a, String) -> (a,String).
I could write:

modifyT :: ((a, String) -> (a, String)) -> a ->  State String a
modifyT trans a = do str <- get
	 	     let (a', str') = trans (a, str)
		     put str'
	 	     return a'

f :: State String ()
f = do put "hallo"
	 modify strTrans
	 i <- modifyT strIntTrans 4    -- strIntTrans :: (Int, String) -> (Int, 
String)
	 i' <- modifyT strIntTrans i	
	 ...

But this is obviously awkward. How can I stick two Monads in each other? I 
could't figure out how to use StateT. Any help would be appreciated.

Thanks!
  Georg




More information about the Haskell-Cafe mailing list