Hacker News new | ask | show | jobs
by jrockway 5286 days ago
You pass the data around to the functions that need it, just like you would in any programming language.

The simplest case is:

   do_work :: Config -> Result
   do_work config = ...

   read_config :: IO Config

   main = do
       config <- read_config
       print . do_work $ config
But you can also put the configuration in a Reader, and avoid the step of manually passing the config to each function that needs it. Instead of:

    my_program :: Config -> Result
    step1 :: Config -> Arg -> OneResult
    step2 :: Config -> Arg -> SecondResult
    my_program config = step2 config . step1 config
You'll write:

    my_program = runReader config $ do
        x <- step1 42
        return . step2 $ x
So let clauses and parameterized modules are not even in the running. Whenever you have a problem in Haskell, the best way to solve it is to ask yourself how you'd solve it in some other language. Then do that.

(Nobody writes software in any language where every function is responsible for reading a config file; that functionality is delegated to some common instance that is passed around as needed. So do that in Haskell, too.)

1 comments

Reading this a day later, there were a couple brainos in there. First, the second argument to step2 should be of type OneResult rather than Arg. Secondly, there is no need for return at the end of my_program, since step1 and step2 are both "in Reader":

    my_program = do
        x <- step1 42
        step2 x
or my_program = step2 =<< step1 42

(I use =<< instead of >>= so that nonadic composition reads like normal composition (.).)

Anyway, then do:

    runReader my_program config