I'm trying to wrap my head around the applicative instance for Reader by matching the type definition with some examples. One problem is I do not know how to use my Reader newtype.
My definition of Reader is
newtype R r a =
R { run :: r -> a }
The type definition for (<*>)
is
(<*>) :: Applicative f => f (a -> b) -> f a -> f b
Specialized for the Reader type this becomes:
(<*>) :: (r -> a -> b) -> (r -> a) -> (r -> b)
So when I try:
(<*>) (+) (*2) 5 -- seems equivalent to using Reader since the types match
in the REPL I get 15
. I guess this is because it computes (2*5) + 5
.
How would I do the above with my Reader newtype? And is the a
you see in
(r -> a -> b)
the same a
you get from (r -> a)
or am I misunderstanding something?