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

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

Re: Appending to a list


From: steve-humphreys
Subject: Re: Appending to a list
Date: Sun, 13 Dec 2020 22:46:09 +0100





> Sent: Sunday, December 13, 2020 at 10:30 PM
> From: "Óscar Fuentes" <ofv@wanadoo.es>
> To: help-gnu-emacs@gnu.org
> Subject: Re: Appending to a list
>
> Joost Kremers <joostkremers@fastmail.fm> writes:
> 
> > On Sun, Dec 13 2020, steve-humphreys@gmx.com wrote:
> >> Would appending to list like this be good?
> >>
> >> (setq bird '("Toucan" "King Fisher"))
> >> (setq bird (append bird ("Swift") ))

Can one just append lists together, then store the result to one of the 
variables, as I did?  
Or could such an approach lead to unintended consequences in some circumstances?

Regards
Steve

> > This should result in an error, because `("Swift")` isn't quoted. You'd 
> > need:
> >
> >     (setq bird (append bird '("Swift")))
> >
> > But note that the last argument of `append` isn't copied, so it may not be a
> > good idea to use a literal list.
> >
> >> Or does one customarily use other constructs for
> >> adding to a list?
> >
> > `push` is what I would use:
> >
> >     (push "Swift" bird)
> 
> This does not append.
> 
> > No need to use `setq` here.
> >
> > `push` won't check if the element is already in the list, though. You can 
> > use
> > `cl-pushnew` in that case, but be sure to load the library it's in:
> >
> >     (require 'cl-lib)
> >     (cl-pushnew "Swift" bird)
> 
> Isn't that the same as
> 
> (add-to-list bird "Swift" t)
> 
> ?
> 
> 
>



reply via email to

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