Prolog Predicate returns true when two people have the same hobby

I want to write a Prolog predicate that returns true when two people have the same hobby, without using negation. I have the following database:

likes(john,movies). 
likes(john,tennis). 
likes(john,games). 
likes(karl,music). 
likes(karl,running). 
likes(peter,swimming).
likes(peter,movies). 
likes(jacob,art). 
likes(jacob,studying). 
likes(jacob,sleeping). 
likes(mary,running). 
likes(mary,sleeping). 
likes(sam,art). 
likes(sam,movies).

I came up with the following predicate:

same_hobby(X,Y) :-
   likes(X,Z),
   likes(Y,Z).

However, this predicate is also true, if Xequal Y, and I do not want this to be so. Can someone help me find a solution? A small explanation would also be greatly appreciated.

+4
source share
2 answers

dif/2, dif (Term1, Term2), , Term1 Term2, . :

same_hobby(X,Y) :-
            likes(X,Z),
            likes(Y,Z),
            dif(X,Y).

dif/2 - ,

same_hobby(X,Y) :-
            dif(X,Y),
            likes(X,Z),
            likes(Y,Z).

, X Y, (X, Z), (Y, Z), .

+2

dif/2, , X Y :

same_hobby(X, Y) :-
    likes(X, Z),
    likes(Y, Z),
    dif(X, Y).

, , X Y , same_hobby/2 .

+2

Source: https://habr.com/ru/post/1618886/


All Articles