2016-07-05 75 views
2

我使用下面的代碼:如何避免在Prolog中使用findall重複輸出?

father_child(tom, sally). 
father_child(john, alfred). 
father_child(george, peter). 
father_child(tom, dick). 
father_child(john, harry). 
father_child(george, eliz). 

siblings(X, Y):- father_child(Z, X), father_child(Z, Y), dif(X,Y). 

我可以使用下面的代碼的所有兄弟姐妹的列表,但它會產生重複的輸出,在翻轉後的兄弟姐妹位置。這怎麼可能避免:

?- findall((X,Y), siblings(X,Y), L). 
L = [ (sally, dick), (alfred, harry), (peter, eliz), (dick, sally), (harry, alfred), (eliz, peter)]. 

回答

4

一種方法是,而是採用dif/2,使用@</2的方式,不僅可以確保它們是不同的,但在一個特定的順序,確保對獨特性:

siblings(X, Y):- father_child(Z, X), father_child(Z, Y), X @< Y.