Haskell 读卡器monad-读卡器与asks功能的区别?
读卡器monad有一个asks函数,它的定义正好是读卡器函数,为什么它作为一个单独的函数存在,其定义与读卡器相同?为什么不总是使用阅读器Haskell 读卡器monad-读卡器与asks功能的区别?,haskell,monads,reader-monad,Haskell,Monads,Reader Monad,读卡器monad有一个asks函数,它的定义正好是读卡器函数,为什么它作为一个单独的函数存在,其定义与读卡器相同?为什么不总是使用阅读器 class Monad m => MonadReader r m | m -> r where -- | Retrieves the monad environment. ask :: m r ask = reader id -- | Executes a computation in a modified
class Monad m => MonadReader r m | m -> r where
-- | Retrieves the monad environment.
ask :: m r
ask = reader id
-- | Executes a computation in a modified environment.
local :: (r -> r) -- ^ The function to modify the environment.
-> m a -- ^ @Reader@ to run in the modified environment.
-> m a
-- | Retrieves a function of the current environment.
reader :: (r -> a) -- ^ The selector function to apply to the environment.
-> m a
reader f = do
r <- ask
return (f r)
-- | Retrieves a function of the current environment.
asks :: MonadReader r m
=> (r -> a) -- ^ The selector function to apply to the environment.
-> m a
asks = reader
class Monad m=>Monad r m | m->r其中
--|检索monad环境。
问:m r
ask=读卡器id
--|在修改后的环境中执行计算。
本地::(r->r)--^用于修改环境的函数。
->我将--^@Reader@运行在修改过的环境中。
->我是
--|检索当前环境的函数。
读卡器::(r->a)--^要应用于环境的选择器函数。
->我是
读卡器f=do
r(r->a)--^应用于环境的选择器函数。
->我是
asks=读取器
我找到了将这种冗余引入包和包的修补程序。修补程序/提交描述为。。。不是很有启发性。但是,在这两种情况下,询问
早于读取器
,并且在这两种情况下,相同的更改引入了状态
和编写器
原语
因此,一些猜测:
StateT
->state
;writer
->writer
;ReaderT
->reader
)。这使得用户更容易记住他们想要的东西叫什么asks
已经存在,它被保留了下来,以获得少量的向后兼容性如果我们想要一个明确的答案,我们可能必须询问Ed Kmett或Twan van Laarhoven,他们显然是这些变化的始作俑者。与其他monad变形金刚的兼容性:以及自我记录的问题
reader
读作“我有一个函数,我想把它当作某个monad的实例来处理”<代码>询问读作“我想获得环境并对其应用一些东西”。它们有同样的效果,但对人类的阅读方式不同。@WillemVanOnsem我不确定这是否真的回答了这个问题。“奇怪的行为,所以它与X兼容”的明显后续是“为什么X有这种奇怪的行为?”。在这个例子中:monawarder
有两个相同的函数,因为RWST
有两个相同的函数;但是为什么RWST
有两个相同的功能呢?看看这个transformers
补丁,它看起来像是将现有的读卡器
仅用于读卡器
类型。这个reader
是在一个应用程序中引入的,它可能是在尝试添加当reader
和朋友从自己的newtype
s切换到type reader=ReaderT Identity
时丢失的“构造函数”,发生了什么?