emacs-devel
[Top][All Lists]
Advanced

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

Re: Negative nth index


From: Mattias Engdegård
Subject: Re: Negative nth index
Date: Wed, 25 Dec 2024 11:30:20 +0100

24 dec. 2024 kl. 22.33 skrev Pip Cet via Emacs development discussions. 
<emacs-devel@gnu.org>:

> I think the OP meant that (nth -1 '(1 2)) is 1 on current Emacs, which
> is definitely unexpected.

It's a straight consequence of (nth N L) = (car (nthcdr N L)).

>> I don't see a strong argument in favor of making it index from the end
>> rather than signal an error.  What would the implementation look like?
> 
> I agree, but the current implementation does not signal an error.  It
> should, and I think that's what the suggestion was.

It is true that signalling an error for negative arguments might catch some 
bugs but doing so would also risk breaking working code that relies on the 
current (well-defined) behaviour.

And there's not much performance to gain from it: while a good compiler might 
use it for value range propagation, as in

  (use (nth n lis))
  (if (< n 0) ...)  ; dead if nth signals on negative n

it doesn't really justify a change.




reply via email to

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