demo_count_elems_acc/3
Instantiations of arguments in counting elements in a list
/* ************************************************ */
/* */
/* demo_count_elems_acc/3 */
/* Arg 1: List */
/* Arg 2: Counter: integer */
/* Arg 3: Total: integer */
/* Summary: calls demo_count_elems_acc/4. */
/* Author: P J Hancox */
/* Date: 21 October 1994 */
/* */
/* ************************************************ */
% 1
demo_count_elems_acc(List, 0, Total) :-
demo_count_elems_acc(List, 0, Total, 1).
/* ************************************************ */
/* */
/* demo_count_elems_acc/4 */
/* Arg 1: List */
/* Arg 2: Counter: integer */
/* Arg 3: Total: integer */
/* Arg 4: Integer */
/* Summary: Arg 3 is number of elements in Arg 1. */
/* Arg 2 is counter in accumulator. */
/* Arg 4 represents depth of recursion. */
/* Author: P J Hancox */
/* Date: 21 October 1994 */
/* */
/* ************************************************ */
% 1 terminating condition
demo_count_elems_acc([], Total, Total, Depth) :-
tab(Depth * 5),
write('At the termination condition'),
nl,
tab((Depth * 5) + 3),
write('List is: '),
write([]),
nl,
tab((Depth * 5) + 3),
write('Count is: '),
write(Total),
nl,
tab((Depth * 5) + 3),
write('Total is: '),
write(Total),
nl.
% 2 recursive
demo_count_elems_acc([Head|Tail], Sum, Total, Depth1) :-
tab(Depth1 * 5),
write('Before recursion at depth '),
write(Depth1),
nl,
tab((Depth1 * 5) + 3),
write('List is: '),
write([Head|Tail]),
nl,
tab((Depth1 * 5) + 3),
write('Count is: '),
write(Sum),
nl,
tab((Depth1 * 5) + 3),
write('Total is: '),
write(Total),
nl,
Depth2 is Depth1 + 1,
Count is Sum + 1,
demo_count_elems_acc(Tail, Count, Total, Depth2),
tab(Depth1 * 5),
write('After recursion at depth '),
write(Depth1),
nl,
tab((Depth1 * 5) + 3),
write('List is: '),
write([Head|Tail]),
nl,
tab((Depth1 * 5) + 3),
write('Count is: '),
write(Sum),
nl,
tab((Depth1 * 5) + 3),
write('Total is: '),
write(Total),
nl.
/* ************************************************ */
/* End of program */
/* ************************************************ */