What is a good name for this state-like monad?

It seems that the interesting you can do with it (that you can't do with Writer) is to write an introspect function that examines the state/output and changes it.

Up vote 7 down vote favorite 2 share g+ share fb share tw.

This is something of a combination of State and Writer. I have checked the monad laws. Newtype M s a = M { runM :: s -> (s,a) } instance (Monoid s) => Monad (M s) where return = M .

Const . (mempty,) m >>= f = M $ \s -> let (s' ,x) = runM m s (s'',y) = runM (f x) (s `mappend` s') in (s' `mappend` s'', y) StateWriter seems kinda lame. Haskell monads link|improve this question edited Dec 14 '10 at 7:57 asked Dec 14 '10 at 5:12luqui15.6k3258 68% accept rate.

I would have expected return a = M . Flip (,) a or something like that. – ephemient Dec 14 '10 at 7:30 Sry, typo, const.

– luqui Dec 14 '10 at 7:58 Why doesn't >>= yield (s `mappend` s' `mappend` s'', y)? I'm just a bit confused about the monoid returned. – rampion Dec 15 '10 at 20:49 1 @rampion, It's a combination of writer and state.

It returns the part of the state that was generated from within the computation. – luqui Dec 16 '10 at 23:44 1 enlightenment – rampion Dec 16 '10 at 23:54.

It seems that the interesting you can do with it (that you can't do with Writer) is to write an introspect function that examines the state/output and changes it: introspect :: (s -> s) -> M s () introspect f = M $ \s -> (f s, ()) I can't see that you can do this for writer, I think you'd have to make do with a post-transformer instead: postW :: Writer w a -> (w -> w) -> Writer w a postW ma f = Writer $ let (w,a) = getWriter ma in (f w,a).

Isn't it what censor :: MonadWriter w m => (w -> w) -> m a -> m a (from Control.Monad.Writer. Class) is for? – Ed'ka Dec 15 '10 at 3:27 1 @Ed'ka - yes postW is the same as censor.

But both are "post-transformers", the operation on the "log" (w -> w) has to be twinned with a single writer-monad operation. With introspect you can chain it with (>>) in compound monadic expressions. – stephen tetley Dec 15 '10 at 8:41.

1 AccumState is nice, maybe StateAccum. That matches what I need it for at least. – luqui Dec 14 '10 at 23:01.

Maybe call SW (Statefull Writer), I think short names are rather intuitive and save some typing.

– rampion Dec 14 '10 at 19:30 @rampion: myFunctionWithVeryLongAndExtremelyDescriptiveName :: Integer -> SomeExtremelyComplicatedDatatypeWhereThisIsTheLongestNameIcanThinkOf -> LongAndDescriptiveMonadT LongerAndMoreDescriptiveName a be This is why I like short names. - It saves some typing. And for the ones who don't understand the name: RTMF :) – FUZxxl Dec 15 '10 at 3:57 4 In Haskell culture, RTFP.

– luqui Dec 16 '10 at 12:54 1 Read The F*cking Paper (or Read The Functional Pearl :-) – luqui Dec 17 '10 at 9:13 1 @luqui: ROFL, you got it exactly. This is what I feel. (For instance when I read some doc like "Arrows according to by ", whithout weblink and just marginal information to the rest.

– FUZxxl Dec 18 '10 at 11:17.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions