
% William Clocksin, Clause and Effect, Worksheet 6
% Length of a List
% Trace len([1,2,3],L) and lenA([1,2,3],L) to show tail recursion

len([], 0).
len([_|T],N) :- len(T,N1), N is N1 + 1.

% with an accumulator
lenA(L,N) :- accumulate(L,0,N).

accumulate([],A,A).
accumulate([_|T],A,N) :- A1 is A + 1, accumulate(T,A1,N).
