Created
June 5, 2019 03:59
-
-
Save Shuumatsu/75544f61ce5e91f23f62e3ecb4fb8222 to your computer and use it in GitHub Desktop.
State monad as composition of (a ->) & (, s)
This file contains hidden or 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 State s a = State { runState :: s -> (a, s) } | |
instance Functor (State s) where | |
-- fmap f st = State | |
-- $ \s -> let (a, s') = runState st s | |
-- in (f a, s') | |
-- fmap f st = State $ fmap (fmap f) (runState st) | |
fmap f st = State $ (.) (\(a, s) -> (f a, s)) (runState st) | |
fmap f st = State $ (\g x -> (\(a, s) -> (f a, s)) $ g x) (runState st) | |
fmap f st = State $ (\x -> (\(a, s) -> (f a, s)) $ (runState st) x) | |
fmap f st = State | |
$ (\x -> let (a, s) = (runState st) x | |
in (f a, s)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment