Question

In Petite Chez Scheme (threaded) . I defined two lists named myq and myqq.

(define make-queue 
  (lambda () 
    (let ((end (cons 'ignored '()))) 
      (cons end end)))) 

(define myqq (make-queue)) 
(define myq '((ignored) ignored)) 

;this shows myq and myqq are equal
(display (equal? myqq myq)) 
(newline) 

;test myqq
(display myqq) 
(newline) 
(set-car! (cdr myqq) 'b) 
(display myqq) 
(newline) 

;test myq
(display myq) 
(newline) 
(set-car! (cdr myq) 'b) 
(display myq) 
(newline) 

this is the result:

#t 
((ignored) ignored) 
((b) b) 
((ignored) ignored) 
((ignored) b) 

My question is: as the

(display (equal? myqq myq)) 

shows myq and myqq are equal. Why do the same commands:

(set-car! (cdr myqq) 'b) 
(set-car! (cdr myq) 'b) 

lead to a different result? Plus, I don't know why (set-car! (cdr myqq) 'b) results in ((b) b).I think it should result in ((ignored) b), because we never change the car of myqq!

Was it helpful?

Solution 2

The result of make-queue is a structure consisting of two cons cells, not three:

  • One cons cell contains ignored and () in car and cdr respectively.
  • The other cons cell contains a link to the first cons cell in both its car and its cdr.

When you do (set-car! (cdr myqq) 'b), you're changing the car of the first cons cell (because this is what is referenced in (cdr myqq)) from ignored to b.

equal? recursively checks pairs and similar structures, ultimately applying eqv? to the primitive values (in your case symbols). It doesn't count the fact that there is structural sharing between different components in myqq, and this isn't the case for myq, as a relevant difference.

equal? isn't the final word on equality, it's just one particular (and often useful) sense in which two structures can be equal. You can use other comparison functions to see that there's a difference between the two structures, e.g.:

>>> (eq? (car myqq) (cdr myqq))
#t 
>>> (eq? (car myq) (cdr myq))
#f 

OTHER TIPS

Equal? does not mean identical. Equal basicly asks are these two things values? If so are they the same? If they are lists of values, does each element of these list have the same value as to corresponding list.

 (define example 'ignored) 
 (equal? 'ignored example) -> #t  
 (set! example 'b) 
 (equal? 'ignored example) -> #f 

That makes sense, right?

It has to do with the fact that cons cells are a pair of pointers.

When you define myqq you bind the name end to the value 'ignored, Which internally is a symbol, associated with a pointer pointing to some memory location with the value 'ignored. When returning the value of myqq, it's a name pointing to a pair, this pair is is two pointer, both pointing to the same place that the pointer of end was pointing to. set-cdr! changes the value of the memory at the location pointed to by the cdr pointer. In the case of myqq this was also to location that the car was pointing to so you change both. In myq the car and cdr are each pointing to their own memory location loaded with the value 'ignored.

As pointed out by svk eq? will test equality by comparing pointers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top