Subject: Re: Defining macros ?
From: Erik Naggum <erik@naggum.no>
Date: 1999/01/16
Newsgroups: comp.lang.lisp
Message-ID: <3125445229821476@naggum.no>

* "Stephan Schweitzer" <schweitz@global.nacamar.de>
| I have a question about macro definition.
| 
| I often see a macro definition like the following :
| 
| (defmacro a-macro (value-1 value-2)
|   (let ((var-1 (gensym))
|         (var-2 (gensym)))
|     ´(let ((,var-1 ,value-1)
|            (,var-2 ,value-2))
|        .....)))
| 
| I think the second let-form is not necessary and the macro definition
| could be written like this :
| 
| (defmacro a-macro (value-1 value-2)
|   (let ((var-1 (gensym))
|         (var-2 (gensym)))
|     ´(progn
|        (setq ,var-1 ,value-1)
|        (setq ,var-2 ,value-2)
|        .....)))
| 
| So my question is :
| 
| Is there a difference between the two macro definitions?

  very much so.  macro functions return a new form that _replaces_ the
  macro call form.  that is, with

(defmacro fast (&body body) `(progn ,@body))

  defined, a call like (fast (setq foo 1) (setq bar 2)) will _expand_ into
  (progn (setq foo 1) (setq bar 2)) and then _that_ form will be analyzed
  (compiled, interpreted, whatever) in place of the original form.  note
  that macros return source code (that is, lists, usually), even though
  they are usually compiled.  macros are functions that transform source
  code, they are not functions that evaluate anything themselves.

  as you can see, the LET binding of the symbols returned by the GENSYMs in
  your example is no longer in scope by the time the returned form is
  analyzed.  the symbols are used in the former version as names of lexical
  variables with names that guarantee no collision with other symbols that
  may be used in both the macro body and the caller's scope.  that is, with

(defmacro repeat (count &body body) `(dotimes (i ,count) ,@body))

  a call to the macro REPEAT like this would be _very_ confusing:

(do ((i 0 (1+ i))
     (length (length string)))
    ((= i length))
  (repeat 4
    (write-char (char string i))))

  and this, incidentally, is why Scheme proponents are opposed to macros in
  Common Lisp.  however, it is sometimes useful to export a binding from
  the macro to the macro body, as in the macro lexically bound by the macro
  expansion form wrapping the body of a DEFMETHOD: CALL-NEXT-METHOD, and in
  the macro available to return from a LOOP: LOOP-FINISH.  (if you didn't
  get this point, keep it for later.)

  use the function MACROEXPAND to see what happens at a macro call point.
  you give it a macro call form, either read from a stream or quoted if a
  literal in the function call.

| And if there is not difference between the to definition which one is
| better programming style?

  the latter definition is not only different, it is in error, for reasons
  I hope I have made clear, so you should never use it.

#:Erik