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

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

Re: [External] : Re: Appending lists


From: Jean Louis
Subject: Re: [External] : Re: Appending lists
Date: Mon, 21 Jun 2021 00:46:01 +0300
User-agent: Mutt/2.0.7+183 (3d24855) (2021-05-28)

* Emanuel Berg via Users list for the GNU Emacs text editor 
<help-gnu-emacs@gnu.org> [2021-06-20 23:43]:
> Actually `append' can also be trouble because the last list
> isn't copied, the new list just has a cdr somewhere to it -
> yes, to the actual list.
> 
> Try this:
> 
>   (setq list-head '(1))                          ; (1)
> 
>   (setq list-tail '(2 3 4))                      ; (2 3 4)
> 
>   (setq whole-list (append list-head list-tail)) ; (1 2 3 4)
> 
>   (setcdr list-tail '(3.5 4))                    ; (3.5 4)
> 
>   whole-list                                     ; (1 2 3.5 4)
> 
> See that "whole-list" changed even tho we did it with `setq',
> append, and didn't mention it...

I see. I wonder if there are some implications in my code. Though
I never used `setcdr' in that combination (or never).

Maybe this is the way to go:

(setq list-head '(1)) ⇒ (1)
(setq list-tail '(2 3 4)) ⇒ (2 3 4)
(setq whole-list (append list-head (copy-sequence list-tail))) ⇒ (1 2 3 4)
(setcdr list-tail '(3.5 4)) ⇒ (3.5 4)
whole-list ⇒ (1 2 3 4)


-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



reply via email to

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