From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: David Kastrup Newsgroups: gmane.emacs.devel Subject: Re: space leak from `values' Date: 29 Jul 2004 10:50:54 +0200 Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Message-ID: References: NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1091091199 16966 80.91.224.253 (29 Jul 2004 08:53:19 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Thu, 29 Jul 2004 08:53:19 +0000 (UTC) Cc: Dave Love , emacs-devel@gnu.org, Stefan Monnier , Miles Bader Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Jul 29 10:53:08 2004 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1Bq6ey-0001f0-00 for ; Thu, 29 Jul 2004 10:53:08 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1Bq6i9-0002GF-B6 for ged-emacs-devel@m.gmane.org; Thu, 29 Jul 2004 04:56:25 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.33) id 1Bq6hU-0002G8-L1 for emacs-devel@gnu.org; Thu, 29 Jul 2004 04:55:44 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.33) id 1Bq6hQ-0002Fv-PQ for emacs-devel@gnu.org; Thu, 29 Jul 2004 04:55:41 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1Bq6hP-0002Fs-Vd for emacs-devel@gnu.org; Thu, 29 Jul 2004 04:55:40 -0400 Original-Received: from [199.232.76.164] (helo=fencepost.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.34) id 1Bq6dm-0002ID-6C for emacs-devel@gnu.org; Thu, 29 Jul 2004 04:51:54 -0400 Original-Received: from localhost ([127.0.0.1] helo=lola.goethe.zz) by fencepost.gnu.org with esmtp (Exim 4.34) id 1Bq6cr-0006NF-6c; Thu, 29 Jul 2004 04:50:57 -0400 Original-To: storm@cua.dk (Kim F. Storm) In-Reply-To: Original-Lines: 45 User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.devel:26084 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:26084 storm@cua.dk (Kim F. Storm) writes: > Miles Bader writes: > > > storm@cua.dk (Kim F. Storm) writes: > > > + DEFUN ("setnthcdr", Fsetnthcdr, Ssetnthcdr, 2, 3, 0, > > > + doc: /* Set cdr of Nth element of LIST to VALUE (nil if omitted), returns the result. > > > > What's wrong with (setcdr (nthcdr (1- N) LIST) VALUE) ? > > Read the third line of the doc string: > > > If list has less than N elements, do not modify list. > > Your suggestion fails if N is less than the length of the list: > > (let ((list '(1 2 3 4)) > (n 10) > (value nil)) > (setcdr (nthcdr (1- n) list) value)) > > With (my version of) setnthcdr you replace > (if (> (length kill-ring) kill-ring-max) > (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))) > by > (setnthcdr kill-ring-max kill-ring) It must also be noted that length requires a lot of prerequisites: that the list is not circular, ends with a cons, and so on. For example, (length '(3 4 5 . 6)) throws an exception. One could write this as (setcdr (or (nthcdr (1- n) list) (cons nil nil)) value) or as (let ((tail (nthcdr (1- n) list))) (and (consp tail) (setcdr tail value))) or something to avoid the length call, however. -- David Kastrup, Kriemhildstr. 15, 44793 Bochum