From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Barry Margolin Newsgroups: gmane.emacs.help Subject: Re: using setq to create lists based on other lists... Date: Sun, 02 Dec 2018 06:21:12 -0500 Organization: A noiseless patient Spider Message-ID: References: NNTP-Posting-Host: blaine.gmane.org X-Trace: blaine.gmane.org 1543749808 2123 195.159.176.226 (2 Dec 2018 11:23:28 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 2 Dec 2018 11:23:28 +0000 (UTC) User-Agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Dec 02 12:23:24 2018 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gTPqO-0000RT-Kb for geh-help-gnu-emacs@m.gmane.org; Sun, 02 Dec 2018 12:23:24 +0100 Original-Received: from localhost ([::1]:44129 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gTPsU-0006eW-VU for geh-help-gnu-emacs@m.gmane.org; Sun, 02 Dec 2018 06:25:34 -0500 Original-Path: usenet.stanford.edu!goblin3!goblin.stu.neva.ru!news.neodome.net!news.uzoreto.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!barmar.motzarella.org!.POSTED!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 63 Original-Injection-Info: barmar.motzarella.org; posting-host="87ac160df7e43f5f73f34952e352f544"; logging-data="26187"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18fhsTFg7aMQ4zaVqYRggaM" Cancel-Lock: sha1:rCSoGJJBetHcEk5g5VMgpMIiVYA= Original-Xref: usenet.stanford.edu gnu.emacs.help:224691 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:118820 Archived-At: In article , Jean-Christophe Helary 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 ***