cl-user> (let ((a '(1 2 3)))
(let ((b (car (cdr a))))
(setf b 4))
a)
;Compiler warnings :
; In an anonymous lambda form: Unused lexical variable B
(1 2 3)
A cons
cell is a pair of pointers. car
dereferences the first, and cdr
dereferences the second. Your list is effectively
a -> [ | ] -> [ | ] -> [ | ] -> NIL
| | |
1 2 3
Up top where you're defining b
, (cdr a)
gets you that second arrow. Taking the car
of that dereferences the first pointer of that second cell and hands you its value. In this case, 2
. If you want to change the value of that pointer, you need to setf
it rather than its value.
cl-user> (let ((a '(1 2 3)))
(let ((b (cdr a)))
(setf (car b) 4))
a)
(1 4 3)