After reading a couple of tutorials on Haskell state monads I wanted to try them out myself. The tutorials I read claim that the Control.Monad.State provide the following defini
It doesn't exist any more. Unfortunately, this makes many Haskell resources on the web about it outdated.
To create a value, you can just use the state function:
state :: (s -> (a, s)) -> State s a
runState
, which used to be a field of State
, is now just a normal function itself, but it works in the same way as before.
State
has been rewritten in terms of the StateT
monad transformer:
type State s = StateT s Identity
StateT
itself has a constructor StateT
that functions very similarly to the old State
constructor:
newtype StateT s m a = StateT { runStateT :: s -> m (a, s) }
The only difference is that there is an extra parameter m
. This is just a slot where you can add in any other monad, which StateT
then extends with state-handling capabilities. Naturally, to regain the old functionality of State
, you just have to set m
to Identity
, which doesn't do anything.
newtype Identity a = Identity { runIdentity :: a }