Skip to content

Commit

Permalink
Reader applicative, Monad instances
Browse files Browse the repository at this point in the history
  • Loading branch information
Anastasios Valtinos committed Nov 2, 2022
1 parent 165941f commit b991ffb
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion Haskell Excercises & Code/Alejandro - Book of Monads/monadic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,19 @@ instance Monad (State'' s) where

-- let (a, s) = rsa input in (f a, s)
-- m a -> (a -> m b) -> m b
-- State s a -> ( a -> State s b) -> State s b
-- State s a -> ( a -> State s b) -> State s b

newtype Reader'' r a = Reader'' { runReader'' :: r -> a}

instance Functor (Reader'' r) where
fmap f (Reader'' ra) = Reader'' $ fmap f ra

instance Applicative (Reader'' r) where
pure x = Reader'' $ \_ -> x
Reader'' fa <*> Reader'' xa = Reader'' $ \input -> let a = xa input
ab = fa input
in ab a

instance Monad (Reader'' r) where
return x = Reader'' $ \_ -> x
rra >>= f = Reader'' $ \input -> let a = runReader'' rra input in runReader'' (f a) input

0 comments on commit b991ffb

Please sign in to comment.