all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Stephen Berman <stephen.berman@gmx.net>
To: Jean-Christophe Helary <brandelune@gmail.com>
Cc: help-gnu-emacs <help-gnu-emacs@gnu.org>
Subject: Re: using setq to create lists based on other lists...
Date: Sun, 02 Dec 2018 16:00:09 +0100	[thread overview]
Message-ID: <87r2f02cue.fsf@rub.de> (raw)
In-Reply-To: <6512D5B6-B3BE-4B02-81E0-B73F4BED9FCE@gmail.com> (Jean-Christophe Helary's message of "Sun, 2 Dec 2018 22:28:39 +0900")

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



  parent reply	other threads:[~2018-12-02 15:00 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.5010.1543748027.1284.help-gnu-emacs@gnu.org>
2018-12-02 11:21 ` using setq to create lists based on other lists Barry Margolin
2018-12-02 11:51   ` Stephen Berman
2018-12-02 12:22     ` Jean-Christophe Helary
2018-12-02 13:08       ` Stephen Berman
2018-12-02 13:28         ` Jean-Christophe Helary
2018-12-02 14:40           ` Michael Heerdegen
2018-12-02 15:34             ` Jean-Christophe Helary
2018-12-02 15:44               ` Michael Heerdegen
2018-12-02 15:57                 ` Jean-Christophe Helary
2018-12-02 15:00           ` Stephen Berman [this message]
2018-12-02 15:30             ` Jean-Christophe Helary
     [not found]             ` <mailman.5026.1543764670.1284.help-gnu-emacs@gnu.org>
2018-12-04  9:00               ` Barry Margolin
2018-12-02 12:03   ` Jean-Christophe Helary
     [not found] <mailman.5042.1543777897.1284.help-gnu-emacs@gnu.org>
2018-12-04  9:04 ` Barry Margolin
     [not found]   ` <(message>
     [not found]     ` <from>
     [not found]       ` <Barry>
     [not found]         ` <Margolin>
     [not found]           ` <on>
     [not found]             ` <Tue>
     [not found]               ` <04>
     [not found]                 ` <Dec>
     [not found]                   ` <2018>
     [not found]                     ` <04:04:52>
2018-12-04 13:56   ` Stefan Monnier
2018-12-05  1:07   ` Robert Thorpe
2018-12-05  2:32     ` Drew Adams
2018-12-05  6:45       ` Jean-Christophe Helary
2018-12-05  8:00         ` Marcin Borkowski
2018-12-05  8:11           ` Jean-Christophe Helary
2018-12-05 14:57         ` Drew Adams
     [not found]         ` <mailman.5218.1544021892.1284.help-gnu-emacs@gnu.org>
2018-12-05 16:59           ` Barry Margolin
     [not found]     ` <mailman.5186.1543978155.1284.help-gnu-emacs@gnu.org>
2018-12-05 16:50       ` Barry Margolin
     [not found]   ` <mailman.5145.1543931778.1284.help-gnu-emacs@gnu.org>
2018-12-05 16:47     ` Barry Margolin
2018-12-02 10:53 Jean-Christophe Helary
2018-12-02 15:07 ` Stefan Monnier
2018-12-02 15:41   ` Jean-Christophe Helary
2018-12-02 16:05     ` Stefan Monnier
2018-12-02 16:23       ` Jean-Christophe Helary
2018-12-02 17:02         ` Stefan Monnier
2018-12-02 17:21           ` Jean-Christophe Helary
2018-12-02 19:11             ` Robert Thorpe
2018-12-02 23:44               ` Jean-Christophe Helary
     [not found]   ` <mailman.5028.1543765273.1284.help-gnu-emacs@gnu.org>
2018-12-03 13:43     ` Rusi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87r2f02cue.fsf@rub.de \
    --to=stephen.berman@gmx.net \
    --cc=brandelune@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.