[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: using setq to create lists based on other lists...
From: |
Stephen Berman |
Subject: |
Re: using setq to create lists based on other lists... |
Date: |
Sun, 02 Dec 2018 12:51:30 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) |
On Sun, 02 Dec 2018 06:21:12 -0500 Barry Margolin <barmar@alum.mit.edu> wrote:
> 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.
[...]
> But reassigning the variable doesn't affect the others, because now
> 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’s 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)
=>
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
- Re: using setq to create lists based on other lists..., (continued)
- Re: using setq to create lists based on other lists..., Stefan Monnier, 2018/12/02
- Re: using setq to create lists based on other lists..., Jean-Christophe Helary, 2018/12/02
- Re: using setq to create lists based on other lists..., Stefan Monnier, 2018/12/02
- Re: using setq to create lists based on other lists..., Jean-Christophe Helary, 2018/12/02
- Re: using setq to create lists based on other lists..., Stefan Monnier, 2018/12/02
- Re: using setq to create lists based on other lists..., Jean-Christophe Helary, 2018/12/02
- Re: using setq to create lists based on other lists..., Robert Thorpe, 2018/12/02
- Re: using setq to create lists based on other lists..., Jean-Christophe Helary, 2018/12/02
Message not available
Re: using setq to create lists based on other lists..., Barry Margolin, 2018/12/02
- Re: using setq to create lists based on other lists...,
Stephen Berman <=
- Re: using setq to create lists based on other lists..., Jean-Christophe Helary, 2018/12/02
- Re: using setq to create lists based on other lists..., Stephen Berman, 2018/12/02
- Re: using setq to create lists based on other lists..., Jean-Christophe Helary, 2018/12/02
- Re: using setq to create lists based on other lists..., Michael Heerdegen, 2018/12/02
- Re: using setq to create lists based on other lists..., Jean-Christophe Helary, 2018/12/02
- Re: using setq to create lists based on other lists..., Michael Heerdegen, 2018/12/02
- Re: using setq to create lists based on other lists..., Jean-Christophe Helary, 2018/12/02
Re: using setq to create lists based on other lists..., Stephen Berman, 2018/12/02
Re: using setq to create lists based on other lists..., Jean-Christophe Helary, 2018/12/02
Message not availableRe: using setq to create lists based on other lists..., Barry Margolin, 2018/12/04