Skip to content

Instantly share code, notes, and snippets.

@wangbj
Created January 22, 2016 08:28
Show Gist options
  • Save wangbj/9cd6760e827071d8d41f to your computer and use it in GitHub Desktop.
Save wangbj/9cd6760e827071d8d41f to your computer and use it in GitHub Desktop.
foldMap, fold to monoid
data Tree a = Leaf | Branch a !(Tree a) !(Tree a)
inOrder g Leaf = mempty
inOrder g (Branch a l r) = g a <> inOrder g l <> inOrder g r
instance Foldable Tree where
foldMap g = inOrder g
treeSum :: Tree Integer -> Integer
treeSum = getSum . foldMap Sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment