I would like to learn, how to use the Reader Monad. Unfortunately only a small amount of example code is available
I would like to create a Reader, where the environment and the retrived values are Int. I defined the type like this:
type IntRead = Reader Int Int
The code I tried is this:
addStuff :: Reader Int Int
addStuff = do
a <- (*2)
b <- (+10)
return (a+b)
I get an error, because ReaderT is expected. How to create a function like addStuff using the Reader Monad? Where should I provide the environment to this function?
You can convert functions to readers and back with these two isomorphisms:
E.g.
and then you can test your code with
runReader addStuff 5.This is OK for learning purposes. For more serious code, you shouldn't use the isomorphisms that much, but instead rely on
askorasks. E.g.or, better
or, even better, using applicative style:
The whole point of the reader abstraction is not to think about the underlying function. You can just pretend to have access to a read-only variable, which is accessible through the
askprimitive.Usually, only at the very last step you use
runReaderto actually use your monadic reader action.