Making a custom datatype ord-able

functional-programming, haskell, types

Solution

The definition of Ord looks something like (but not quite)

class Ord a where
    compare :: a -> a -> Ordering

and `Ordering` has three possible values: `LT, EQ, GT`.

So, you need to define what the result of each comparison should be. Something like:

instance Ord Suit where
    compare Clubs Diamonds    = LT
    compare Diamonds Clubs    = GT
    compare Diamonds Diamonds = EQ
    compare Diamonds _        = LT -- Diamonds are lower than everything besides Clubs and Diamonds

Your actual ordering may be different, but that should give you the basic idea.

Problem

Can someone explain to me how do I make a custom data type ord-able? ** I'm not allowed to make modifications to Suit itself, eg. deriving (Eq, Ord) ``` data Suit = Clubs | Diamonds | Hearts | Spades deriving (Eq) ``` My Attempt: ``` instance Ord Suit where compare suit1 suit2 = compare suit1 suit2 ``` but that seems to go on a continuous loop and doesn't halt.

Original source