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 when X
equals Y
and I do not want this to be the case. Can anyone help me find a solution? A small explanation would also be greatly appreciated.
dif/2
: This predicate is true iff its arguments are different. So:same_hobby(X, Y) :- dif(X, Y), likes(X, Hobby), likes(Y, Hobby).
– mat