Reverse State monad is really nice and mind blowing example of Haskell language's expressiveness and lazy evaluation. But it's not that easy to understand this monad. Moreover, it's really hard to find some convincing real life example of what you can do with Reverse State monad easier than with any other tool in the language.
Reverse State monad is defined in the next way:
newtype RState s a = RState { runRState :: s -> (a,s) }
instance Monad (RState s) where
return x = RState $ (,) x
RState sf >>= f = RState $ \s ->
let (a, past) = sf future
(b, future) = runRState (f a) s
in (b, past)
It already has some examples and usages but I don't find them quite practical.
- Quora answer: well-explained and even has real life example of usage but without code and it's not clear whether it's a really good idea to use
RState
. - Mindfuck: introducing this nice concept but example is not useful. Nobody will write Fibonacci numbers this way.
- Kwang's Haskell Blog: shows how
Writer
can be emulated withRState
but come on. Not really a real life example :)
I'm also aware of tardis
package but no tutorial of this library, documentation examples are really abstract, not so many people really understand it. The closest to what I want is this tutorial but it has example of tardis
, not just RState
. As well as this book reference.
Thus I'm not looking for tardis
real life patterns, I'm interested only in RState
illustration if possible. Though I understand that there might be no samples of pure RState
usages. In that case minimal example with RStateT
transformer or tardis
is good enough.
Did someone use this monad in real life or have really nice & useful illustration with code?
Data.Traversable.mapAccumR
. – Beera