[comp.lang.lisp] Macros for the elementary lithp prog

wagner@iaoobelix.UUCP (01/08/87)

> /***** iaoobelix:comp.lang.lisp / mit-eddi!glenn /  3:21 am  Jan  7, 1987*/
> Subject: Macros for the elementary lithp programmer
> 
> 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))
>

That's not valid CommonLISP. Something's missing!?


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

Your problem is to translate (foo . args) to (/ . args) or (* . args)
depending on whether the first arg is an odd number or not.

Unfortunately you didn't supply your macro expansion, so it is quite
difficult to say what's wrong with your solution. However, here is one
that works:

	(defmacro foo (&rest args)
	  `(if (oddp ,(car args))
	       (/ ,@args)
	       (* ,@args)))

Note that the *expansion* itself contains the if form. This is necessary
because you may desire to write not only (foo 2 3 4) but also 
(foo (+ 1 2) 3 4) where the first arg is not a constant number.

Without this restriction (i.e. assuming the first arg is a constant
number) you could write the following (since arg1 is a constant, we
can check oddp *while* expanding the macro form):

	(defmacro foo (&rest args)
	  (if (oddp (car args))
	      `(/ ,@args)
	      `(* ,@args)))

The above functions will work under CommonLISP.

Your mistake could have been the wrong time for checking the oddp
condition. Checking has to be performed while evaluating the new
expansion, not while expanding the macro!

In case of further questions you can contact me directly via
E-Mail (see below).

Juergen Wagner,			(USENET)   ...!unido!iaoobel!wagner
				Fraunhofer Institute IAO, Stuttgart