为什么我的谓词不回溯? [英] Why is my predicate not backtracking?
本文介绍了为什么我的谓词不回溯?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不明白为什么我的谓词不回溯并找到所有解决方案.
I don't understand why my predicate doesnt backtrack and find all solutions.
person(john).
person(erik).
allExceptSpider(person(Spider),T ):-
setof(person(X),person(X),S),
subtract(S,[person(Spider) ],T).
如果我用两个变量来调用该谓词:
If I call this predicate with Two variables:
allExceptSpider(person(Z),Q)
那么它只会给我答案Z = john,Q = [person(erik)] 但是找到Z = erik,Q = [person(john)]不会回溯 为什么?
Then it will only give me the answer Z = john, Q = [person(erik)] but it won't backtrack to find Z = erik ,Q = [person(john)] why?
推荐答案
TL; DR::如果您使用逻辑纯度.
person(john).
person(erik).
allExceptSpider(Spider, T) :-
setof(X, person(X), S),
subtract(S, [Spider], T).
保持纯度!怎么样?像这样使用 list_item_subtracted/3
Preserve purity! How? Use list_item_subtracted/3
like so:
allExceptSpiderNU(Spider, T) :-
setof(X, person(X), S),
list_item_subtracted(S, Spider, T).
直接查询示例:
?- allExceptSpider(Z, Q).
Q = [erik], Z = john.
?- allExceptSpiderNU(Z,Q).
Q = [ erik] , Z=john
; Q = [john ], Z=erik
; Q = [john,erik], dif(Z,erik), dif(Z,john).
这篇关于为什么我的谓词不回溯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文