Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Recursion Prolog查找所有谓词_Recursion_Prolog_Artificial Intelligence_Prolog Setof - Fatal编程技术网

Recursion Prolog查找所有谓词

Recursion Prolog查找所有谓词,recursion,prolog,artificial-intelligence,prolog-setof,Recursion,Prolog,Artificial Intelligence,Prolog Setof,我正在努力寻找一个人的所有兄弟。。我创建了以下规则 find_all_brothers(Z):- findall(X,brother(X,Z),X0),write(X0). 然而,如果一个人有一个以上的兄弟,那么他只能找到一个兄弟。。我假设我必须以某种方式使用递归,但我有点卡住了 如果您有以下关系: brother(sam, bill). brother(bill, fred). 如果你想找到比尔的所有兄弟,你需要做更多的工作: find_all_brothers(Z) :-

我正在努力寻找一个人的所有兄弟。。我创建了以下规则

    find_all_brothers(Z):- findall(X,brother(X,Z),X0),write(X0).

然而,如果一个人有一个以上的兄弟,那么他只能找到一个兄弟。。我假设我必须以某种方式使用递归,但我有点卡住了

如果您有以下关系:

brother(sam, bill).
brother(bill, fred).
如果你想找到比尔的所有兄弟,你需要做更多的工作:

find_all_brothers(Z) :-
    findall(X, (brother(X, Z) ; brother(Z, X)), X0), write(X0).
为避免列表中的任何冗余成员,
setof
将排序并仅提供唯一的成员:

find_all_brothers(Z) :-
    setof(X, (brother(X, Z) ; brother(Z, X)), X0), write(X0).

findall
将找到所有这些文件。你能给我看看你兄弟的开场白吗!?哦,亲爱的哈哈!好的,我现在就把它们加起来,谢谢!:)嗯,取决于。。。见我的答案。:)@false是,我考虑了
setof
。我不确定在这种情况下是否有必要,这取决于老年退休金计划的事实和规则是如何制定的
setof
当然更安全。解决方案的顺序是否相关?冗余解决方案重要吗?在这些情况下,
findall/3
就可以了。否则,
setof/3
是更好的选择,即使在
目标
失败的情况下,它也失败了。是的,这些都是很好的问题,它们的答案不一定与原来的问题清楚。