2016-07-05 6 views
2

で一緒にすべての兄弟を取得する私は、コードを次のように一緒にすべての兄弟を取得しようとしています:はプロローグ

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

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

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

しかし、それは唯一のペアを与えます。何人の兄弟がいるか分からず、兄弟姉妹のリストが必要な場合は、どうすればいいですか?

[[a, b, c], [d, e], [x, y, z, w]] 

答えて

3

あなただけsetof/3またはbagof/3を使用する必要があります。これは、彼らができることの良い例ですfindall/3(簡単に)することはできません。コースの巣の

?- bagof(C, father_child(F, C), Siblings). 
F = george, 
Siblings = [peter, eliz, hermoine] ; 
F = john, 
Siblings = [alfred, harry, ron] ; 
F = tom, 
Siblings = [sally, dick, james]. 

することができます。このfindall/3内側:father_child/2テーブル定義されているが何もない

?- findall(Siblings, 
      bagof(C, father_child(F, C), Siblings), 
      Ss). 
Ss = [[peter, eliz, hermoine], [alfred, harry, ron], [sally, dick, james]]. 

あなたは試してみて、あなたがbagof/3代わりのfindall/3を使用した場合に何が起こるか見るべきです。 (ヒント:findall/3を使用してこれはbagof(Siblings, F^bagof(C, father_child(F, C), Siblings), Ss)と同じです)

+0

完璧な、これは私が望んでいたものです。ありがとう。 – rnso