From 4b3b913a9bd465699ba3182f43a38c05f0d18c7b Mon Sep 17 00:00:00 2001 From: Gabi Volpe Date: Tue, 23 Aug 2016 14:36:42 +0100 Subject: [PATCH] Reader Monad. --- category-theory/reader-monad.hs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 category-theory/reader-monad.hs diff --git a/category-theory/reader-monad.hs b/category-theory/reader-monad.hs new file mode 100644 index 0000000..154e5e6 --- /dev/null +++ b/category-theory/reader-monad.hs @@ -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