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: Stephen Berman
Subject: Re: using setq to create lists based on other lists...
Date: Sun, 02 Dec 2018 16:00:09 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

On Sun, 2 Dec 2018 22:28:39 +0900 Jean-Christophe Helary <brandelune@gmail.com> 
wrote:

>> On Dec 2, 2018, at 22:08, Stephen Berman <stephen.berman@gmx.net> wrote:
[...]
>> 
>> I don't think setq is behaving any different with lists than with other
>> Lisp objects.  Lists are defined this way in Lisp, cf. (info "(elisp)
>> Cons Cell Type"):
>
> Yes, but a list does not generally evaluate to a pointer to the first cell of
> its cons. Which is the case when setq is used. Which is the reason why the
> Introduction insists on that aspect of setq, because before that, all the setq
> examples assigned "straight" values to variables.
>
> For ex. The first sentences of "Global Variables" in the Reference are like 
> this:
>
>> You specify a value for a symbol with setq. For example, (setq x '(a b))
>> gives the variable x the value (a b).
>
> They don't say:
>
> (setq x '(a b)) stores the address of the first (cons) cell in the variable. 
>
> Which it does, and which is confusing if you expect x to hold '(a b), like I
> was today.
>
> So I think it would be nice to add a few lines here and there to clarify that
> behavior.

I think your confusion may be due to not distinguishing the value
assigned by setq, which is a particular object, and the form of the
value, which may look the same but nevertheless be a different object.
Look again at your original example:

(setq list0 '(1 2))
(setq list1 list0)

I pointed out that the second setq sets the value of `list1' to the
value of `list0', so they refer to the same object:

(eq list1 list0)
=>
t

But if you now do this:

(setq list1 '(1 2))

you have set the value of `list1' to a new list, which happens to have
the same structure as the value of `list0' but is not the same object:

(eq list1 list0)
=>
nil

And now (setcar list0 3) turns the value of `list0' into (3 2) but the
value of `list1' is still (1 2).

You see the same behavior e.g. with strings (which are implemented in C
as arrays, i.e. a variable whose value is a string points to the address
of the first element of the array holding the characters of the string):

(setq str0 "bla")
(setq str1 str0)

(eq str1 str0)
=>
t

(setq str1 "bla")

(eq str1 str0)
=>
nil

So there is no special behavior of setq, but differences due to the
objects themselves.

Steve Berman



reply via email to

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