Can you explain why the first one is false and the second one is true?
And how this works? Thanks.
(eq? '(1 2 3) '(1 2 3)) ;False
(eq? '() '()) ;True
Can you explain why the first one is false and the second one is true?
And how this works? Thanks.
(eq? '(1 2 3) '(1 2 3)) ;False
(eq? '() '()) ;True
There's only one empty list, so all uses of ()
refer to that list, and it's eq?
to itself. The Scheme Specification description of the storage model says:
Notwithstanding this, it is understood that the empty list cannot be newly allocated, because it is a unique object.
and the specification of eqv?
(which is referenced by the eq?
description) says that two objects are equivalent if
obj1 and obj2 are both the empty list
But when you create a non-empty list, it creates a fresh one every time, and they're not eq?
to each other even if they contain the same elements.
Quoted from TSPL3:
[..] Two objects are considered identical if they are represented internally by the same pointer value [..] The empty list () is identical to itself wherever it appears. [..] Two pairs, vectors, or strings created by different applications of cons, vector, string, etc., are distinct.
If you write instead
(let ((x '(1 2 3)))
(eq? x x))
it will be #t
.