2012-08-01 40 views
2

有沒有一種情況,gensym的在前綴上附加唯一數字的功能很方便?我不明白爲什麼有gensym什麼時候比Lisp更喜歡make符號

(let ((str "batman")) 
    (eq (make-symbol str) 
     (make-symbol str))) 

總是返回nil

回答

5

GENSYM例如使調試生成的代碼稍微容易一些。

示例:

請參閱LOOP宏的擴展。您可以通過查看其名稱來查看哪些符號相同,即使它們未被封裝在包中。有兩個未中斷的臨時變量。不同的名字現在使用清楚。

CL-USER 4 > (pprint (macroexpand '(loop for i in '(1 2 3) sum i))) 

(BLOCK NIL 
    (MACROLET ((LOOP-FINISH() '(GO #:|end-loop-1103|))) 
    (LET ((I NIL) (#:|tail-1106| '(1 2 3)) (#:|by-1107| 'SYSTEM:CDR$CONS)) 
     (LET ((#:|accumulator-1104| 0)) 
     (DECLARE (TYPE NUMBER #:|accumulator-1104|)) 
     (TAGBODY 
     #:|begin-loop-1102| NIL 
       (PROGN 
        (WHEN (OR (ENDP #:|tail-1106|)) (GO #:|end-loop-1103|)) 
        (LET ((#:|temp-1109| (FUNCALL #:|by-1107| #:|tail-1106|)) 
         (#:|temp-1108| (SYSTEM:CAR$CONS #:|tail-1106|))) 
        (SETQ I #:|temp-1108|) 
        (SETQ #:|tail-1106| #:|temp-1109|))) 
       (INCF #:|accumulator-1104| I) 
       (GO #:|begin-loop-1102|) 
     #:|end-loop-1103| (RETURN-FROM NIL #:|accumulator-1104|)))))) 
相關問題