[英]Swi Prolog: how to count the number of elements by using rules (not query)
如上所述,如何使這成為可能? 例如:
**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
。 它們是查找所有解決方案的通用 prolog 謂詞。
如果您想要一些特定於swi-prolog
的內容來計算解決方案,那么您可以使用aggregate_all
。
num_children(X, N) :- aggregate_all(count, child_of(X, _Y), N).
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.