Created
March 31, 2015 01:14
-
-
Save TikhonJelvis/0eebde0ac267e076bf28 to your computer and use it in GitHub Desktop.
An implementation of the reverse state monad with the extra newtype wrapping and unwrapping needed to actually make it work:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
newtype RState s a = RState (s -> (a, s)) | |
-- takes initial state s and outputs both a result and a new state | |
runRState :: RState s a -> s -> (a, s) | |
runRState (RState f) start = f start | |
instance Monad (RState s) where | |
return x = RState $ \ s -> (x, s) | |
RState step1 >>= step2 = RState $ \ start -> | |
let (resultA, final) = step1 intermediate | |
(resultB, intermediate) = runRState (step2 resultA) start in | |
(resultB, final) | |
get = RState $ \ s -> (s, s) | |
put s' = RState $ \ _ -> ((), s') | |
nats = do ns <- get | |
put (0 : map (+ 1) ns) | |
return ns |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Idiomatically, I would actually implement
runRState
using record syntax:but it's confusing if you're never seen the pattern before.