Working with the Reader monad transformer
In the previous recipe, we implemented our own State Monad transformer. In this recipe, we will revisit the mtl library and use the Reader monad transformer. The Reader monad transformer is a restricted version of the State Monad transformer in which we are allowed only to get the state (but not modify it).
How to do it...
- Create a new project
read-transusing thesimpleStack template. - Open
read-trans.cabaland add the following dependencies in thebuild-dependssubsection of theexecutablesection:
executable read-trans
hs-source-dirs: src
main-is: Main.hs
default-language: Haskell2010
build-depends: base >= 4.7 && < 5
, mtl- Open
src/Main.hs. We will add our example over here. Import the following module after the initial module declaration:
import Control.Monad.Reader
- Write an example in which we keep read from an integer state, and check if our state meets certain criteria:
...