haskell - 更改 Writer monad 中的写入数据

标签 haskell monads

给定一个 Writer monad Action ,我想通过将函数映射到 monad Action 中的写入数据来修改它。

就像是:

retell :: (w -> w') -> Writer w a -> Writer w' a

库中是否已经存在这样的功能?如果不是,如何定义?

最佳答案

retell f = Writer . second f $ runWriter 

还有一个mapWriter库提供的功能。所以你可以这样做:
retell = mapWriter . second
second函数在 Control.Arrow ,但您可以自己定义一个不太通用的版本,如下所示:
second f (a, b) = (a, f b)

关于haskell - 更改 Writer monad 中的写入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10641658/

相关文章:

haskell - 为什么Haskell Data.ByteString.Lazy.Char8没有解压缩功能?

Haskell IO Monad 和内存使用

monads - PureScript FFI 和 Aff Monad : Why does the effect never run?

haskell - Emacs 组织模式和读写 Haskell

haskell - 与函数实例化显示的奇怪模式匹配

Haskell——理解编写器类型声明

scala - 如何在 Scala 中使用 Reader 和 Writer monad?

haskell - 有没有一种方法可以在一个函数中使用多个 monad?

haskell - 根据另一个列表过滤 haskell 列表

list - 为什么 GHC 不能对一些无限列表进行推理?