-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter6exercises.hs
79 lines (42 loc) · 1.28 KB
/
chapter6exercises.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
module Chapter6Exercises where
data TisAnInteger = TisAn Integer
instance Eq TisAnInteger where
(==) (TisAn v) (TisAn v') = v == v'
data TwoIntegers = Two Integer Integer
instance Eq TwoIntegers where
(==) (Two a b) (Two a' b') =
a == a' && b == b'
data StringOrInt =
TisAnInt Int
| TisAString String
instance Eq StringOrInt where
(==) (TisAnInt a) (TisAnInt a') = a == a'
(==) (TisAString a) (TisAString a') = a == a'
(==) (TisAnInt a) (TisAString a') = False
(==) (TisAString a) (TisAnInt a') = False
data Pair a =
Pair a a
instance Eq a => Eq (Pair a) where
(==) (Pair v w) (Pair v' w') =
v == v' && w == w'
data Tuple a b =
Tuple a b
instance (Eq a, Eq b) => Eq (Tuple a b) where
(==) (Tuple x y) (Tuple x' y') =
x == x' && y == y'
data Which a =
ThisOne a
| ThatOne a
instance (Eq a) => Eq (Which a) where
(==) (ThisOne x) (ThisOne x') = x == x'
(==) (ThatOne x) (ThatOne x') = x == x'
(==) (ThisOne x) (ThatOne x') = False
(==) (ThatOne x) (ThisOne x') = False
data EitherOr a b =
Hello a
| Goodbye b
instance (Eq a, Eq b) => Eq (EitherOr a b) where
(==) (Hello x) (Hello x') = x == x'
(==) (Goodbye x) (Goodbye x') = x == x'
(==) (Hello x) (Goodbye y) = False
(==) (Goodbye x) (Hello y) = False