From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Tim X Newsgroups: gmane.emacs.help Subject: Re: Adding Lists/Sequences Date: Thu, 25 Sep 2008 15:27:48 +1000 Organization: Rapt Technologies Message-ID: <87hc846bhn.fsf@lion.rapttech.com.au> References: <3b97cca7-1c31-4e07-ba09-f0aeca96019c@34g2000hsh.googlegroups.com> <85r67apl6z.fsf@lola.goethe.zz> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1222321394 7333 80.91.229.12 (25 Sep 2008 05:43:14 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 25 Sep 2008 05:43:14 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Sep 25 07:44:12 2008 connect(): Connection refused Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1Kije4-0006GU-KS for geh-help-gnu-emacs@m.gmane.org; Thu, 25 Sep 2008 07:44:08 +0200 Original-Received: from localhost ([127.0.0.1]:43622 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Kijd2-0003rv-Fx for geh-help-gnu-emacs@m.gmane.org; Thu, 25 Sep 2008 01:43:04 -0400 Original-Path: news.stanford.edu!headwall.stanford.edu!newshub.sdsu.edu!news.astraweb.com!border2.newsrouter.astraweb.com!not-for-mail Original-Newsgroups: gnu.emacs.help User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (gnu/linux) Cancel-Lock: sha1:kXPYOJBxX249k99F60wsrbkmGFo= Original-Lines: 95 Original-NNTP-Posting-Host: de10aa4c.news.astraweb.com Original-X-Trace: DXC=gb<[`N2iDS:CObSL`Ra@h6L?0kYOcDh@:; >GTR`=ZX:29jMSI8HST^>:gTkTnZ]WU0]]NDGf:^d; 6 Original-Xref: news.stanford.edu gnu.emacs.help:162694 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:58036 Archived-At: Thierry Volpiatto writes: > David Kastrup writes: > >> Thierry Volpiatto writes: >> >>> Nordlöw writes: >>> >>>> Is there a general function, say foo, that adds lists or, even better, >>>> sequences together? >>>> >>>> I want this >>>> (foo '("a" "b") '("c" "d")) >>>> to evaluate to >>>> '("a" "b" "c" "d") >>> >>> ,---- >>> | ELISP> (nconc '("a" "b" "c") '("d" "e" "f")) >>> | ("a" "b" "c" "d" "e" "f") >>> `---- >>> >>> Note: `append' is not destructive >> >> This is so bad that I can't believe it. >> >> (defun ugh () (nconc '("a" "b" "c") '("d" "e" "f"))) >> (ugh) -> ("a" "b" "c" "d" "e" "f") >> (ugh) -> ("a" "b" "c" "d" "e" "f" . #3) >> >> The latter is a tail-cyclic list. >> >> (ugh) -> hangs > We speak of what is bad and very bad but didn't answer to what is good > or not, i send here a bad exemple as i constat i never used that in my > programs (nconc), idem for `nreverse'.And when i read again my commonlisp > book it say that is very delicate to use destructives functions. > So can you explain when it is good (if it is) to use these destructive > functions. > > Here an example of the use of append that is safe. > > > ,---- > | ELISP> (setq A '(a b c)) > | (a b c) > | > | ELISP> (setq B '(1 2 3)) > | (1 2 3) > | > | ELISP> (defun quite-good (beg tail) > | (append beg tail)) > | quite-good > | ELISP> (quite-good A B) > | (a b c 1 2 3) > | > | ELISP> (setq C (quite-good A B)) > | (a b c 1 2 3) > | > | ELISP> A > | (a b c) > | > | ELISP> B > | (1 2 3) > | > | ELISP> C > | (a b c 1 2 3) > `---- I think there are possibly two different issues here that need to be considered. 1. Modifying 'literal' lists is usually risky. Weird things can happen because on some levels, you are modifying what is best considered a constant. This is the difference between '(1 2 3) and (list 1 2 3). 2. The destructive operators manipulate the structure of the list rather than creating a new copy with its own structure. This can have two problems. Firstly, you can create circular lists as David points out and more generally, you can get unexpected 'side effects' if that structure is also bound to another symbol. In general, I think yo should only use the destructive operators on a structure which you know is not shared. For example, you could define your function so that it makes a copy of the list and then applies a distructive operation to the list and return the new list rather than just applying the destructive operation to the list that is passed in. Of course, the problem with tis is that often the destructive functions are used for performance reasons and having to make a copy each time may cause its own performance problems. HTH Tim -- tcross (at) rapttech dot com dot au