How to sum a list of numbers in Emacs Lisp?

jfs picture jfs · Feb 26, 2009 · Viewed 27.1k times · Source

This works:

(+ 1 2 3)
6

This doesn't work:

(+ '(1 2 3))

This works if 'cl-*' is loaded:

(reduce '+ '(1 2 3))
6

If reduce were always available I could write:

(defun sum (L)
  (reduce '+ L))

(sum '(1 2 3))
6

What is the best practice for defining functions such as sum?

Answer

kmkaplan picture kmkaplan · Feb 26, 2009
(apply '+ '(1 2 3))