-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tree.hs
32 lines (23 loc) · 870 Bytes
/
Tree.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
module Tree where
import Data.Monoid
import Data.Foldable
data Tree a = Nil | Node a (Tree a) (Tree a) deriving (Show, Eq)
instance Functor Tree where
fmap _ Nil = Nil
fmap f (Node a l r) = Node (f a) (fmap f l) (fmap f r)
instance Applicative Tree where
pure a = Node a Nil Nil
Node f fl fr <*> Node g gl gr = Node (f g) (fl <*> gl) (fr <*> gr)
_ <*> Nil = Nil
Nil <*> _ = Nil
instance Monad Tree where
return a = pure a
(Node a l r) >>= f = f a
Nil >>= _ = Nil
instance Foldable Tree where
fold Nil = mempty
fold (Node a l r) = fold l `mappend` a `mappend` fold r
foldMap f Nil = mempty
foldMap f (Node a l r) = (foldMap f l) `mappend` (f a) `mappend` (foldMap f r)
foldr f b Nil = b
foldr f b (Node a l r) = foldr f (f a (foldr f b r)) l