[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: space leak from `values'
From: |
David Kastrup |
Subject: |
Re: space leak from `values' |
Date: |
29 Jul 2004 10:50:54 +0200 |
User-agent: |
Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50 |
address@hidden (Kim F. Storm) writes:
> Miles Bader <address@hidden> writes:
>
> > address@hidden (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