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

RE: macros vs. blocks




   Date: Mon, 18 Nov 2002 10:56:38 -0800
   From: "Todd Proebsting" <toddpro@microsoft.com>
   
   SWAP can be trivially written with reference parameters.  I don't know
   SETF and ROTATEF.  Are there good examples of macros that cannot be
   written with closures and reference parameters?

Sure.  How about this TRACE macro, which takes
an integer-valued expression and prints both the
expression and the value of the expression?  

In Lisp:

  (defmacro trace (x) `(format t "~S = ~S~%" ',x ,x))

 For program:

  (defun main ()
    (let ((x 1))
      (trace (+ x 3))))

 the output is:

  (+ X 3) = 4

In C:

  #define TRACE(x) printf("\n%s = %d", #x, x)

 For program:

  main() {
   int x = 1;
   TRACE(x+3);
  }

 the output is:
  
  x+3 = 4

The point here is that the programmer need write an expression
only once and it gets used twice, once as an expression per se
to be printed and once as a program fragment to be evaluated.

--Guy