O'Reilly logo

Haskell Cookbook by Yogesh Sajanikar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Create a new project read-trans using the simple Stack template.
  2. Open read-trans.cabal and add the following dependencies in the build-depends subsection of the executable section:
        executable read-trans        hs-source-dirs: src        main-is: Main.hs        default-language: Haskell2010        build-depends: base >= 4.7 && < 5               , mtl
  1. Open src/Main.hs. We will add our example over here. Import the following module after the initial module declaration:
       import Control.Monad.Reader
  1. Write an example in which we keep read from an integer state, and check if our state meets certain criteria:
        example :: ReaderT Int IO ()        example = do        s <- ask        lift $ putStrLn $ "Current env state is " ++ (show s)        s_is_10 <- asks (== 10) lift $ putStrLn $ "Current state ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required