Next: , Previous: , Up: Recursive Patterns   [Contents][Index]


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.

This page has generated for branch:work/add_lispintr, commit:65845cf60c073f2f3182d1d07483530e9bbe1d96 to check Japanese translation.