Reputation: 689
Say I have a function foo:
(defun foo (x y &rest args)
...)
And I later want to wrap it with a function bar:
(defun bar (x &rest args)
(foo x 100 args))
Assume bar was then called like this: (bar 50 1 2 3)
With this setup, args is a list within the body of bar that holds the trailing parameters, so when I pass it to foo, instead of getting the equivalent of (foo 50 100 1 2 3)
I of course get (foo 50 100 '(1 2 3))
. If these were macros, I would use ``(foo ,x 100 ,@args)` within the body of bar to splice args into the function call. ,@ only works inside a backtick-quoted list, however.
How can I do this same sort of splicing within a regular function?
Upvotes: 22
Views: 5507
Reputation: 149
This method is slow, but it may be what you're looking for.
Backquotes, commas, and comma-ats aren't only for macros. They can also be used in functions if you use eval too. Again, this is not fast or efficient. At any rate, here it is:
(defun bar (x &rest args)
(eval `(foo ,x 100 ,@args)))
Upvotes: 1
Reputation: 11854
APPLY will call its first argument with its subsequent arguments, and the last argument must be a list. So:
(apply #'foo x 100 args)
Upvotes: 38