I want to write a function in Common Lisp, which destructively modifies its argument. In C or Rust I would use a pointer/reference to an object, which can be dereferenced inside the body of the function. In CL I write:
(defun foo (lst)
(setf lst NIL))
But after evaluating this form:
(let ((obj (list "a" "b")))
(foo obj)
obj) => ("a" "b")
one sees that the function foo
has no effect. I can explain that by pass by value semantic, where we modify a local copy of the argument pushed onto the function stack.
If we define another function:
(defun bar (lst)
(setf (car lst) NIL))
and evaluate a similar form
(let ((obj (list "a" "b")))
(bar obj)
obj) => (NIL "b")
we will clearly see that the lst
was modified as if we would use the pass by reference semantic. So, (setf lst NIL)
did not worked but (setf (car lst) NIL)
did. Could you please explain why?