How could I convert the following into a tail recursive version.
sum(void,0).
sum(t(V,L,R),S) :-
sum(L,S1),
sum(R,S2),
S is V + S1 + S2.
It seems impossible to maintain a single accumulator as the branching is in 2^n magnitude.
A possible solution would be to have the accumulator add a new accumulator to a list on each iteration. Maybe the above solution is optimal?
Thanks in advance.