Recursive Pattern: _accumulate_
...............................
Another recursive pattern is called the `accumulate' pattern. In
the `accumulate' recursive pattern, an action is performed on every
element of a list and the result of that action is accumulated with the
results of performing the action on the other elements.
This is very like the `every' pattern using `cons', except that
`cons' is not used, but some other combiner.
The pattern is:
* If a list be empty, return zero or some other constant.
* Else, act on the beginning of the list (the CAR of the list),
- and combine that acted-on element, using `+' or some
other combining function, with
- a recursive call by the function on the rest (the CDR) of
the list.
Here is an example:
(defun add-elements (numbers-list)
"Add the elements of NUMBERS-LIST together."
(if (not numbers-list)
0
(+ (car numbers-list) (add-elements (cdr numbers-list)))))
(add-elements '(1 2 3 4))
=> 10
Note:Making a List of Files, for an example of the
accumulate pattern.