Search code examples
emacselisp

How to define a function with a variable number of arguments?


Instead of this:

((lambda (a b) (apply '+ (list a b)))
 1 2)

it is possible to write this in Scheme:

((lambda args (apply '+ args))
 1 2)

Now it is possible to pass more than two arguments to the function.

When I try it in Emacs Lisp I get the error: invalid function.

How to define this function in Emacs Lisp?


Solution

  • In Emacs Lisp, you can put &rest in the argument list of the function, to get the remaining arguments as a list:

    (lambda (&rest args) (apply '+ args))