Skip to content

Instantly share code, notes, and snippets.

@MgaMPKAy
Last active January 1, 2016 16:49
Show Gist options
  • Save MgaMPKAy/8172960 to your computer and use it in GitHub Desktop.
Save MgaMPKAy/8172960 to your computer and use it in GitHub Desktop.
Lists form a free monoid.
import Data.Monoid
h x = [x]
{--
h
A ----->[A]
\ |
\ |
\ |
f \ | f#
\ |
\ |
\|
B
For every monoid B and f :: A -> B, there is a unique homorphism f# from the monoid of list to B.
Which is the correct proof: ftofsharp or fold ?
--}
ftofsharp :: Monoid b -- monoid (implictly determined by f)
=> (a -> b) -- f
-> ([a] -> b) -- f#
ftofsharp f = f . head
fold :: Monoid b => (a -> b) -> ([a] -> b)
fold f [] = mempty
fold f (x:xs) = mappend (f x) (fold f xs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment