-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ch16_Exercises.hs
86 lines (62 loc) · 2.26 KB
/
Ch16_Exercises.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
{-# LANGUAGE FlexibleInstances #-}
module Ch16_Exercises where
-- Write Functor instances for the following datatypes.
-- 01
data Quant a b = Finance | Desk a | Bloor b deriving (Show, Eq)
instance Functor (Quant a) where
_ `fmap` Finance = Finance
_ `fmap` (Desk a) = Desk a
f `fmap` (Bloor b) = Bloor $ f b
-- 02
data K a b = K a deriving (Show, Eq)
instance Functor (K a) where
_ `fmap` (K a) = K a
-- 03
data Flip f b a = Flip (f a b) deriving (Show, Eq)
instance Functor (Flip K b) where
f `fmap` (Flip (K a)) = Flip $ K (f a)
-- 04
data EvilGoateeConst a b = GoatyConst b deriving (Show, Eq)
instance Functor (EvilGoateeConst a) where
f `fmap` (GoatyConst b) = GoatyConst (f b)
-- 05
data LiftItOut f a = LiftItOut (f a) deriving (Show, Eq)
instance Functor f => Functor (LiftItOut f) where
f `fmap` (LiftItOut fa) = LiftItOut $ f <$> fa
-- 06
data Parappa f g a = DaWrappa (f a) (g a) deriving (Show, Eq)
instance (Functor f, Functor g) => Functor (Parappa f g) where
f `fmap` (DaWrappa fa ga) = DaWrappa (f <$> fa) (f <$> ga)
-- 07
data IgnoreOne f g a b = IgnoringSomething (f a) (g b) deriving (Show, Eq)
instance Functor g => Functor (IgnoreOne f g a) where
f `fmap` (IgnoringSomething fa gb) = IgnoringSomething fa (f <$> gb)
-- 08
data Notorious g o a t = Notorious (g o) (g a) (g t) deriving (Show, Eq)
instance Functor g => Functor (Notorious g o a) where
f `fmap` (Notorious go ga gt) = Notorious go ga (f <$> gt)
-- 09
data List a = Nil | Cons a (List a) deriving (Show, Eq)
instance Functor List where
_ `fmap` Nil = Nil
f `fmap` (Cons a la) = Cons (f a) (f `fmap` la)
-- 10
data GoatLord a =
NoGoat
| OneGoat a
| MoreGoats (GoatLord a)
(GoatLord a)
(GoatLord a)
deriving (Show, Eq)
instance Functor GoatLord where
_ `fmap` NoGoat = NoGoat
f `fmap` (OneGoat a) = OneGoat (f a)
f `fmap` (MoreGoats ga ga' ga'') = MoreGoats (f `fmap` ga)
(f `fmap` ga')
(f `fmap` ga'')
-- 11
data TalkToMe a = Halt | Print String a | Read (String -> a)
instance Functor TalkToMe where
_ `fmap` Halt = Halt
f `fmap` (Print s a) = Print s (f a)
f `fmap` Read g = Read (f . g)