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:

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.