4

我试图了解什么是 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 

那么,首先,您能否指定要考虑“您调用的函数”以及要考虑“调用您的函数”的内容?

4

1 回答 1

9

我认为这只是对英语的误解。当它说“你”时,它只是意味着“你当前关心的代码”。

如果您正在编写一个函数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"
于 2020-06-20T00:58:02.840 回答