I have created a function to randomly select a 'bit' out of a nested list, e.g., ((1 0 0 1) (1 1 1 1) (0 1 0 1))
, and then flip it. If it is a one, make it a zero and vice versa. The function works well, but I have discovered that it mutates the original argument despite my making a copy. Here is the function, the two writes at the end demonstrate this problem. If I pass ((1 1 1 1))
to this, I expect to see this original value printed by (write DNA-seq)
but instead the original is modified, so (write DNA-seq)
and (write CDNA-seq)
print the same thing.
(defun rand-mutate (DNA-seq)
(let ((CDNA-seq (copy-list DNA-seq)))
(let ((gene (random-range 0 (length CDNA-seq))))
(let ((base (random-range 0 (length (nth gene CDNA-seq)))))
(cond ((= (nth base (nth gene CDNA-seq)) 0) (setf (nth base (nth gene CDNA-seq)) 1))
(t (setf (nth base (nth gene CDNA-seq)) 0))) (write DNA-seq)(write CDNA-seq)))))