Example Macro

Some Lisp dialects have a function neq?, a negated form of eq?. neq? can be defined as a macro as follows:

(define-macro neq?
  (lambda (x y)
    (list 'not (list 'eq? x y)) ))

When neq? is used, e.g. in (neq? (car l) 'foo), the Lisp interpreter first evaluates the macro with x bound to (car l) and y bound to (quote foo). The macro returns:
      (not (eq? (car l) (quote foo))) ,
which is evaluated by the interpreter or compiler just as if it were the original expression.

In this case, it does not matter if the variables x and y conflict with user variables, since none of the user's code is evaluated within the macro.

Contents    Page-10    Prev    Next    Page+10    Index