David Andvett
David Andvett

Reputation: 153

How to stop printing the same value twice in prolog?

I have this facts or database in prolog to see if the educations is the same or less than. for example highschool <= highschool is true and highschool <= phd is true too, but masters <= highschool is false.

edu_less(high_school, bachelor).
edu_less(bachelor, masters).
edu_less(masters, phd).

edu_lessOrEqual(X,X).
edu_lessOrEqual(X, Y):- edu_less(X,Y).
edu_lessOrEqual(X, Y):- edu_less(X,Z),
                        edu_lessOrEqual(Z,Y).

but this outputs

edu_lessOrEqual(masters, phd).
true;
true;
false.

when i want only one true to be printed in the output.

true;
false.

Upvotes: 0

Views: 617

Answers (1)

Daniel Lyons
Daniel Lyons

Reputation: 22803

Basically, it's because in the third clause of edu_lessOrEqual/2, you recursively call edu_lessOrEqual/2, so you wind up with a case where Z and Y are both instantiated to phd (phd is in fact equal to phd so it fulfills the logic you have spelled out). You can correct it by adding a Z \= Y on the end of the third clause, but this is a situation where I would be tempted to use a conditional statement just to make sure I don't wind up with useless choice points.

Upvotes: 2

Related Questions