/* carddeck returns a deck of cards in normal order, represented as a list of tuples, each tuple representing the card value and suit. */ carddeck(CardDeck) :- addcard(1, spade, CardDeck). /* addcard takes the number and suit of the current card to be added, and returns the new set of cards with the current card added */ addcard(1, nil, []). /* terminate when all suits exhausted */ addcard(1, Suit, CardDeck) :- addcard(2, Suit, CardDeck1), append([card(ace, Suit)], CardDeck1, CardDeck). addcard(Number, Suit, CardDeck) :- Number > 1, Number < 11, Number1 is Number + 1, addcard(Number1, Suit, CardDeck1), append([card(Number, Suit)], CardDeck1, CardDeck). addcard(11, Suit, CardDeck) :- nextsuit(Suit, NextSuit), addcard(1, NextSuit, CardDeck1), append([card(jack, Suit), card(queen, Suit), card(king, Suit)], CardDeck1, CardDeck). /* nextsuit moves to the next suit. */ nextsuit(spade, heart). nextsuit(heart, diamond). nextsuit(diamond, club). nextsuit(club, nil). /* standard append */ append([], Ys, Ys). append([X | Xs], Ys, [X | Zs]) :- append(Xs, Ys, Zs).