From a639897ae1607f70b98928f82f6d21534e173521 Mon Sep 17 00:00:00 2001 From: Evan Turner Date: Fri, 19 Jan 2018 22:23:22 -0500 Subject: [PATCH] Complete redo of Eq instances exercise --- 06/06.05-eq-instances.hs | 65 ++++++++++++++++++++++++++++++++++++++++ 06/eq-instances.hs | 57 ----------------------------------- 2 files changed, 65 insertions(+), 57 deletions(-) create mode 100644 06/06.05-eq-instances.hs delete mode 100644 06/eq-instances.hs diff --git a/06/06.05-eq-instances.hs b/06/06.05-eq-instances.hs new file mode 100644 index 0000000..748338f --- /dev/null +++ b/06/06.05-eq-instances.hs @@ -0,0 +1,65 @@ +-- 1. +data TisAnInteger = TisAn Integer + deriving Show + +instance Eq TisAnInteger where + (==) (TisAn x) (TisAn y) = x == y + +----------------------------------------------------------------------------- +-- 2. +data TwoIntegers = Two Integer Integer + deriving Show + +instance Eq TwoIntegers where + (==) (Two x y) (Two x' y') = x == x' && y == y' + +----------------------------------------------------------------------------- +-- 3. +data StringOrInt = TisAnInt Int + | TisAString String + deriving Show + +instance Eq StringOrInt where + (==) (TisAnInt x) (TisAnInt y) = x == y + (==) (TisAString x) (TisAString y) = x == y + (==) _ _ = False + +----------------------------------------------------------------------------- +-- 4. +data Pair a = Pair a a + deriving Show + +instance Eq a => Eq (Pair a) where + (==) (Pair x y) (Pair x' y') = x == x' && y == y' + +----------------------------------------------------------------------------- +-- 5. +data Tuple a b = Tuple a b + deriving Show + +instance (Eq a, Eq b) => Eq (Tuple a b) where + (==) (Tuple x y) (Tuple x' y') = x == x' && y == y' + +----------------------------------------------------------------------------- +-- 6. +data Which a = ThisOne a + | ThatOne a + deriving Show + +instance Eq a => Eq (Which a) where + (==) (ThisOne x) (ThisOne x') = x == x' + (==) (ThatOne x) (ThatOne x') = x == x' + (==) _ _ = False + +----------------------------------------------------------------------------- +-- 7. +data EitherOr a b = Hello a + | Goodbye b + deriving Show + +instance (Eq a, Eq b) => Eq (EitherOr a b) where + (==) (Hello x) (Hello x') = x == x' + (==) (Goodbye x) (Goodbye x') = x == x' + (==) _ _ = False + + diff --git a/06/eq-instances.hs b/06/eq-instances.hs deleted file mode 100644 index 45d5914..0000000 --- a/06/eq-instances.hs +++ /dev/null @@ -1,57 +0,0 @@ --- 1. -data TisAnInteger = - TisAn Integer - -instance Eq TisAnInteger where - (==) (TisAn x) (TisAn y) = x == y - --- 2. -data TwoIntegers = - Two Integer Integer - -instance Eq TwoIntegers where - (==) (Two x y) (Two x' y') = x == x && y == y' - --- 3. -data StringOrInt = - TisAnInt Int - | TisAString String - -instance Eq StringOrInt where - (==) (TisAnInt x) (TisAnInt y) = x == y - (==) (TisAString x) (TisAString y) = x == y - - --- 4. -data Pair a = - Pair a a - -instance Eq a => Eq (Pair a) where - (==) (Pair x y) (Pair x' y') = x == x' && y == y' - --- 5. -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' - --- 6. -data Which a = - ThisOne a - | ThatOne a - -instance Eq a => Eq (Which a) where - (==) (ThisOne x) (ThisOne y) = x == y - (==) (ThatOne x) (ThatOne y) = x == y - (==) _ _ = False - --- 7. -data EitherOr a b = - Hello a - | Goodbye b - -instance (Eq a, Eq b) => Eq (EitherOr a b) where - (==) (Hello x) (Hello y) = x == y - (==) (Goodbye x) (Goodbye y) = x == y - (==) _ _ = False