Haskell: Monad transformers and global state

Viewed 382

I'm trying to learn Haskell. I'm trying to write a programm that contains a "global state": Vars. I want to change a component of the state (e.g. var1) each time I call a function. The change can be a simple function on the components (e.g. +4). Also, it prints out the component changed. Here is what I've done so far (but I'm stuck). Edit: after running the code I want to see the recent version of the global state.

import Control.Monad.State
import Control.Monad.IO.Class (liftIO)

data Vars = Vars {
 var1 :: Int,
 var2 :: Float
} deriving (Show)

sample :: StateT Vars IO a
sample = do 
        a <- change
        liftIO $ print a
        -- I want to call change again and apply more change to the state


change  :: StateT Vars IO a
change  = do
        dd <- get
         -- I don't know what to do next!

main = do 
  runStateT sample (Vars 20 3)
  evalStateT sample (Vars 20 3)
1 Answers
Related