
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.

Nikolay Metchev wrote:
data Face = Ace | Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Jack | Queen | King deriving (Enum, Show, Eq)
listComparator :: (Eq a) => [a] -> a -> a -> Ordering listComparator xs a b = compare x y where x = elemIndex a xs y = elemIndex b xs
fourOfAKindBonusFaceOrder :: [Face] fourOfAKindBonusFaceOrder = [Queen, King, Ten, Ace, Nine, Jack]
Add: newtype FourOfAKindBonusFace = FKBF Face deriving Eq instance Ord FourOfAKindBonusFace where compare (FKBF x) (FKBF y) = listComparator fourOfAKindBonusFaceOrder x y Henceforth you can do these in the four-of-a-kind order: FKBF Queen >= FKBF Ten FKBF Ten <= FKBF Queen compare (FKBF Ten) (FKBF Queen)

On 22/04/07, Nikolay Metchev
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.
I wrote some code for comparing poker hands that you may find useful [1]. It gives each hand a classification, and then orders first on the classification, and then on some secondary property of the classification (eg, highest card in hand). - Joe [1] http://www.lambda-software.com/darcs/programming-challenges/2.8.2/poker.hs
participants (3)
-
Albert Y. C. Lai
-
Joe Thornber
-
Nikolay Metchev