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-trans
using thesimple
Stack template. - Open
read-trans.cabal
and add the following dependencies in thebuild-depends
subsection of theexecutable
section:
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:
...