Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
给定一个Writermonad 动作,我想通过在 monad 动作内的写入数据上映射一个函数来修改它。
Writer
就像是:
retell :: (w -> w') -> Writer w a -> Writer w' a
库中是否已经存在这样的功能?如果不是,如何定义?
retell f = Writer . second f $ runWriter
库还mapWriter提供了一个功能。所以你可以这样做:
mapWriter
retell = mapWriter . second
该second函数位于 中Control.Arrow,但您可以自己定义一个不太通用的版本,如下所示:
second
Control.Arrow
second f (a, b) = (a, f b)