我想弄清楚如何在count和sum规则上使用递归。如何在Prolog中递归计数和求和
我通常用list,findall和length或findall和sum_list来做,但我不确定这是否是我所有情况下的最佳选择。
这是我的做法与列表:
%person(name, surname, age)
person('A', 'H', 22).
person('B', 'G', 24).
person('C', 'F', 20).
person('D', 'E', 44).
person('E', 'D', 45).
person('F', 'C', 51).
person('G', 'B', 40).
person('H', 'A', 51).
count_person(Total_count) :- % rule to count how many person are.
findall(N, person(N, _, _), List),
length(List, Total_count).
sum_ages(Total_sum) :- % rule to sum all the ages.
findall(Age, person(_, _, Age), List),
sum_list(List, Total_sum).
或在这里:https://swish.swi-prolog.org/p/cswl.pl
我应该怎么做这个使用递归?