Skip to content

Instantly share code, notes, and snippets.

@Shuumatsu
Created June 5, 2019 03:59
Show Gist options
  • Save Shuumatsu/75544f61ce5e91f23f62e3ecb4fb8222 to your computer and use it in GitHub Desktop.
Save Shuumatsu/75544f61ce5e91f23f62e3ecb4fb8222 to your computer and use it in GitHub Desktop.
State monad as composition of (a ->) & (, s)
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