我试图理解什么是Haskell Reader monad,但我在书中的这一部分很挣扎:
类型参数
r
的"只读"性质意味着您可以交换不同类型或值的r
来交换您调用的函数,但不能交换调用您的函数。演示这一点的最佳方法是使用withReaderT
函数,它允许我们使用提供不同的参数来启动一个新的Reader
上下文:withReaderT :: (r' -> r) -- ^ The function to modify the environment. -> ReaderT r m a -- ^ Computation to run in the modified environment. -> ReaderT r' m a
那么,首先,您能否指定要考虑什么"您调用的函数"以及要考虑什么"调用您的函数"?
我认为这只是英语的误解。当它说"你"时,它只是意味着"你当前关注的代码"。
如果您正在编写函数myfunction
:
myfunction x y = sqrt (x*x + y*y)
main = print $ myfunction 3 4
如果我们说你是myfunction
,那么sqrt
是你调用的函数,main
是调用你的函数。
本书试图说明的是,你的代码可以在你想要的任何环境中调用函数,但这些函数不能改变你的代码的环境。反过来,调用代码的代码可以指定它希望您看到的任何环境,但您无法更改该代码的环境。
下面是一个注释示例:
import Control.Monad.IO.Class
import Control.Monad.Trans.Reader
import Control.Monad.Trans
showValue :: String -> ReaderT String IO ()
showValue str = do
s <- ask
lift . putStrLn $ str ++ ": " ++ s
-- This is me (i.e. my code).
-- I show the environment twice, and it's guaranteed to be the same both times
myFunction :: ReaderT String IO ()
myFunction = do
showValue "myFunction sees"
withReaderT (const "Something Completely Different") functionThatICall
showValue "myFunction still sees"
-- This is a function that I call.
-- I decide what value it sees, but it can't decide what I see.
functionThatICall :: ReaderT String IO ()
functionThatICall = showValue "functionThatICall sees"
-- This is a function that calls me. It decides what value I see,
-- but I can't change what it sees.
functionThatCallsMe :: ReaderT String IO ()
functionThatCallsMe = do
showValue "functionThatCallsMe sees"
myFunction
showValue "functionThatCallsMe still sees"
main = runReaderT functionThatCallsMe "Hello World"