给定Writermonad动作,我想通过将函数映射到monad动作内的写入数据来修改它.
就像是:
retell :: (w -> w') -> Writer w a -> Writer w' a
Run Code Online (Sandbox Code Playgroud)
这样的函数在库中是否已存在?如果没有,怎么定义?
Apo*_*isp 11
retell f = Writer . second f $ runWriter
Run Code Online (Sandbox Code Playgroud)
mapWriter图书馆还提供一项功能.所以你可以这样做:
retell = mapWriter . second
Run Code Online (Sandbox Code Playgroud)
该second函数在Control.Arrow,但您可以自己定义一个不太通用的版本,如下所示:
second f (a, b) = (a, f b)
Run Code Online (Sandbox Code Playgroud)