[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Why doesn't nconc change my variable?
From: |
Pascal J. Bourguignon |
Subject: |
Re: Why doesn't nconc change my variable? |
Date: |
Sun, 05 Oct 2014 20:12:26 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) |
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
Re: Why doesn't nconc change my variable?, Pascal J. Bourguignon, 2014/10/05