I am new at haskell, I have to write a program context-aware,so I thought I can use the Reader Monad for keeping the context read from a file, I know how to read the file puting the content in a list of tuplessomething like [([Char],[Char])], but I do not know how to implement the Reader Monad for making the environment available to all the components of my program without using imperative style, In particular I do not know how to set and use the environment, as far as I understood I should give it as parameter to all the functions that need the environment with runReader function env, but I am very confused, can somebody give me some indications or a good tutorial? thanks in advance
Share
I think it’s easiest if you look at how you would solve this problem without using Reader, then compare the translated version. Here’s a trimmed-down example from a program I’m working on where the environment is a set of callback functions to update the display. It’s slightly more complicated because it uses ReaderT instead of Reader, but everything works in basically the same way.
Now I’ll change it to use the Reader monad to pass along the environment. Since the code was already in IO, it’s necessary to use the monad transformer version,
ReaderT.At this point, the program’s main loop will essentially be:
So that’s how you can use Reader. Every function that used to take an environment parameter no longer needs to. Functions that don’t take the environment can be used directly or lifted if they’re monadic.