help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Why doesn't nconc change my variable?


From: Drew Adams
Subject: RE: Why doesn't nconc change my variable?
Date: Sat, 4 Oct 2014 18:58:47 -0700 (PDT)

> (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).

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



reply via email to

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