(define l '(a))
(define p (cons l l))
(set-car! (cdr p) 'b)
После последнего (set-car! (cdr p) 'b)
p
будет ((b) b)
, а не ((a) b)
. Почему?
(define l '(a))
(define p (cons l l))
(set-car! (cdr p) 'b)
После последнего (set-car! (cdr p) 'b)
p
будет ((b) b)
, а не ((a) b)
. Почему?
Ячейка cons
содержит два указателя на два других объекта. Ваш рассматриваемый код будет примерно переведен на этот псевдо-C:
struct cons {
void *car;
void *cdr;
};
cons *l = &cons{"a", NULL};
cons *p = &cons{l, l}; // no copy takes place, we're handling pointers here
p->cdr->car = "b"; // this changes the "l" object, and nothing else