[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug in copy-list implementation



I've been experimenting with the commands copy-list and copy-tree in a program
I've been writing. I experienced a bug in that when using copy-list to make
a copy of list that I was going to mutate, the program would mutate the 
original list too, but when I switched to copy-tree, the problem went away.

(defun mutate (program)
  (let ((prog-to-mutate (copy-list program)))
     (mutate-loop prog-to-mutate)))

where mutate-loop is a function that destructively modifies its argument.
when it returned, it had also modified program, not just prog-to-mutate. 

However, when I switched to copy-tree, the problem was solved.

I did run some tests on this, and got a few inconsistencies with this argument
though. I tried doing the above, but before mutate-loop I put in
(setf prog-to-mutate '(a b c))
(pprint prog-to-mutate)
(pprint program)

and this printed 
'(a b c)
and then the original program, respectively.

However, I put a pprint after the mutate-loop line, and then pprinted 
prog-to-mutate and then program
and they both printed
'(a b c)
'(a b c)

(mutate-loop in this particular case just mutates the argument to be itself.)

There is something wierd going on here.

Andrew Begel
MIT Media Lab
Epistemology and Learning
abegel@media.mit.edu