在prolog中,我的数据库设置如下
male("John").
male("Bob").
male("Billy").
male("Gary").
Parent("Bob","John").
Parent("Billy","Bob").
Parent("Gary", "Billy").
ancestor(Ancestor, Descendant) :-
parent(Ancestor, Descendant).
ancestor(Ancestor, Descendant) :-
parent(Ancestor, CommonAncestor),
ancestor(CommonAncestor, Descendant).是否有可能跟踪这个祖先函数的递归有多深?例如,如果我们跑
?- ancestor("Billy", "John", X).是否有可能有X返回2,或在情况下
?- ancestor("Bob", "John", X).有X返回1吗?
发布于 2016-10-15 05:39:29
哟可以写:
ancestor(Ancestor, Descendant,1) :-
parent(Ancestor, Descendant).
ancestor(Ancestor, Descendant,X) :-
parent(Ancestor, CommonAncestor),
ancestor(CommonAncestor, Descendant,X1),
X is X1+1.下面是一些例子:
?- ancestor("Billy", "John", X).
X = 2 ;
false.
?- ancestor("Bob", "John", X).
X = 1 ;
false.https://stackoverflow.com/questions/40054664
复制相似问题