6

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?

2
  • Can you do this by chaining the pass method in the MonadWriter class? If not I'd make my own subclass of Writer - Rewriter that provides a retell operation. Commented May 18, 2012 at 16:53
  • @stephentetley - correcting myself, as you want to type change the w of the Writer monad you can't do this with pass. I would go with a Rewriter subclass that extends Writer with retell. Commented May 19, 2012 at 6:56

1 Answer 1

11
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) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.