MonadWriter类中的冗余

时间:2018-07-06 02:27:03

标签: haskell monads monad-transformers

我一直在阅读MonadWriter类的文档,并且发现writertell可以彼此书写。在我看来,如果类方法按照文档描述的方式运行,那么pass可以用telllisten来写:

pass :: m (a, w -> w) -> m a
pass m = do
    ((a, f), w) <- listen m
    tell $ f w
    return a

我的理解正确吗?

1 个答案:

答案 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]))