Vars
。我想在每次调用函数时更改状态中的某个组件(例如var1
)。更改可以是对组件的简单函数(例如 +4)。此外,它还会打印出更改了的组件。到目前为止,这是我的工作成果(但我被卡住了)。编辑:运行代码后,我想看到全局状态的最新版本。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)