如上所述,如何实现这一点?例如:
**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和child 2(或类似的东西。非常感谢。
您应该了解setof/3
、bagof/3
和findall/3
。它们是用于查找所有解决方案的通用prolog谓词。
如果你想要swi-prolog
特定的东西来计算解决方案,那么你可以使用aggregate_all
。
num_children(X, N) :- aggregate_all(count, child_of(X, _Y), N).
https://www.swi-prolog.org/FAQ/SingletonVar.html