How to sum a list of numbers in Emacs Lisp?

前端 未结 7 2134
夕颜
夕颜 2021-01-31 09:08

This works:

(+ 1 2 3)
6

This doesn\'t work:

(+ \'(1 2 3))

This works if \'cl-*\' is loaded:

7条回答
  •  太阳男子
    2021-01-31 09:43

    You can define your custom function to calculate the sum of a list passed to it.

    (defun sum (lst) (format t "The sum is ~s~%" (write-to-string (apply '+ lst))) 
    EVAL: (sum '(1 4 6 4))
    -> The sum is "15"
    

提交回复
热议问题