Cap*_*liC 1 lisp equality common-lisp quote
我不明白为什么
(setq a_sym 'abc)
(print (eq a_sym 'abc))
(print (eq 'x 'x))
(print (eq (first '('x 2 3)) 'x))
Run Code Online (Sandbox Code Playgroud)
版画
T
T
NIL
Run Code Online (Sandbox Code Playgroud)
为什么'x第三个语句中的符号与第二个语句的处理方式不同?并且,脚踏实地,如何比较它们的身份?
如果您trace正在比较,您将立即看到您的错误:
[1]> (eq (first '('x 2 3)) 'x)
NIL
[2]> (trace eq)
** - Continuable Error
TRACE(EQ): #<PACKAGE COMMON-LISP> is locked
If you continue (by typing 'continue'): Ignore the lock and proceed
The following restarts are also available:
ABORT :R1 Abort main loop
Break 1 [3]> :c
WARNING: TRACE: redefining function EQ in top-level, was defined in C
;; Tracing function EQ.
(EQ)
[4]> (eq (first '('x 2 3)) 'x)
1. Trace: (EQ ''X 'X) ; <======= note 1
1. Trace: EQ ==> NIL
NIL
[5]> (eq (first '(x 2 3)) 'x)
1. Trace: (EQ 'X 'X) ; <======= note 2
1. Trace: EQ ==> T
T
Run Code Online (Sandbox Code Playgroud)
IOW,你是"过度"你的x:当你输入时'x,它是一样的(quote x),你正在检查符号x和列表的相等性(quote x),当然,还有nil.
(eq ''x 'x):因为eq是一个函数,它的参数进行评估,我们比较'x == (quote x)有x和获取nil.
(eq 'x 'x):出于同样的原因,我们是在比较x与x和获取t.