Okay i noticed this semi weridish behavior in one of my projects using scheme, and lists. I managed to isolate the behavior, into a single section. The code is:
(define x (list 1 2 3))
(define y (list 4 5))
(define z (cons (car x) (cdr y)))
(define w (append y z))
(define v (cons (cdr x) (cdr y)))
(set-car! x 6)
(set-car! y 7)
(set-cdr! (cdr x) (list 8))
x
y
z
w
v
Gives us the output of:
(6 2 8)
(7 5)
(1 5)
(4 5 1 5)
((2 8) 5)
Can anyone explain to me:
- Why does (set-car! x 6) not update Z? Since according to my understanding car/cdr return pointers or references to the corresponding values. This is really werid and im kinda confused.
- If car/cdr does not return references/pointers then how is the final set-cdr! manipulating the list v?
Any ideas? Its a simple fix, but im more curious as to why the weridness with the variables is going on.