glenn@mit-eddie.MIT.EDU (Glenn Meader) (01/07/87)
Could someone who is not offended by the triviality of this question please tell me how to test things inside a defmacro definition. By the way I use Common Lisp. Example: (defmacro foo(&rest body)) I want this form to translate to (/ body) if (oddp (car body)) and to (* body) if (evenp (car body)). This example is only to demonstrate a principle. My LISP, when given (foo 2 3 4), complains that '2 is a bad function. Also is there a more appropriate group for this type of elementary question? Thanks.
barmar@mit-eddie.MIT.EDU (Barry Margolin) (01/09/87)
In article <4455@mit-eddie.MIT.EDU> glenn@mit-eddie.MIT.EDU (Glenn Meader) writes: >Example: (defmacro foo(&rest body)) > >I want this form to translate to (/ body) if (oddp (car body)) >and to (* body) if (evenp (car body)). This example is only >to demonstrate a principle. My LISP, when given (foo 2 3 4), complains >that '2 is a bad function. Also is there a more appropriate group for >this type of elementary question? Thanks. It would have helped if you had included the rest of your macro in your example, but I think I can guess what it looked like: (defmacro foo (&rest body) (if (oddp (car body)) `(/ ,body) `(* ,body))) The problem is not in the way you do the test, it is in the expansions. Since the value of BODY is a list, the expansion of (foo 2 3 4) is (* (2 3 4)) Now it should be obvious why it complained that 2 is a bad function. The fix is to use ". ,body" in the macro instead of ",body". One way to learn these things by yourself is to make use of the MACROEXPAND function to see what the expansion is. If you're lucky, your implementation will have an interactive macroexpander, like the Lisp Machine's MEXP. -- Barry Margolin ARPA: barmar@MIT-Multics UUCP: ..!genrad!mit-eddie!barmar
kuczynsk@crcge1.UUCP (Pascal Kuczynski) (01/09/87)
In article <4455@mit-eddie.MIT.EDU> glenn@mit-eddie.UUCP writes: >Example: (defmacro foo(&rest body)) > >I want this form to translate to (/ body) if (oddp (car body)) >and to (* body) if (evenp (car body)). This example is only >to demonstrate a principle. My LISP, when given (foo 2 3 4), complains >that '2 is a bad function. (defmacro foo (&rest body) (cons (if (oddp (car body)) '/ '*) body)) or more legible: (defmacro foo (&rest body) (if (oddp (car body)) `(/ ,@body) `(* ,@body))) -- Pascal Kuczynski. Laboratoires de Marcoussis. Route de Nozay. Marcoussis 91460. FRANCE ...mcvax!inria!crcge1!kuczynsk