[Haskell-cafe] 'Proper' use of the State monad
Stefan O'Rear
stefanor at cox.net
Mon Apr 30 18:17:21 EDT 2007
On Mon, Apr 30, 2007 at 11:16:47PM +0200, Denis Volk wrote:
> Hello all,
>
> I am trying to make a (turn-based) game in Haskell and need to pass
> around quite a bit of information, so using the State monad seems most
> appropriate. My question is, which is a better idea:
>
> 1) Using State GameState r and then call execState for each game event
> (i.e. user input) so I can do IO
> 2) Using StateT GameState IO () and have the entire game live in one
> big execStateT call. (I note XMonad does something similar.)
>
> There are difficulties with the first option, including keeping even
> more state about what we're doing (for instance, are we in a menu?),
> and adding stuff later would possibly require substantial rewrites.
> Other than the fact that I would have IO in places where it perhaps
> shouldn't be, the second option seems better, but I am new to all this
> and it may be that I'm missing something.
You can also give some of your actions the type Monad m => StateT
GameState m (), so that parametricity guarantees no actual IO will
occur.
Stefan
More information about the Haskell-Cafe
mailing list