【发布时间】:2017-08-16 09:04:05
【问题描述】:
我很难理解 Reader Monad。从我阅读的所有地方我的理解是,它允许将变量(读取模式)共享给不同的函数。下面是两个函数计算和相同的实现(抱歉函数名称不好),一个有 Reader,一个没有它。我没有得到使用 Reader 的好处。
module Moreunderstanding where
import Control.Monad.Reader
computation :: Reader Int Int
computation = do
a <- ask
b <- asks square
return (a + b)
square :: Int -> Int
square x = x ^ 2
samething:: Int -> Int
samething = do
a <- square
b <- id
return (a + b)
我不确定我在这里缺少什么。
【问题讨论】:
-
在第二个例子中,你隐式使用了 Reader monad for ((->) r),
Reader只是一个包装器。