According to the introduction about ReaderT
,我只能找到:Haskell - 如何构建一个将读取一些设置文件的程序?
ReaderT Env IO String
这也意味着
...is a computation which involves reading values from some environment
of type Env (the semantics of Reader, the base monad) and performing some
IO in order to give a value of type String.
所以执行的顺序将成为
1. (Already has) some environment values -- ReaderT Env
2. Do `IO` accroding to these pre-existing values -- IO a
3. Use the `IO a` values to do more computations -- IO b...z
这将要求我们的节目有一定的预先存在的值为环境,但我认为大部分程序需要加载这些环境价值观。如数据库URL,调试切换器或其他任何东西。
因此,我们有一个相反的顺序执行的,并且其是非法根据单子堆栈:
1. Do some `IO` to load environment settings -- IO a first !!
2. Do more `IO` according to these settings -- fill the env and do IO
的单子堆栈将成为:
IOT Env ReaderT Env
这是非法,因为monad不能成为monad堆栈中的monad基址。那么,有没有一种正确的方式来初始化我的程序与外部设置文件?
PS1。我注意到xmonad
编译它的设置作为程序的一部分。我仍然不确定这是否是“加载”设置的唯一方法...
看看这个类似的问题:http://stackoverflow.com/questions/11226338/missing-something-with-reader-monad-passing-the-damn-无所不在 – 2012-07-23 08:36:06
读取配置文件与'读取'命令行参数没有区别。你不需要ReaderT,只需要'config < - fmap parseConfig(readFile“config”)',这与罗马人所说的'number < - fmap(read.head)getArgs'没有区别。 – applicative 2012-07-23 14:47:34