help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: using setq to create lists based on other lists...


From: Barry Margolin
Subject: Re: using setq to create lists based on other lists...
Date: Sun, 02 Dec 2018 06:21:12 -0500
User-agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X)

In article <mailman.5010.1543748027.1284.help-gnu-emacs@gnu.org>,
 Jean-Christophe Helary <brandelune@gmail.com> wrote:

> I spend most of the day investigating why creating a list with setq was not 
> "working".
> 
> For ex:
> (setq list0 '(1 2))
> (setq list1 list0)
> 
> If you do
> 
> (setcar list0 0)
> 
> then for some reason (for which I could not find an explanation in the elisp 
> reference) the car of list1 also changes, and vice-versa.
> 
> Which is totally unexpected since when you do:
> 
> (setq list0 0)
> 
> list1 does not become 0
> 
> I don't suppose that's a bug, but really it ought the be very clearly 
> documented in the reference. Also, I'd like to know why that's happening.

list0 and list1 both contain references to the same cons. When you use 
setcar, you're changing the contents of one of the cells in that cons. 
Since both variables refer to it, the change is visible through either 
of them.

The same thing happens in just about all object-oriented languages. 
Multiple variables can refer to the same object; if you change the 
contents of the object, all of them reflect the change.

But reassigning the variable doesn't affect the others, because now 
they're not referring to the same object.

> 
> So, to avoid that behavior, I had to resort to using copy-tree:
> 
> (setq list1 (copy-tree list0))
> 
> Which is really not the first thing I would have thought about.

copy-tree makes a new set of conses, so modifying one of them has no 
effect on the other.

If you're familiar with C, you can think of Lisp variables as being like 
pointers, and conses are like structures. Your code is equivalent to:

cons *list0 = malloc(sizeof(cons));
list0->car = make_number(1);
list0->cdr = malloc(sizeof(cons));
list0->cdr->car = make_number(2);
list0->cdr->cdr = null;
cons *list1 = list0;
list0->car = make_number(0);

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***


reply via email to

[Prev in Thread] Current Thread [Next in Thread]