[Haskell-cafe] newbie question on ordering

Nikolay Metchev nikolaymetchev at gmail.com
Sun Apr 22 07:30:46 EDT 2007


Hello guys,
I have decided to try and get back into Haskell recently. I have used it in
the past but have forgotten large chunks.
I am trying to express the logic of a particular card game. For this purpose
I need to be able to order cards in various orders. At first I thought that
the Ord class was my answer but I found it very verbose. Below is my best
attempt so far but I can't help but feel it too is verbose and that there
must be a better way of comparing cards in different orders.

data Face = Ace | Two | Three | Four | Five | Six | Seven | Eight | Nine
  | Ten | Jack | Queen | King deriving (Enum, Show, Eq)

data Suit = Clubs | Diamonds | Hearts | Spades deriving (Show, Enum, Eq)

type Card = (Face, Suit)

listComparator :: (Eq a) => [a] -> a -> a -> Ordering
listComparator xs a b = compare x y
                        where
                          x = elemIndex a xs
                          y = elemIndex b xs

sequentialBonusFaceOrder :: [Face]
sequentialBonusFaceOrder  = [Seven, Eight, Nine, Ten, Jack, Queen, King,
Ace]

fourOfAKindBonusFaceOrder :: [Face]
fourOfAKindBonusFaceOrder = [Queen, King, Ten, Ace, Nine, Jack]

sequentialBonusFaceComparator :: Face -> Face -> Ordering
sequentialBonusFaceComparator = listComparator sequentialBonusFaceOrder

fourOfAKindBonusFaceComparator :: Face -> Face -> Ordering
fourOfAKindBonusFaceComparator = listComparator fourOfAKindBonusFaceOrder

The problem with this approach is that if you want to compare you to write
things of the following nature:
fourOfAKindBonusFaceComparator f1 f2 == GT

this isn't very clear especially if you want "<=" you have to do
fourOfAKindBonusFaceComparator f1 f2 /= GT

Any suggestions would be nice.


More information about the Haskell-Cafe mailing list