Working with the Writer monad transformer
In the previous recipe, we worked with the Reader monad transformer. In this recipe, we will work with the Writer monad transformer. Like the Reader monad transformer, the Writer monad transformer is also a restricted version of a state transformer, in which we can only write (but cannot read).
Getting ready
In this example, we will use the Writer monad transformer to keep updating a balance sheet with transactions. We will keep pushing transactions to the Writer monad, finally yielding the balance after all transactions are processed.
We will work with the mtl
library in this recipe.
How to do it...
- Create a new project
write-trans
using thesimple
Stack template.
- Open
write-trans.cabal
and add themtl
dependency in thebuild-depends
subsection of theexecutable
section:
executable write-trans hs-source-dirs: src main-is: Main.hs default-language: Haskell2010 build-depends: base >= 4.7 && < 5 ...