(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
セルには、基本的に、他の 2 つのオブジェクトへの 2 つのポインターが含まれています。問題のコードは、大まかに次の疑似 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