Code: Select all
(setf (cdr (last list)) (cons 'new nil))
Code: Select all
(setf (cdr (last list)) (cons 'new nil))
yes, but see belowforegam wrote:does last require O(n) time
It extends the old one, but you could think of it as creating a new list as well as long as you refrain from mutating your lists (using setf on them). The thing to realize is that it while it reuses the memory of the old list, that doesn't affect any other references to that old list. Take for instance:foregam wrote:does push create a new list or extend the old one
Code: Select all
(let ((lst1 '(1 2 3 4)))
(let ((lst2 lst1))
(push 0 lst2)
lst2 ))
=> (0 1 2 3 4) ; As expected
(let ((lst1 '(1 2 3 4)))
(let ((lst2 lst1))
(push 0 lst2)
lst1 ))
=> (1 2 3 4) ; Original list left unchanged
In these cases, they are usually thought of as part of the standard, although I am not sure, actually. The structure of a cons cell is a well defined thing, however I believe that as long as your implementation of cons, car, and cdr and friends all behave as specified your Lisp would be ANSI compliant.foregam wrote:are these details implementation specific or part of the standard