Skip to content

Commit

Permalink
Reader Monad.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabi Volpe committed Aug 23, 2016
1 parent 21c5b90 commit 4b3b913
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions category-theory/reader-monad.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Control.Monad.Instances

-- Monad instance for functions (also called Reader Monad)
--instance Monad ((->) r) where
-- return x = \_ -> x
-- h >>= f = \w -> f (h w) w

-- example using the function monad (in fact it's the Reader Monad itself)
addStuff :: Int -> Int
addStuff = do
a <- (*2)
b <- (+10)
return (a+b)

a1 = addStuff 2

-- redefined using let in
addStuffB :: Int -> Int
addStuffB x = let
a = (*2) x
b = (+10) x
in a+b

0 comments on commit 4b3b913

Please sign in to comment.