[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Optimizing rebindings in macros



As REM@SUWATSON.stanford.edu has mentionned to me, my idea (not rebinding
atomic arguments) does not work with more than one argument, because it may
change evaluation order. For instance:

(defmacro test(x y)
  (let ((xx (gensym))(yy (gensym)))
    `(let ((,xx ,x)(,yy ,y))
       (list ,xx ,yy ,xx ,yy))))

(setq u 1)
(test u (setq u 2))
-> (1 2 1 2)

Now if I optimize the binding xx:

(defmacro opt-test(x y)
  (if (consp x)
    (let ((xx (gensym))(yy (gensym)))
      `(let ((,xx ,x)(,yy ,y))
         (list ,xx ,yy ,xx ,yy)))
    (let ((yy (gensym)))
      `(let ((,yy ,y))
         (list ,x ,yy ,x ,yy)))))

(setq u 1)
(opt-test u (setq u 2))
-> (2 2 2 2)

This was my contribution to the illustration of the danger of macros :-)
     Daniel Ranson.
     ranson@lannion.cnet.fr