[英]Prolog:simple family relation
I am learning Prolog and what i want is to do a simple "calculation"(i dont know how its called in Prolog) of the members of a simle family. 我正在学习Prolog,我想要做的是一个简单的家族成员的简单“计算”(我不知道Prolog中的称呼)。
For example i have: 例如我有:
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
(it seems funny and completely out of logic when its out of my mind :) ) (当它在我脑海中时,似乎很有趣,而且完全不合逻辑:))
father(steve,petter).
brother(john,steve).
father(X,Y):-brother(X,Z),father(Z,Y)).
and my question is who is the father of john(the correct awnser would be petter) 我的问题是,谁是约翰的父亲(正确awnser将是皮特)
?-father(john,X).
But it always giving me false. 但这总是给我假的。
When you enter father(john, X).
当您输入father(john, X).
, it starts by trying to find a Z
such that brother(john, Z)
is true. ,它首先尝试找到一个Z
,以使brother(john, Z)
为真。 No such Z
exists, so it returns false. 不存在这样的Z
,因此它返回false。
Note that brother(steve, john)
does not imply brother(john, steve)
unless you tell Prolog that it should. 请注意,除非您告诉Prolog应该, brother(steve, john)
并不暗示brother(john, steve)
。
SOLVED: 解决了:
father(steve,petter).
brother(john,steve).
whoisfather(X,Y):-brother(X,Z),father(Z,Y).
?- whoisfather(john,X).
X = petter.
instead of 代替
father(steve,petter).
brother(john,steve).
father(X,Y):-brother(X,Z),father(Z,Y)).
?- father(john,X).
false.
See comments 看评论
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.