我怎么解决这个问题??
输入:count_atoms(T,Count)
输出 count_atoms(a(b,c(d,e),f),Count)。计数 = 4 ;
我真的不知道......请你能帮帮我吗?
也许基于堆栈的方法可以提供帮助。您可以编写至少四个辅助谓词,如下所示:
% Increases accumulator if T is atomic + call to count_atoms/6
count_atoms(T, Stack, StackSize, Accumulator, Count) :- ...
% Gets the arity of T if T is compound + call to count_atoms/6
count_atoms(T, Stack, StackSize, Accumulator, Count) :- ...
% Gets Nth subterm of T and puts it on the stack + call to count_atoms/6
count_atoms(T, N, Stack, StackSize, Accumulator, Count) :- ...
% Pops element from stack + call to count_atoms/5
count_atoms(T, _, Stack, StackSize, Accumulator, Count) :- ...
但是您仍然需要一个 count_atoms/2 谓词和一个来停止算法并产生结果。