Re: Help - how to pass a "keyed parameter list" to a macro or function - beginner is at wits end
Steve Haflich <[email protected]> Fri, 02 Jul 2004 06:05:06 -0700
| Newsgroups | gmane.lisp.allegro |
|---|---|
| Message-ID | <29291.1088773506@bach> |
From: Gary King <[email protected]> In short, use #'apply. For example: (apply #'make-instance 'foo '(:a 1 :b 2 :c 3)) Correct, but you didn't answer the second question about macros. It is also useful to understand this syntax: > (defmacro create-obj (arg) > `(make-instance 'obj ,@arg)) > > This does not expance correctly, and I have tried everything under the > sun that I could think of , except the correct method? (defmacro create-obj (&rest args) `(make-instance 'obj ,@args)) The above definition works, but be sure to understand that it does not need to be a macro. Since it evaluates its arguments in the usual functional manner, it would work exactly the same as the code Gary implied: (defun create-obj (&rest args) (apply #'make-instance 'obj args)) The macro version is an example of a macro that has functional semantics. The prog1 and prog2 macros are other examples of macros that could have been defined as functions with the same syntax and semantics. (defun prog1 (arg1 &rest args) (declare (ignore args)) arg1)