From: lisplover
Subject: how can I implement this in macro?
Date: 
Message-ID: <1112059374.950229.231710@f14g2000cwb.googlegroups.com>
I am reading PG's on lisp and find this

(defvar *!equivs* (make-hash-table))
(defun ! (fn)
    (or (gethash fn *!equivs*) fn))
(defun def! (fn fn!)
    (setf (gethash fn *!equivs*) fn!))

to return a destructive equivalent. He comments that "Since the
relation between a function and its destructive counterpart will
usually be known before runtime, it would be most efficient to define !
as a macro, or even provide a read macro for it."
I try to implement that in macro. but find that when I define
(defmacro !(fn)
  `(gethash (symbol-function ',fn) *!equivs*))
It is fine to call
CL-USER>(funcall (! reverse) '(1 2))
->(2 1)

But when I call it through a function

CL-USER> (defun test2(fn)
           (funcall (! fn) '(1 2)))
TEST2
CL-USER> (test2 'reverse)
Error occurs.
SYMBOL-FUNCTION: undefined function FN
   [Condition of type SYSTEM::SIMPLE-UNDEFINED-FUNCTION]

Would anyone like to point out what is the proper way to do it in
macro? Thanks.

From: Peter Scott
Subject: Re: how can I implement this in macro?
Date: 
Message-ID: <1112061177.483451.256280@l41g2000cwc.googlegroups.com>
In this code:

(defun test2 (fn)
  (funcall (! fn) '(1 2)))

You're not passing the value of the variable called "FN" to !, you're
passing the symbol 'FN. Macros don't evaluate their arguments, so it
tries to find the destructive equivalent of the function called "FN".
Failing this, it gives you an error.

You need to ask yourself what you're trying to accomplish. You're using
a macro to move the work to compile-time, but then you're asking TEST2
to do the work at runtime. For that sort of thing, a function really
would be best.

If you want to make it faster when the argument is known at compile
time, I suggest defining it as a function and looking into compiler
macros. This lets you get the best of functions and macros. Of course,
the usual caveat about optimization applies: don't do it until you know
you need it and you know where you need it.

-Peter
From: ··············@hotmail.com
Subject: Re: how can I implement this in macro?
Date: 
Message-ID: <1112060098.606574.86570@z14g2000cwz.googlegroups.com>
Do you clearly understand how the error message is arising? That would
be a first step toward understanding the issue.

As a hint, you want to postpone looking up the function if you don't
know what it is at macro-expansion time. The macro can take the
shortcut only if it knows the right substitute to place into the
expansion.