emacs-devel
[Top][All Lists]
Advanced

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

Re: Negative nth index


From: Philip Kaludercic
Subject: Re: Negative nth index
Date: Tue, 24 Dec 2024 13:09:22 +0000

Thierry Volpiatto <thievol@posteo.net> writes:

> Stefan Monnier <monnier@iro.umontreal.ca> writes:
>
>>>>>> Should negative index for nth be valid? e.g.
>>>>>> (nth -1 '(1 2)) returns 1
>>
>> [ I'd make it return 2 otherwise you can't use it to return the
>>   last element.  ]
>>
>>>>> What does this do in other Lisps?
>>>> In CL, it errors (ditto for nthcdr with negative index).  Same for (nth
>>>> '(1 2 3) -1) in Clojure or (list-ref '(1 2 3) -1) in Scheme.
>>> Stefan Monnier, any comments here?
>>
>> 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?
>> Would it be significantly more efficient than doing it "by hand" e.g.:
>>
>>     (let ((l (length X)))
>>       (nth (if (< i 0) (- l i) i) X))
>
> I guess you meant (+ l i) no?

And do we need a check to see if (< (- i) (length l)),  otherwise

  (nth -10 '(1 2 3))

could still return a non-nil value, while

  (nth 10 '(1 2 3))

just returns nil.

Using (mod i l) would also be too DWIM-y, right?

>>
>> - Stefan
>>
>>
>>



reply via email to

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