1

I don't get why

(setq a_sym 'abc)
(print (eq a_sym 'abc))

(print (eq 'x 'x))
(print (eq (first '('x 2 3)) 'x))

prints

T 
T 
NIL 

Why the symbol 'x in the third statement is handled differently than second ? And, down to earth, how to compare them for identity ?

sds
  • 58,617
  • 29
  • 161
  • 278
CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • Your use of `print` indicates that you use "batch" - you might want to use the REPL instead. See http://stackoverflow.com/q/15708911/850781 – sds Dec 05 '16 at 21:07

2 Answers2

5

If you trace what you are comparing, you will see your mistake right away:

[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

IOW, you are "overquoting" your x: when you type 'x, it's the same as (quote x) so, you are checking for equality of symbol x and list (quote x) and, of course, getting nil.

Notes:

  1. (eq ''x 'x): since eq is a function, its arguments are evaluated and we are comparing 'x == (quote x) with x and getting nil.

  2. (eq 'x 'x): for the same reason, we are comparing x with x and getting t.

Related:

  1. Lisp quote work internally
  2. Confused by Lisp Quoting
  3. When to use 'quote in Lisp
  4. Lisp: quoting a list of symbols' values
Community
  • 1
  • 1
sds
  • 58,617
  • 29
  • 161
  • 278
  • you are welcome! as an exercise, you might want to `(trace first)` and see what happens. – sds Dec 05 '16 at 21:10
  • Well, I tried `(step ...)`. The level of details and lacking of experience was stressing me... when I hit :n, for instance, I'm unable to predict the debugger answer. I'll try `(trace ...)`, thanks again – CapelliC Dec 05 '16 at 21:21
  • lisp takes time to get used to, but its power and beauty are unsurpassable! :-) – sds Dec 05 '16 at 21:25
2

Syntax and reading

You wrote:

symbol 'x

Note that 'x is not a symbol. It's a quote character in front of a symbol. The quote character has a special meaning in s-expressions: read the next item and enclose it in (quote ...).

Thus 'x is really the list (quote x).

CL-USER 9 > (read-from-string "'x")
(QUOTE X)
2

Evaluation

A quoted object is not evaluated. The quote is a special operator, which means it is built-in syntax/semantics in Common Lisp and not a function and not a macro. The evaluator returns the quoted object as such:

CL-USER 10 > (quote x)
X

Your example

(eq (first (quote ((quote x) 2 3))) (quote x))

Let's evaluate the first part:

CL-USER 13 > (first (quote ((quote x) 2 3)))
(QUOTE X)

The result is the list (quote x).

Let's evaluate the second part:

CL-USER 14 > 'x
X

So the result is the symbol x.

x and (quote x) are not eq.

Evaluation and Quoting

'('x 2 3)

What would be the purpose of the second quote in the list?

The first quote already means that the WHOLE following data structure is not to be evaluated. Thus it is not necessary to quote a symbol inside to prevent its evaluation. If a list is quoted, none of its sublists or subelements are evaluated.

Summary

The quote is not part of the symbol. It is a built-in special operator used to prevent evaluation.

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346