问题描述
正如上面的标题,如何使这成为可能? 例如:
**Facts:**
parent(child,parent).
parent(child,parent2).
parent(child2,parent).
parent(child2,parent2).
**Rules:**
childof(X,Y) :- parent(Y,X).
number_of_child(X,Y):- X has Y number of child
我应该如何实现 number_of_child 规则? 我的预期答案是 Y 将显示 2(因为有 child 和 child2)或类似的东西。 谢谢。
解决方法
您应该了解 setof/3
、bagof/3
和 findall/3
。它们是找到所有解决方案的通用序言谓词。
如果您想要一些特定的 swi-prolog
来计算解决方案,那么您可以使用 aggregate_all
。
num_children(X,N) :- aggregate_all(count,child_of(X,_Y),N).