type Name = [Char] type Score = Int data Kind = Assignment | Exam | Quiz deriving (Eq, Ord, Show) -- A "named tuple" for representing entries in a "gradebook" data GradeEntry = Entry Name Kind Score deriving (Show) -- Implementing a different Ord functionality -- we only look -- at scores when comparing instance Ord GradeEntry where compare (Entry _ _ score1) (Entry _ _ score2) | score1 > score2 = GT | score1 < score2 = LT | otherwise = EQ -- A goofy version of == for GradeEntries that only looks at -- the names. We define our own "==" function. instance Eq GradeEntry where (Entry name1 _ _) == (Entry name2 _ _) = name1 == name2 getScore (Entry _ _ points) = points entries = [Entry "Brad" Assignment 87, Entry "Brad" Exam 35, Entry "Bob" Quiz 100] data Coord = Cartesian Float Float | Polar Float Float {- distance (Cartesian x y) = ... distance (Polar r theta) = ... -} -- Our first recursive type -- Nat is on left and right data Nat = Zero | Succ Nat deriving (Eq, Ord, Show) natToInt Zero = 0 natToInt (Succ n) = 1 + natToInt n -- Another recursive type. Could implement our own lists! data IntList = Empty | LNode Int IntList deriving (Eq, Ord, Show) sumIL Empty = 0 sumIL (LNode num rest) = num + (sumIL rest) -- Polymorphic type definition. Give me a type for a, and -- an AnyList is an a followed by an AnyList of a's. data AnyList a = Nil | ALNode a (AnyList a) deriving (Eq, Show) data Possibly a = Fail | Ok a deriving (Eq, Ord, Show) firstSatisfying p [] = Fail firstSatisfying p (x:xs) | p x = Ok x | otherwise = firstSatisfying p xs