fold, reduce, in emacs lisp

Hauke Rehfeld send in this improved version of “fold”.

(defun fold (f x list)
  "Recursively applies (F i j) to LIST starting with X.
For example, (fold F X '(1 2 3)) computes (F (F (F X 1) 2) 3)."
 (let ((li list) (x2 x))
   (while li
     (setq x2 (funcall f x2 (pop li)))
   )
   x2
  )
)

This one improves upon on a primitive one i've written, which has problems if element in the list eval to nil. For some info and a practical example of using “fold”, See: More Elisp Examples. Thanks Hauke.

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs