Created
March 2, 2015 17:13
-
-
Save MiyamonY/0de9449a5aafa84973cb 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
import qualified Data.Foldable as F | |
import Data.Monoid | |
data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show) | |
instance Functor Tree where | |
fmap f EmptyTree = EmptyTree | |
fmap f (Node x l r) = Node (f x) (fmap f l) (fmap f r) | |
instance F.Foldable Tree where | |
foldMap f EmptyTree = mempty | |
foldMap f (Node x l r) = F.foldMap f l `mappend` | |
f x `mappend` | |
F.foldMap f r | |
testTree = Node 5 | |
(Node 3 | |
(Node 1 EmptyTree EmptyTree) | |
(Node 6 EmptyTree EmptyTree)) | |
(Node 9 | |
(Node 8 EmptyTree EmptyTree) | |
(Node 10 EmptyTree EmptyTree)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment