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 14:08:34 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

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

> Stephen, thank you for the additional explanations.
>
>> On Dec 2, 2018, at 20:51, Stephen Berman <stephen.berman@gmx.net> wrote:
>
>>>> 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.
>> 
>> 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:
>
> Well, yes and no.
>
>>    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,
>
> That's not clear at all from the paragraph you quote. Because for all
> practical purposes, when I evaluate list0 I get (1 2) and not "a pointer to an
> object that is the list (1 2)".
>
> In fact, I just found the explanation, it is in the Introduction to Emacs Lisp
> and it says:
>
>> When a variable is set to a list with a function such as setq, it stores the
>> address of the first box in the variable.
>
> So, setq has a specific behavior when it applies to lists: it evaluates the
> form as a pointer to an object and not as a value.

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"):

   A “list” is a series of cons cells, linked together so that the CDR
   slot of each cons cell holds either the next cons cell or the empty
   list.
   [...]
     A note to C programmers: a Lisp list thus works as a “linked list”
     built up of cons cells.  Because pointers in Lisp are implicit, we
     do not distinguish between a cons cell slot holding a value versus
     pointing to the value.

See also the diagrams and explanations in (info "(elisp) Box Diagrams")
and in the discussion of `append' in (info "(elisp) Building Lists").

Steve Berman



reply via email to

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