schemer5rs

Scheme and R5RS questions about eq


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

Solution

  • 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.