I get strange behavior when calling a destructive definition that takes as argument an local variable whose type is a list created with a quote .
Destructive function:
(defun insert-at-pos (pos list elem) (if (= pos 0) (cons elem list) (let ((aux-list (nthcdr (1- pos) list))) (setf (rest aux-list) (cons elem (rest aux-list))) list)))
WRONG: A local variable is a list created by a special quote statement.
(defun test () (let ((l '(1 2 3))) (print l) (insert-at-pos 2 l 4) (print l))) > (test) (1 2 3) (1 2 4 3) (1 2 4 3) > (test) (1 2 4 3) (1 2 4 4 3) (1 2 4 4 3) > (test) (1 2 4 4 3) (1 2 4 4 4 3) (1 2 4 4 4 3)
CORRECT: A local variable is a list created using the list function.
(defun test2 () (let ((l (list 1 2 3))) (print l) (insert-at-pos 2 l 4) (print l)))
or
(defun test2 () (let ((l '(1 2 3))) (print l) (setf l (cons (first l) (cons (second l) (cons 4 (nthcdr 2 l))))) (print l))) > (test2) (1 2 3) (1 2 4 3) (1 2 4 3) > (test2) (1 2 3) (1 2 4 3) (1 2 4 3) > (test2) (1 2 3) (1 2 4 3) (1 2 4 3)
Does anyone know the reason for this strange behavior?
list lisp common-lisp quote
Paulo tomΓ©
source share