2
votes

Im trying to figure out how to get Prolog to generate all possible pairs from a deck of cards. I have found example where lists are used to represent all possible cards in the deck, and then using the list to get possible pairs. However I would like to generate pairs directly from the relations of card instead of using a list of cards. The problem im having with a naive implementation is that im getting all permutations instead of combinations, that is get:

C1 = king spade C2 = king club AND C1 = king club C2 = king spade

But in my example these two pairs are the same.

Below is how I represent cards in Prolog.

suite(spade).
suite(heart).
suite(diamond).
suite(club).

num(queen).
num(king).

Now my questions are: 1. What is the reason most examples uses lists when deling with different card combinatorial examples instead of using relations directly to query for possible pairs, straights etc? 2. How would a predicate look to query for all possible pairs combinations given the relations shown above?

1

1 Answers

2
votes
  1. Is definitely not the case: Quite a few examples use the facts directly and build pairs from them without using lists at all. However, a list is a natural representation for a hand of cards, so it is no wonder that somewhere along the way lists will make an occurrence.

  2. For a canonical representation, consider imposing an order on the cards. Take for example the Prolog standard order of terms and write:

:- op(400, xfx, of).

suite(spade).
suite(heart).
suite(diamond).
suite(club).

num(queen).
num(king).

pair(A-B) :-
        num(NA),
        suite(SA),
        num(NB),
        suite(SB),
        A = (NA of SA),
        B = (NB of SB),
        A @< B.

Example query:

?- pair(P).
P = queen of heart-queen of spade ;
P = queen of diamond-queen of spade ;
P = queen of diamond-queen of heart ;
P = queen of club-queen of spade ;
P = queen of club-queen of heart ;
P = queen of club-queen of diamond ;
P = king of spade-queen of spade ;
P = king of spade-queen of heart ;
P = king of spade-queen of diamond ;
P = king of spade-queen of club ;
P = king of heart-queen of spade ;
P = king of heart-queen of heart ;
P = king of heart-queen of diamond ;
P = king of heart-queen of club ;
P = king of heart-king of spade ;
P = king of diamond-queen of spade ;
P = king of diamond-queen of heart ;
P = king of diamond-queen of diamond ;
P = king of diamond-queen of club ;
P = king of diamond-king of spade ;
P = king of diamond-king of heart ;
P = king of club-queen of spade ;
P = king of club-queen of heart ;
P = king of club-queen of diamond ;
P = king of club-queen of club ;
P = king of club-king of spade ;
P = king of club-king of heart ;
P = king of club-king of diamond ;
false.

As you see, the pair king of club-king of spade does occur, but king of spade-king of club does not. Such a canonical representation will make many checks a lot easier for you.