【发布时间】:2021-10-03 17:53:21
【问题描述】:
您好,我正在研究 PROLOG 家谱问题,这就是我目前所拥有的:
/*1. Write Prolog clauses to express the following three relationships,
* given the parent/2 relationship: grandparent/2, sibling/2, cousin/2.*/
% clauses
parent(jill, amy).
parent(jill, tim).
parent(jill, john).
parent(amy, grace).
parent(amy, anna).
parent(tim, sam).
parent(tim, joel).
parent(tim, ben).
% rules
grandparent(X,Y) :-
parent(Z,Y),
parent(X,Z).
sibling(X, Y) :-
parent(Z, X),
parent(Z, Y).
cousin(X,Y) :-
parent(P, X),
parent(S, Y),
sibling(P, S).
当我输入时:
?- sibling(X, tim).
输出给出:
X = amy
但约翰和艾米都是蒂姆的兄弟姐妹。同样的问题发生在:
?- cousin(ben, X).
给出:
X = grace
当格蕾丝和安娜都是本的堂兄弟时。 为了让代码输出 tim 的所有兄弟姐妹和 ben 的堂兄弟,我需要进行哪些更改? 谢谢。 :)
【问题讨论】:
标签: prolog