2017-02-21 2 views
2

プロローグプログラムで特定の人のいとこをすべて一覧表示しようとしていますが、動作させられないようです。私は自分のコードをチェックしたが、それは正しいようだが、私が望む出力が得られていない。プロローグファミリーツリー、いとこの問題

father(john, johnny). 
father(john, peter). 
father(josh, william). 
father(simone, betty). 

mother(mary, johnny). 
mother(mary, peter). 
mother(catherine, william). 
mother(kate, betty). 

parent(A,B) :- father(A,B). 
parent(A,B) :- mother(A,B). 

siblings(B,G) :- parent(P,B), parent(P,G), B\=G. 
cousins(X,Y) :- parent(A,X), parent(B,Y), siblings(A,B), X\=Y. 

私は2人のいとこを返すようにcousins(X, william).を照会するとき、私はしたいが、私は唯一のリターンとして偽取得しています。私は間違って何をしていますか?

編集:HERESに私が今持っているが、わずか1人のいとこはここ

father(grandpa1, mary). 
 
father(grandpa1, catherine). 
 
father(grandpa2, john). 
 
father(grandpa2, simone). 
 
father(john, johnny). 
 
father(john, peter). 
 
father(josh, william). 
 
father(simone, betty). 
 

 
mother(grandma1, mary). 
 
mother(grandma1, catherine). 
 
mother(grandma2, john). 
 
mother(grandma2, simone). 
 
mother(mary, johnny). 
 
mother(mary, peter). 
 
mother(catherine, william). 
 
mother(kate, betty). 
 

 
parent(A,B) :- father(A,B). 
 
parent(A,B) :- mother(A,B). 
 

 
siblings(B,G) :- parent(P,B), parent(P,G), B\=G. 
 
cousins(X,Y) :- parent(A,X), parent(B,Y), siblings(A,B), X\=Y.

+1

ちょっとしたコメントです。 Prologは何も返さないので、 'false'が返されることはありません。目標/述語が成功するか失敗するかは、それだけです。 – Enigmativity

答えて

7

を表示するために得ることができることは

enter image description here

データベースの絵です両親の間に関係がないからといって、いとこがいないことを示しています。あなたが興味を持っている場合

enter image description here

?- forall(cousins(X,william),writeln(X)). 
johnny 
peter 
betty 

は、グラフィカルな表現は、この最小限の「のり」のコードで、このgithub repoから入手することができます:

parent_child(P,C) :- parent(P,C). 
のは、未知のが、共有、祖父母を追加してみましょう
+1

私は未知の親をいくつか追加しましたが、まだ希望の結果が得られていません。ここに私は追加されました(両親のための未知の父と母親): father(uk1f、mary)。 father(uk1f、catherine) father(uk2f、john)。 father(uk2f、simone)。 mother(uk1m、mary) 母親(uk1m、キャサリン)。 mother(uk2f、john) 母親(uk2f、simone)。 私は、いとこ(S、johnny)ではなく照会ごとに1つのいとこを取得しています。ベティだけ返す、それは間違ってクエリですか?これまでの助けてくれてありがとう – lucyb