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: Joost Kremers
Subject: Re: Appending to a list
Date: Sun, 13 Dec 2020 22:03:34 +0100
User-agent: mu4e 1.5.7; emacs 27.1.50

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

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)

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)

HTH

-- 
Joost Kremers
Life has its moments



reply via email to

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