Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the written data in a Writer monad

Tags:

haskell

monads

Given a Writer monad action, I want to modify it by mapping a function over the written data inside the monad action.

Something like:

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

Does such a function already exists in the libraries? If not, how can one be defined?

like image 348
Romildo Avatar asked May 17 '12 18:05

Romildo


1 Answers

retell f = Writer . second f $ runWriter 

There is also a mapWriter function provided by the libraries. So you could do this:

retell = mapWriter . second

The second function is in Control.Arrow, but you can define a less general version of it yourself like this:

second f (a, b) = (a, f b)
like image 197
Apocalisp Avatar answered Sep 22 '22 22:09

Apocalisp