[Haskell-cafe] newbie question on ordering

Albert Y. C. Lai trebla at vex.net
Mon Apr 23 01:10:35 EDT 2007


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)



More information about the Haskell-Cafe mailing list