emacs-devel
[Top][All Lists]
Advanced

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

Re: Negative nth index


From: Pip Cet
Subject: Re: Negative nth index
Date: Tue, 24 Dec 2024 21:33:17 +0000

"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.  ]

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

> 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.

To me, (nth -1 list) means "I set list to be the cdr of x, now tell me
what x was", so the right answer is "I can't do that, so here's an error
and go fix your code".  It's perfectly analogous to (/ 0 0), which means
"I multiplied a number x by 0 and got 0.  Tell me what x was."

For circular lists, we could give a possible answer for x (just as we
could give a possible answer for x in the 0/0 example), by moving back
in the cycle, but it's not the only possible answer, so we shouldn't.

(For lists both proper and circular, we could also create a new cons
cell and return it, I guess).

Indexing from the end is even worse than defining 0/0 to be 17.

Pip




reply via email to

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