Skip to content

Instantly share code, notes, and snippets.

@GoldsteinE
Created March 17, 2020 20:51
Show Gist options
  • Save GoldsteinE/0f23c3088de07b0ea8a028f48059db38 to your computer and use it in GitHub Desktop.
Save GoldsteinE/0f23c3088de07b0ea8a028f48059db38 to your computer and use it in GitHub Desktop.
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