From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Why doesn't nconc change my variable?
Date: Sun, 05 Oct 2014 20:12:26 +0200 [thread overview]
Message-ID: <87fvf2mlmd.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: mailman.10475.1412502895.1147.help-gnu-emacs@gnu.org
Marcin Borkowski <mbork@wmi.amu.edu.pl> writes:
> On 2014-10-05, at 03:58, Drew Adams wrote:
>
>>> (setq my-list ())
>>> (nconc my-list '("wtf"))
>>>
>>> and my-list is still nil. If, OTOH, I do
>>> (setq my-list ())
>>> (setq my-list (nconc my-list '("wtf")))
>>>
>>> my-list is ("wtf").
>>>
>>> Why is that so? I though nconc is supposed to change all its
>>> arguments but the last one. Is the latter construct a correct
>>> way of adding an element at the end of the list?
>>
>> No, it's not supposed to do that. You discovered just what it
>> does do. And you could have discovered it earlier by reading
>> some doc. See the Elisp manual, node `Rearrangement' (found by
>> doing `i nconc' in the manual).
>
> I did read it. And a few times, for that matter.
>
> You see, one of the worst intellectual mistakes you can make is to have
> a wrong mental model of something. This seems to be the case here, and
> that's why I really want to grok this issue.
>
>> See also node `Sets and Lists', in particular this (about `delq',
>> but the same idea applies to other destructive list operations):
>>
>> Note that `(delq 'c sample-list)' modifies `sample-list' to splice
>> out the third element, but `(delq 'a sample-list)' does not splice
>> anything--it just returns a shorter list. Don't assume that a variable
>> which formerly held the argument LIST now has fewer elements, or that
>> it still holds the original list! Instead, save the result of `delq'
>> and use that. Most often we store the result back into the variable
>> that held the original list:
>>
>> (setq flowers (delq 'rose flowers))
>
> That I didn't read earlier; I've read it now, and still don't get it.
> Sorry. (Edit: while I was writing the rest of this post, things
> apparently got more clear, but I'll leave the rest, since if I'm right,
> this might be (hopefully) instructive for others reading it, and I might
> even want to save it to post it on my blog.)
>
> Now I have two choices. One is to install the C source and try to read
> it. This I cannot do now (time constraints, and while I did learn some
> C, it was, what, fifteen years ago or so). That's why I'm asking here.
> Please bear with me. This should also be good for Emacs, since it may
> be the case that if I cannot understand the manual, someone else might
> have troubles with that, too, so maybe the manual is buggy.
>
> What I thought was essentially this: under the hood, a list is a pointer
> (using C terminology) to a cons cell. The car of the cons cell is a
> pointer to some data (say, a string), and the cdr - a pointer to the
> next cons cell.
>
> What I thought is that if you say, e.g.,
>
> (setq my-list ("hello" "world"))
>
> and then
>
> (nconc my-list '("wtf"))
>
> then my-list points to the list ("hello" "world" "wtf"). This seems to
> be indeed the case. (And the wtf's get added if I repeat the nconc step
> - I confirmed that experimentally.)
You did not. Your experience was wrong or incomplete.
(setf print-circle t)
(defun strange (list) (nconc list '("wtf")))
(strange (strange (list 1 2 3)))
--> (1 2 3 . #1=("wtf" . #1#))
(setf print-circle nil
print-length 15
eval-expression-print-length 15)
(defun strange (list) (nconc list '("wtf"))) ; you need to redefine it.
(strange (strange (list 1 2 3)))
--> (1 2 3 "wtf" "wtf" "wtf" . #3)
Actually, if you try to call strange^3, it falls into an infinite loop,
because after strange^2, "wtf" is in a circular list.
Again, do not mix immutable data with mutable data, if you want to
mutate the result later!
Use: (nconc list (list "wtf")), not quote.
--
__Pascal Bourguignon__ http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk
next prev parent reply other threads:[~2014-10-05 18:12 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-10-05 1:36 Why doesn't nconc change my variable? Marcin Borkowski
2014-10-05 1:58 ` Drew Adams
2014-10-05 9:54 ` Marcin Borkowski
2014-10-05 10:52 ` Thorsten Jolitz
2014-10-05 16:29 ` Drew Adams
2014-10-05 16:38 ` Drew Adams
[not found] ` <mailman.10475.1412502895.1147.help-gnu-emacs@gnu.org>
2014-10-05 18:12 ` Pascal J. Bourguignon [this message]
2014-10-05 18:14 ` Pascal J. Bourguignon
2014-10-05 18:31 ` Pascal J. Bourguignon
[not found] <mailman.10460.1412473030.1147.help-gnu-emacs@gnu.org>
2014-10-05 1:59 ` Pascal J. Bourguignon
2014-10-05 9:35 ` Marcin Borkowski
[not found] ` <mailman.10474.1412501757.1147.help-gnu-emacs@gnu.org>
2014-10-05 17:50 ` Pascal J. Bourguignon
2014-10-05 18:04 ` Pascal J. Bourguignon
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
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87fvf2mlmd.fsf@kuiper.lan.informatimago.com \
--to=pjb@informatimago.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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).