0

I'm trying to rule to check if someone likes a friend other than the one stated in the argument. So for example,

likes(Alice,Bob).
likes(Bob,Alice).
likes(Alice,Jeff).
likes(Jeff,Alice).

I'm trying to create a rule friends(X,Y) that if both of them like each other, we look for another pair that either X or Y is linked to. Any help?

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • Are you saying that you only want your predicate is `friends\2` to succeed if there exists a third person that either of the given people also like? – Enigmativity Apr 02 '20 at 10:13
  • What have you tried? Can you at least write out most of the program so that we have as little to do as possible? We're not a code writing service you know. – Enigmativity Apr 02 '20 at 10:16
  • This may help: https://stackoverflow.com/questions/60883594/recursive-loop-exit-statement-needed – David Tonhofer Apr 02 '20 at 10:29
  • constants are written starting with a lowercase. By using `Alice` and `Bob`, you basically say that everyone is friends with everyone. – Willem Van Onsem Apr 02 '20 at 14:40
  • Please specify how the predicate should look like, what the knowledge base is, what the expected output is, and your own attempt to solve this. – Willem Van Onsem Apr 02 '20 at 14:41

1 Answers1

0

The first solution returns the first person found that one of the two people likes, in case these 2 people like each other.

likes(alice,bob).
likes(bob,alice).
likes(alice,jeff).
likes(jeff,alice). 

friends_aux(X, Y, Z) :- likes(Y, Z), Z \= X.
friends_aux(X, Y, Z) :- likes(X, Z), Z \= Y.
friends(X, Y, Z) :- likes(X, Y), likes(Y, X), friends_aux(X, Y, Z), !.

And the second solution returns true if any of the two people have any other couple.

friends2_aux(X, Y) :- likes(Y, Z), Z \= X.
friends2_aux(X, Y) :- likes(X, Z), Z \= Y.
friends2(X, Y) :- likes(X, Y), likes(Y, X), friends2_aux(X, Y), !.

I attached an image of the results.

Results

Hopefully this is what you wanted.