From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Stephen Berman Newsgroups: gmane.emacs.help Subject: Re: using setq to create lists based on other lists... Date: Sun, 02 Dec 2018 12:51:30 +0100 Message-ID: <874lbw4059.fsf@gmx.net> References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: blaine.gmane.org 1543751413 5089 195.159.176.226 (2 Dec 2018 11:50:13 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 2 Dec 2018 11:50:13 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) 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:50:09 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 1gTQGG-0001CK-9d for geh-help-gnu-emacs@m.gmane.org; Sun, 02 Dec 2018 12:50:08 +0100 Original-Received: from localhost ([::1]:44221 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gTQIM-0001fE-Pb for geh-help-gnu-emacs@m.gmane.org; Sun, 02 Dec 2018 06:52:18 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:53706) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gTQHt-0001ew-Ss for help-gnu-emacs@gnu.org; Sun, 02 Dec 2018 06:51:50 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gTQHm-0007uW-62 for help-gnu-emacs@gnu.org; Sun, 02 Dec 2018 06:51:49 -0500 Original-Received: from mout.gmx.net ([212.227.15.19]:48325) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gTQHl-0007rF-Q1 for help-gnu-emacs@gnu.org; Sun, 02 Dec 2018 06:51:42 -0500 Original-Received: from rosalinde ([188.109.205.60]) by mail.gmx.com (mrgmx002 [212.227.17.190]) with ESMTPSA (Nemesis) id 0MHX4u-1gWj8h1Kyy-003M6x for ; Sun, 02 Dec 2018 12:51:31 +0100 In-Reply-To: (Barry Margolin's message of "Sun, 02 Dec 2018 06:21:12 -0500") X-Provags-ID: V03:K1:jqMucSqTfq5El3EHEq33r5mzetGhFBxDKetRTau9KrZcz/Qn4/m a+zUsjzlPooMibkf4rGKUtj3P4/93iA0R6Bl7NmgRIATTYdiITTVCKLEbAq6+vAF6V5vO9h DF+P3Xzv9GlUfz4gQ9KehtrlXKEOox3ffbitYdQ4ddP5DPQx4wMkfzr+nSzDXch6cMaYJ8j cJDh1qixPJhgMqXPGEPgQ== X-UI-Out-Filterresults: notjunk:1;V03:K0:m/HoyKeb0R4=:He3hXTOP9cRTOao6FSxHtW aX9C3seS++5TJmpMvl9qkAG4mhu6D32hDCHNMLCoe7Mv5EVSOLvISwzYWNxZVpudhOjY8cbMO BXg8g+5vm1RXMnMK3lGHvNoWS92s6mhay1/GW6cxC2iONCEZOuVVJV3B4UkK0jQxfj3+W26eG +mSmpjs/8RPu9lK9MZygYee8kk1vd+JDzBGsxh1i+O/OnRLX4GzVk/xym4lp0TohGkoFxdaTS 99mtVn2sCtTC4BmSsf/XHNkq+19raDvymO1tWcXFbjzAQLZ7IvrgfAxFmzISpusMVtDc1OauB C6PUx2GPmIzp5lokALxmAptXhUeffwkETPmfMrNi0fWRxR/EApdD/lTNteHKHiea238rmB2Kq 3eCg8uYtcug3Zp05VaCnDwNhDr2jKx+921lLEiZbmSA1+1LBYxbkHw+DdGlnhuxTxQ9i15y5M GzHsy31uKvNiJECA9IlPCpqV79lMWE13dox5XfxKY2ZtQp+pXUpkBrCLpTDucShU93GZwFH58 +D1vtoomlirAPe1vCkCLZVfPk5OjmMarHe7EiG5qjPk8mMqxDjFRTtBT2Nfyxfqgfrop3rF6g 87g9VoUamnqx0eL9jSbD8MR3BisHtDL75vpXDdVnMU1IZzCY3JvIn5wWn4/alvwk2c5X0v6W1 jIIWzirMby3oYSsCfF27Hgrr/al6bda62FtngY/NVtkw8TjX1aKcKCxt+RBbOOOF8JjVSlWld ee/gxdCy1VF+mkrWB2lpJP3++bqJArr/QGAZM+hu1dTm2CkFtJ8Pm8bSQfO+RexQ5Xj9Y3cK X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 212.227.15.19 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:118821 Archived-At: On Sun, 02 Dec 2018 06:21:12 -0500 Barry Margolin wro= te: > In article , > Jean-Christophe Helary wrote: > >> I spend most of the day investigating why creating a list with setq was = not=20 >> "working". >>=20 >> For ex: >> (setq list0 '(1 2)) >> (setq list1 list0) >>=20 >> If you do >>=20 >> (setcar list0 0) >>=20 >> then for some reason (for which I could not find an explanation in the e= lisp=20 >> reference) the car of list1 also changes, and vice-versa. >>=20 >> Which is totally unexpected since when you do: >>=20 >> (setq list0 0) >>=20 >> list1 does not become 0 >>=20 >> I don't suppose that's a bug, but really it ought the be very clearly=20 >> 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=20 > setcar, you're changing the contents of one of the cells in that cons.=20 > Since both variables refer to it, the change is visible through either=20 > of them. [...] > But reassigning the variable doesn't affect the others, because now=20 > they're not referring to the same object. To expand of this, since Jean-Christophe didn't find an explanation of this behavior of setq in the Lisp reference, but it is in fact documented: Special Form: setq [symbol form]... This special form is the most common method of changing a variable=E2=80=99s value. Each SYMBOL is given a new value, which is = the result of evaluating the corresponding FORM. The current binding of the symbol is changed. In the above case, the symbol `list1' is given the result of evaluating `list0', which is the list `'(1 2)'. So now both `list0' and `list1' refer to this list, which is a Lisp object; you can see this with `eq', which returns t if its arguments are the same Lisp object: (eq list1 list0) =3D> t That's why setcar affects both `list0' and `list1'. On the other hand (setq list0 0) changes the current binding of `list0' to the value of `0', which is a different object from `'(1 2)', which is still the value of `list1', so now `list0' and `list1' differ. Steve Berman