summaryrefslogtreecommitdiff
path: root/Haskell-book/06/src/EqInstances.purs
blob: 987d2731bd91339dd3c0ce2d6b6e10f7569cf4bf (plain)
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
module EqInstances where
    -- Exercise: Eq Instances
    -- p. 178

import Prelude

-- Ex. 1
data TisAnInteger =
    TisAn Int

instance eqTisAn :: Eq TisAnInteger where
    eq x y = x == y

-- Ex. 2
data TwoIntegers =
    Two Int Int

instance ewTwoIntegers :: Eq TwoIntegers where
    eq (Two x y) (Two x' y') = x == x' && y == y'

-- Ex. 3
data StringOrInt =
    TisAnInt Int
  | TisAString String

instance eqStringOrInt :: Eq StringOrInt where
    eq (TisAnInt x) (TisAnInt y) = x == y
    eq (TisAString x) (TisAString y) = x == y
    eq _ _ = false

-- Ex. 4
data Pair a =
    Pair a a

instance eqPair :: Eq a => Eq (Pair a) where
    eq (Pair a b) (Pair a' b') = a == a' && b == b'

-- Ex. 5
data Tuple a b =
    Tuple a b

instance eqTuple :: (Eq a, Eq b) => Eq (Tuple a b) where
    eq (Tuple a b) (Tuple a' b') = a == a' && b == b'

-- Ex. 6
data Which a =
    ThisOne a
  | ThatOne a

instance eqWhich :: Eq a => Eq (Which a) where
    eq (ThisOne x) (ThisOne y) = x == y
    eq (ThatOne x) (ThatOne y) = x == y
    eq _ _ = false

-- Ex. 7
data EitherOr a b =
    Hello a
  | Goodbye b

instance eqEitherOr :: (Eq a, Eq b) => Eq (EitherOr a b) where
    eq (Hello _) (Goodbye _) = false
    eq (Goodbye _) (Hello _) = false
    eq _ _ = true