Created
March 17, 2020 20:51
-
-
Save GoldsteinE/0f23c3088de07b0ea8a028f48059db38 to your computer and use it in GitHub Desktop.
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 Writer w a = Writer { runWriter :: (a, w) } | |
instance Functor (Writer w) where | |
fmap f x = let (Writer (a, w)) = x in Writer (f a, w) | |
instance (Monoid w) => Applicative (Writer w) where | |
pure x = Writer (x, mempty) | |
Writer (f, w2) <*> Writer (x, w1) = Writer (f x, w1 `mappend` w2) | |
instance (Monoid w) => Monad (Writer w) where | |
return = pure | |
Writer (a, w) >>= f = let (Writer (a', w')) = f a in Writer (a', w `mappend` w') | |
tell :: w -> Writer w () | |
tell w = Writer ((), w) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment