看代码:
(defmacro while (test &rest body)
`(do ()
((not ,test))
,@body))
(setq x 0)
(trace while)
(while (< x 10)
(princ x)
(incf x))
输出为:
;; Loading file lisp.lisp ...
WARNING: TRACE: redefining function WHILE in D:\test\lisp.lisp, was defined in
top-level
;; Tracing macro WHILE.
1. Trace: (WHILE (< X 10) (PRINC X) (INCF X))
1. Trace: WHILE ==> (DO NIL ((NOT (< X 10))) (PRINC X) (INCF X))
0123456789
;; Loaded file lisp.lisp
利用 (trace while)可以把while的宏展开,从上面展开可以看出 (< x 10)替换了test, (princ x) (incr x)替换了body.