如何避免重复?

时间:2014-03-23 14:50:11

标签: prolog

我有这些谓词:

male(roelof).
male(mans). 
male(ronald).
male(jan).

female(chantal).
female(marie).
female(gerda).
female(dagmar).
female(denise).
female(kimberly).


parent(mans,gerda).
parent(mans,roelof).
parent(marie,gerda).
parent(marie,roelof).
parent(dagmar,denise).
parent(dagmar,kimberly).
parent(ronald,denise).
parent(ronald,kimberly).
parent(chantal,tamara).
parent(roelof,tamara).
parent(jan,chantal).
parent(jan,dagmar).

father_child(Father, Child) :-
 parent(Father, Child),
 male(Father).

mother_child(Mother, Child) :-
 parent(Mother, Child),
 female(Mother).

child_father_mother(Child, Father, Mother) :-
 father_child(Father, Child),
 mother_child(Mother, Child).

same_father(Child, Sibling) :-
 father_child(Father, Child),
 father_child(Father, Sibling).

same_mother(Child, Sibling) :-
 mother_child(Mother, Child),
 mother_child(Mother, Sibling).

siblings(X,Y) :-
        (   same_father(X, Y),
        X \= Y
    ;   same_mother(X, Y),
        \+ same_father(X, Y)
    ).

display_siblings(Person) :-
      findall(Person-Y, siblings(Person,Y), Sibs),
       display_the_siblings(Sibs).

display_the_siblings([]) :-
       write('Er zijn geen zussen/broers bekend').

display_the_siblings([X-Y]) :-
        write('The enigste broer of zuster is '),
        write(Y).

display_the_siblings([XY0,XY1|XYs2]) :-
        XYs0=[XY0,XY1|XYs2],
        write('Alle zusterparen zijn : \n '),
        forall(( member(X - Y,XYs0)
       ),( format('~w en ~w. \n',[X,Y])
        )).

它工作正常,但如果你做display_siblings(X),你会看到像这样的重复

第1人 - 第2人

第2人 - 第1人

如何更改此设置,以便不会显示重复项。 我知道我可以将findall改为setof但是后来我根本看不到任何输出。

鲁洛夫

编辑1:@<有效,但这也意味着显示(金伯利)失败,因为金伯利进一步在字母表然后denise。我是否可以使用与此主题相同的技巧:How can I prevent duplicates in prolog

1 个答案:

答案 0 :(得分:0)

最简单的方法应该是打破对称性,例如

... 
findall(Person-Y, (siblings(Person,Y), Person @< Y), Sibs),
...
相关问题