This question already has an answer here:
I have a list of seven integers, initially all 0s, let's call it "data." Periodically during the course of running my program I want to increment the value of one of those integers by one. At the end of the program I print data. All is fine, except that on each successive run of the program all the values of data from the last run are added to all the values of data from this run. I want only the values of data from this run. This unexpected behavior occurs whether data is a local variable within a class's method, a local variable within a separate function called by a class's method, or a slot of a class. It happens whether I increment the individual values of data by incf or (setf value (1+ value)). When I reload the program, data resets to all zeroes but when I run the program again data again adds all of the last run's data to this run's data. When I increment one of the values of data I use the function nth with index being the value of another object's slot. What could cause this unwelcome persistence of values of my "data" list?
Are you doing something like this:
Quoted data is literal data; there's only one copy of it, and the consequences of modifying it are undefined. The behavior above is common, but you can't depend on it. Some compilers will give a warning when you do this. E.g., in SBCL:
The relevant text from the HyperSpec on
quote
is:Create modifiable lists with, e.g.,
(list 1)
, not'(1)
. This is a common pitfall until you've encountered it. There are some other questions on StackOverflow that mention this issue. A very specific one isbut there are a bunch, too:
The same thing happens in Scheme, though the citations to the documentation are obviously different. For R5RS, the documentation is the following:
There are questions about this as well: