我一直在阅读MonadWriter类的文档,并且发现writer
和tell
可以彼此书写。在我看来,如果类方法按照文档描述的方式运行,那么pass
可以用tell
和listen
来写:
pass :: m (a, w -> w) -> m a
pass m = do
((a, f), w) <- listen m
tell $ f w
return a
我的理解正确吗?
答案 0 :(得分:3)
这最终写成w <> f w
,但它应该写成f w
。
ghci
> import Control.Monad.Writer as W
> :{
pass m = do
((a, f), w) <- listen m
tell (f w)
return a
:}
> pass (writer ((0, map (+ 10)), [1])) :: Writer [Integer] Int
WriterT (Identity (0,[1,11]))
> W.pass (writer ((0, map (+ 10)), [1])) :: Writer [Integer] Int
WriterT (Identity (0,[11]))