-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter14.hs
35 lines (24 loc) · 850 Bytes
/
chapter14.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
33
34
35
import Data.Monoid
import Data.Foldable
-- instance (Monoid a, Monoid b) => Monoid (a,b) where
-- -- mempty :: (a,b)
-- mempty = (mempty, mempty)
-- (mappend) (x,y) (x',y') = (x,y) `mappend` (x',y')
-- instance (Monoid a, Monoid b) => Monoid (a->b) where
-- -- mempty :: a -> b
-- mempty = const mempty
-- -- mappend :: (a -> b) -> (a -> b) -> (a -> b)
-- f1 `mappend` f2 = \x -> f1 x `mappend` f2 x
instance Foldable Maybe where
fold Nothing = mempty
fold (Just x) = x
foldMap _ Nothing = mempty
foldMap f (Just x) = f x
foldr _ _ Nothing = mempty
foldr f b (Just x) = f x b
foldl _ _ Nothing = mempty
foldl f b (Just y) = f b y
instance Traversable Maybe where
-- traverse :: Applicative f => (a -> f b) -> Maybe a -> f (Maybe b)
traverse _ Nothing = pure Nothing
traverse f (Just x) = fmap f (Just x)