Lecture Notes: Macros(三)

2014-11-24 13:28:29 · 作者: · 浏览: 89
-Sum2 (X Y) (let ((First (gensym FIRST-)) (Second (gensym SECOND-)) (Sum (gensym SUM-))) '(let* ((,First ,X) (,Second ,Y) (,Sum (+ ,First ,Second))) (* ,Sum ,Sum)) )) Now

(macroexpand-1 '(Square-Sum2 1 First))
==> (LET* ((#:FIRST-590 1)
          (#:SECOND-591 FIRST)
          (#:SUM-592 (+ #:FIRST-590 #:SECOND-591)))
     (* #:SUM-592 #:SUM-592))

This expansion has no dependence on any local variable names in the macro definition itself, and since the generated ones are guaranteed to be unique, is safe from name collisions.