Prolog:X是Y的祖父

时间:2010-12-07 22:31:02

标签: prolog logic-programming

假设已将以下事实输入Prolog数据库:

father(X, Y)     // X is the father of Y
mother(X, Y)     // X is the mother of Y
male(X)          // X is a male
female(X)        // X is a female
parent(X, Y)     // X is a parent of Y
diff(X, Y)       // X and Y are different

(1)现在为grandpa_of(X,Y)添加一个Prolog规则,其中“X是Y的祖父”

(2)为兄弟(X,Y)添加另一条规则,其中“X是Y的兄弟”

我的想法:

问题1:

我很困惑如何找到父母的父母,我到目前为止所有人都是

grandpa_of(X,Y): - 男(X),......

问题2:

兄弟(X,Y): - 父(P,X),父(P,Y),差异(X,Y)

2 个答案:

答案 0 :(得分:6)

我认为Jason意味着grandpa_of(X,Y) :- father(X,P), parent(P,Y).

答案 1 :(得分:2)

已经很久了......第一个是这样的:

grandpa_of(X, Y) :- father(X, P), father(P, Y).

太久了...... :-P