Next: Recursive Pattern: keep, Previous: Recursive Pattern: every, Up: Recursive Patterns [Contents][Index]
Another recursive pattern is called the
accumulate pattern. In
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:
+or some other combining function, with
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
See Making a List of Files, for an example of the accumulate pattern.