unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: David Kastrup <dak@gnu.org>
Cc: Dave Love <d.love@dl.ac.uk>,
	emacs-devel@gnu.org, Stefan Monnier <monnier@iro.umontreal.ca>,
	Miles Bader <miles@gnu.org>
Subject: Re: space leak from `values'
Date: 29 Jul 2004 10:50:54 +0200	[thread overview]
Message-ID: <x5ekmv2o41.fsf@lola.goethe.zz> (raw)
In-Reply-To: <m33c3bdx2x.fsf@kfs-l.imdomain.dk>

storm@cua.dk (Kim F. Storm) writes:

> Miles Bader <miles@lsi.nec.co.jp> 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

  reply	other threads:[~2004-07-29  8:50 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <rzqzn5qehby.fsf@albion.dl.ac.uk>
     [not found] ` <E1BoSQg-0001Br-II@fencepost.gnu.org>
     [not found]   ` <rzq3c3eex5r.fsf@albion.dl.ac.uk>
     [not found]     ` <E1Bp9wv-0004v0-Ts@fencepost.gnu.org>
     [not found]       ` <rzqllh4m4yi.fsf@albion.dl.ac.uk>
     [not found]         ` <jwvu0vs9gjz.fsf-monnier+emacs@gnu.org>
2004-07-28 23:13           ` space leak from `values' Kim F. Storm
2004-07-29  7:50             ` Miles Bader
2004-07-29  8:41               ` Kim F. Storm
2004-07-29  8:50                 ` David Kastrup [this message]
2004-07-29  9:06                 ` Miles Bader
2004-07-29 12:00                   ` Kim F. Storm
2004-07-30 16:16                     ` Richard Stallman
2004-08-20 12:51                       ` Kim F. Storm
2004-08-21 16:49                         ` Richard Stallman
2004-07-30  4:54                 ` Richard Stallman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=x5ekmv2o41.fsf@lola.goethe.zz \
    --to=dak@gnu.org \
    --cc=d.love@dl.ac.uk \
    --cc=emacs-devel@gnu.org \
    --cc=miles@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).