这是我试图弄清楚的状态单子代码
data State a = State (Int -> (a, Int))
instance Monad State where
return x = State (\c -> (x, c))
State m >>= f = State (\c ->
case m c of { (a, acount) ->
case f a of State b -> b acount})
getState = State (\c -> (c, c))
putState count = State (\_ -> ((), count))
instance Show State where -- doesn't work
show (State a) = show a -- doesn't work
我正在尝试将 State 作为 Show 的实例,以便我可以看到ghci 提示符的动作getState
和动作。putState count
任何指向 State Monad 材料的教程或链接也会很好。