Skip to content

Instantly share code, notes, and snippets.

@chris-taylor
Created June 13, 2012 13:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-taylor/2924160 to your computer and use it in GitHub Desktop.
Save chris-taylor/2924160 to your computer and use it in GitHub Desktop.
Reader monad example
module Module1 where
import Control.Monad.Reader
data Config = Config { arg :: Int }
initialize :: IO Config
initialize = do
n <- getLine
return $ Config (read n)
function1 :: Reader Config Int
function1 = do
conf <- ask
return (arg conf + 1)
function2 :: Reader Config Int
function2 = do
conf <- ask
return (arg conf + 2)
module Module2 where
import Module1
import Control.Monad.Reader
main = do
conf <- initialize
let n1 = runReader function1 conf
let n2 = runReader function2 conf
putStrLn $ "Results: " ++ show (n1,n2)
@chris-taylor
Copy link
Author

NB I totally have not type checked any of the above!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment