Created
January 22, 2016 08:28
-
-
Save wangbj/9cd6760e827071d8d41f to your computer and use it in GitHub Desktop.
foldMap, fold to monoid
This file contains 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
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