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.
A way to write a custom Ord
instance where you don't have to spell out the result of each comparison is:
instance Ord Suit where
compare a b = compare (relativeRank a) (relativeRank b) where
relativeRank Diamonds = 1
relativeRank Clubs = 2
relativeRank Hearts = 3
relativeRank Spades = 4
Here, you only need to mention each constructor once, and you can easily decide on a different ordering.
You can just as well use compare Data.Function.on relativeRank
, but this is maybe easier to understand.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With