if statement - Duplicate results in prolog -


i'm terrible prolog. keep getting duplicate result in simple code"

mates(bob, john). mates(bob, bill). mates(andrea, sara). mates(andrea, bill). friends(x, y) :- mates(x, z), mates(y, z). 

calling friends(bob, x). bob twice. if use , if statement argh!!!

how can elimiate duplicate results? ie if(result1 == result2) dont print;

im looking similar friends, ie result should bob , andrea (because of bill).

shouldn't more along these lines?

friends( x , y ) :- mates( x , y ). friends( x , y ) :- mates( x , t ) , mates( t , y ). 

if want similar friends (per comment below), try:

friend( x , y ) :-   mates( x , t ) ,   mates( y , t ) ,   x \= y . 

the \= operator means 'not unifiable with', should exclude cases party friends his- or herself. exact operator 'not unifiable with' might vary depending on implementation.

also bear in mind "correct" solution bit more convoluted might seem mates relationship transitive: if andrea mate of bills, presumably bill mate of andrea's. solution should take account.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -