Search code examples
lispschemesymbolsracketequivalence

Scheme symbolic equivalence


The platform i'm working with is DrScheme.

I've seen that a pair (a b) [constructed by (cons a b)] is implemented within the language like a procedure that looks like this:

(define (cons a b)
  (lambda(pick)
    (cond ((= pick 1) a)
          ((= pick 2) b))))

and the selectors:

(define (car x) (x 1))
(define (cdr x) (x 2))

Then there are lists, constructed with expression like (cons a (cons b (cons c (cons ...)))).

Now, what i was trying to understand is this (typed on DrScheme's prompt):

> (define l1 '(a b c))
> (define l2 (list 'a 'b 'c))
> l1
(a b c)
> l2
(a b c)
> (eq? l1 l2)
#f

Ok, l2 is just a list (that is, a procedure, ect...) like i've described abode, but... what is l1? A symbol? A sequence of character? And whatever it is, how is it implemented within the language? Thanks!


Solution

  • l1 is also just a list containing the same elements. Note that this also returns #f:

    (define l1 '(a b c))
    (define l2 '(a b c))
    (eq? l1 l2)
    

    While this returns #t:

    (define l1 '(a b c))
    (define l2 (list 'a 'b 'c))
    (equal? l1 l2)
    

    The reason is that eq? checks whether l1 and l2 are references to the same object in memory, while equal? checks whether they have the same contents.