[Haskell-beginners] Learning StateT

Tim Baumgartner baumgartner.tim at googlemail.com
Sat Nov 27 04:22:39 EST 2010


Hi Haskellers,

in order to learn Monad Transformers, I'm trying to write a simple
calculator that has an internal state of type Calc that is modified
after each line the user enters. I started like this:

main :: IO ()
handleLine :: StateT Calc IO Bool   -- return's False in order to exit

main uses runState in order to extract the action it needs from the
transformer. Up to this point, it works fine. But in order to separate
I/O and parsing, I'd prefer to have another function

processInput :: String -> State Calc (Maybe String)

that updates the internal state and returns either a String that
should be printed or Nothing in order to exit. Trying to do that, I
didn't see another possibility but to call runState again from inside
handleLine. I guess there is a better solution. Can you help?

Thanks in advance
Tim


More information about the Beginners mailing list