
The Reader monad just establishes an environment, so you can use "ask"
to retrieve a value from the environment.
Let's say you have the following types representing you Make-
Environment:
data MakeInfo = MakeInfo
{ target_ :: String
, sources_ :: [String]
}
then inside your Monad you can access MakeInfo using "ask". Because
you may want to have IO available, let's use the Monad Transformer
version of the Reader Monad, to define our MakeMonad:
type MakeMonad = ReaderT MakeInfo IO
runMake :: MakeMonad () -> MakeInfo -> IO ()
runMake m makeInfo = runReaderT m makeInfo
and runMake will run it.
Then you can access source and target e.g. with Applicatives:
test = do
sources <- sources_ <$> ask
target <- target_ <$> ask
system $ "gcc -o " ++ target ++ " " ++ (foldl (++) $ map ('
':) sources)
Since using "sources_ <$> ask" and such may still be annoying, this
gist[1] uses some (questionable) TypeClass-hackery and some extension
to overcome this "problem"...
Using this solution one can simply write:
test = sh $ "gcc -o" & target & sources
which looks somewhat nicer. This example also defines runTest and a
test function (which calls the shell command "echo" to print some
lines) you can try in ghci by typing "runTest test"...
[1] http://gist.github.com/614246
On 3 Okt., 16:56, C K Kashyap
On Sun, Oct 3, 2010 at 5:22 PM, steffen
wrote: If you don't want to mention "r1" explicitly, but want to refer to "target", "sources" and such only a monadic approach (e.g. Reader Monad) might be what you want.
Thanks Steffen ... would you be able to give me an example?
-- Regards, Kashyap _______________________________________________ Haskell-Cafe mailing list Haskell-C...@haskell.orghttp://www.haskell.org/mailman/listinfo/haskell-cafe