www.delorie.com/gnu/docs/emacs-lisp-intro/emacs-lisp-intro_180.html   search  
Buy the book!

Programming in Emacs Lisp

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [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)
    (+ (car numbers-list) (add-elements (cdr numbers-list)))))

(add-elements '(1 2 3 4))
    => 10

See section Making a List of Files, for an example of the accumulate pattern.

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003