ceving
ceving

Reputation: 23826

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?

Upvotes: 2

Views: 1117

Answers (1)

legoscia
legoscia

Reputation: 41568

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))

Upvotes: 2

Related Questions