Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing duplicates

Status
Not open for further replies.

nojero

Programmer
Sep 13, 2010
1
AR
Hi,

I've the following definitions.(* player(name,age) *)

player(peter,9).
player(paul,10).
player(chris,9).
player(susan,9).

games(X,Y) :- player(X,9),player(Y,9),X \== Y.

I want Prolog to return all possible games to play in a tournament, but only with people aged 9.

The problem is that, when asking

?- games(X,Y).

it gives both

X = chris
Y = susan

and

X = susan
Y = chris

I know I have to use the cut operator (!), and 'fail maybe, but I'm not so sure how to do it.

Can anyone help?

Thx in advance!
 
You can make use of the standard ordering of Prolog terms to invalidate the undesired pair:

Code:
games(X, Y) :-
    player(X, 9),
    player(Y, 9),
    X @< Y.

You need the @< operator because you don't compare integers or strings, but atoms. The @< operator may seem strange, but it does its job. Here is the help page for @<:


+Term1 @< +Term2 [ISO]
True if Term1 is before Term2 in the standard order of terms.


Now chris @< susan, but not susan @< chris.
So only X = chris, Y = susan pair will be validated
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top