I am trying to see the effect of the Lisp push function on sublists.
I have a Common Lisp code segment as follows:
(defvar l2)
(defvar tl2)
(setq l2 '(1 2 3))
(setq tl2 (cdr l2))
(push 5 tl2)
(print tl2)
(print l2)
I expect the last print to print the list (1 5 2 3). However, it only prints (1 2 3). If tl2 is the sublist (2 3) of the original list and push is in-place, why does the original list not become (1 2 5 3)?
Lisp lists are singly linked cons cells with contents and NIL at the end.
If you cons something to the start of a list, it creates a new cons cell, which cdr/rest points to that other list. It does not change any other cons cell.
l2 -> (a . (b . (c . NIL)))
tl2 ^
(push 'z tl2)
tl2 -> (z . ^)
(print tl2) -> (z a b c)
(print l2) -> (a b c)