In this recipe, we will write our own State Monad transformer from scratch. In the state transformer, we embed another monad into a State Monad. Hence, all actions are performed in the embedded monad, whereas the state transformer is responsible for keeping state.
Writing a State Monad transformer
How to do it...
- Create new project state-monad-trans using the simple Stack template:
stack new state-monad-trans simple
- Open src/Main.hs. We will be adding our state transformer here.
- Add the following imports after the initial module declaration:
import Data.Functor
import Control.Applicative
import Control.Monad
- Define the State Monad transformer. Note how we embed middle m (monad) in the type...