Réf. : Re: [Haskell-cafe] GHCi and State

Tillmann Rendel rendel at Mathematik.Uni-Marburg.de
Fri Jun 25 13:23:42 EDT 2010


Hi Corentin,

corentin.dupont at ext.mpsa.com schrieb:
> for GHCi, i will try an IORef. Too bad i allready coded it using "StateT
> GameState IO ()" extensively through the code ;)

That shouldn't be a problem, you could switch back and forth in 
submitRule, approximately like this:

   startGame :: IO (Rule -> IO ())
   startGame = do
     gameState <- newIORef initialState
     return (\rule -> wrapStateT (submitRule rule) gameState)

   wrapStateT ::  StateT s IO a -> IORef s -> IO a
   wrapStateT action ref = do
     state <- readIORef ref
     (answer, state) <- runStateT action state
     writeIORef ref state
     return answer

Now you can start a game with

   mySubmitRule <- startGame

and use mySubmitRule at the ghci prompt afterwards.

   Tillmann


More information about the Haskell-Cafe mailing list