0

我正在学习 Prolog,我想要的是对一个简单的家庭成员进行简单的“计算”(我不知道它在 Prolog 中是如何调用的)。

例如我有:

 1)father of steve is petter
 2)brother steve is john
 3)A person is a son to a Father when the brother of the person has  as father the Father

(当它不在我的脑海里时,它看起来很有趣并且完全不合逻辑:))

father(steve,petter).
brother(john,steve).
father(X,Y):-brother(X,Z),father(Z,Y)).

我的问题是约翰的父亲是谁(正确的遮阳篷应该是皮特)

?-father(john,X).

但它总是给我虚假。

4

2 回答 2

0

解决了:

father(steve,petter).
brother(john,steve).
whoisfather(X,Y):-brother(X,Z),father(Z,Y).
?- whoisfather(john,X).
X = petter.

代替

father(steve,petter).
brother(john,steve).
father(X,Y):-brother(X,Z),father(Z,Y)).
?- father(john,X).
false.

看评论

于 2012-11-03T11:17:21.230 回答
0

当你进入father(john, X).时,它首先试图找到一个Z这样的,这brother(john, Z)是真的。不Z存在这样的,所以它返回false。

请注意,除非您告诉 Prolog 它应该这样做,否则这brother(steve, john)并不意味着。brother(john, steve)

于 2012-11-03T00:13:44.727 回答