all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Negative nth index
@ 2024-12-22  5:43 Anand Tamariya
  2024-12-22  7:39 ` Eli Zaretskii
  2024-12-22 10:16 ` Andreas Schwab
  0 siblings, 2 replies; 12+ messages in thread
From: Anand Tamariya @ 2024-12-22  5:43 UTC (permalink / raw)
  To: Emacs Devel

[-- Attachment #1: Type: text/plain, Size: 71 bytes --]

Should negative index for nth be valid? e.g.
(nth -1 '(1 2)) returns 1

[-- Attachment #2: Type: text/html, Size: 137 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Negative nth index
  2024-12-22  5:43 Negative nth index Anand Tamariya
@ 2024-12-22  7:39 ` Eli Zaretskii
  2024-12-22  9:01   ` Tassilo Horn
  2024-12-22 10:16 ` Andreas Schwab
  1 sibling, 1 reply; 12+ messages in thread
From: Eli Zaretskii @ 2024-12-22  7:39 UTC (permalink / raw)
  To: Anand Tamariya; +Cc: emacs-devel

> From: Anand Tamariya <atamariya@gmail.com>
> Date: Sun, 22 Dec 2024 11:13:18 +0530
> 
> Should negative index for nth be valid? e.g. 
> (nth -1 '(1 2)) returns 1

What does this do in other Lisps?



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Negative nth index
  2024-12-22  7:39 ` Eli Zaretskii
@ 2024-12-22  9:01   ` Tassilo Horn
  2024-12-23  5:40     ` Stefan Kangas
  0 siblings, 1 reply; 12+ messages in thread
From: Tassilo Horn @ 2024-12-22  9:01 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: Anand Tamariya, emacs-devel

Eli Zaretskii <eliz@gnu.org> writes:

>> Should negative index for nth be valid? e.g. 
>> (nth -1 '(1 2)) returns 1
>
> 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.

Bye,
Tassilo



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Negative nth index
  2024-12-22  5:43 Negative nth index Anand Tamariya
  2024-12-22  7:39 ` Eli Zaretskii
@ 2024-12-22 10:16 ` Andreas Schwab
  1 sibling, 0 replies; 12+ messages in thread
From: Andreas Schwab @ 2024-12-22 10:16 UTC (permalink / raw)
  To: Anand Tamariya; +Cc: Emacs Devel

On Dez 22 2024, Anand Tamariya wrote:

> (nth -1 '(1 2)) returns 1

What would (nth -1 '#1=(1 2 . #1#)) return?

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Negative nth index
  2024-12-22  9:01   ` Tassilo Horn
@ 2024-12-23  5:40     ` Stefan Kangas
  2024-12-24  5:11       ` Stefan Monnier
  0 siblings, 1 reply; 12+ messages in thread
From: Stefan Kangas @ 2024-12-23  5:40 UTC (permalink / raw)
  To: Tassilo Horn, Eli Zaretskii; +Cc: Anand Tamariya, emacs-devel, Stefan Monnier

Tassilo Horn <tsdh@gnu.org> writes:

> Eli Zaretskii <eliz@gnu.org> writes:
>
>>> Should negative index for nth be valid? e.g.
>>> (nth -1 '(1 2)) returns 1
>>
>> 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?



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Negative nth index
  2024-12-23  5:40     ` Stefan Kangas
@ 2024-12-24  5:11       ` Stefan Monnier
  2024-12-24  6:42         ` Teemu Likonen
                           ` (4 more replies)
  0 siblings, 5 replies; 12+ messages in thread
From: Stefan Monnier @ 2024-12-24  5:11 UTC (permalink / raw)
  To: Stefan Kangas; +Cc: Tassilo Horn, Eli Zaretskii, Anand Tamariya, emacs-devel

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


- Stefan




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Negative nth index
  2024-12-24  5:11       ` Stefan Monnier
@ 2024-12-24  6:42         ` Teemu Likonen
  2024-12-24  6:48         ` Thierry Volpiatto
                           ` (3 subsequent siblings)
  4 siblings, 0 replies; 12+ messages in thread
From: Teemu Likonen @ 2024-12-24  6:42 UTC (permalink / raw)
  To: Stefan Monnier, Stefan Kangas
  Cc: Tassilo Horn, Eli Zaretskii, Anand Tamariya, emacs-devel

[-- Attachment #1: Type: text/plain, Size: 547 bytes --]

* 2024-12-24 00:11:26-0500, Stefan Monnier wrote:

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

First test if index (i) is negative and only then run the needed
"length" etc.

-- 
/// Teemu Likonen - .-.. https://www.iki.fi/tlikonen/
// OpenPGP: 6965F03973F0D4CA22B9410F0F2CAE0E07608462

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 251 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Negative nth index
  2024-12-24  5:11       ` Stefan Monnier
  2024-12-24  6:42         ` Teemu Likonen
@ 2024-12-24  6:48         ` Thierry Volpiatto
  2024-12-24 13:09           ` Philip Kaludercic
  2024-12-24 15:18         ` Stefan Monnier
                           ` (2 subsequent siblings)
  4 siblings, 1 reply; 12+ messages in thread
From: Thierry Volpiatto @ 2024-12-24  6:48 UTC (permalink / raw)
  To: Stefan Monnier
  Cc: Stefan Kangas, Tassilo Horn, Eli Zaretskii, Anand Tamariya,
	emacs-devel

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?

>
> - Stefan
>
>
>

-- 
Thierry



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Negative nth index
  2024-12-24  6:48         ` Thierry Volpiatto
@ 2024-12-24 13:09           ` Philip Kaludercic
  0 siblings, 0 replies; 12+ messages in thread
From: Philip Kaludercic @ 2024-12-24 13:09 UTC (permalink / raw)
  To: Thierry Volpiatto
  Cc: Stefan Monnier, Stefan Kangas, Tassilo Horn, Eli Zaretskii,
	Anand Tamariya, emacs-devel

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



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Negative nth index
  2024-12-24  5:11       ` Stefan Monnier
  2024-12-24  6:42         ` Teemu Likonen
  2024-12-24  6:48         ` Thierry Volpiatto
@ 2024-12-24 15:18         ` Stefan Monnier
  2024-12-24 15:24         ` Sebastián Monía
  2024-12-24 21:33         ` Pip Cet via Emacs development discussions.
  4 siblings, 0 replies; 12+ messages in thread
From: Stefan Monnier @ 2024-12-24 15:18 UTC (permalink / raw)
  To: Stefan Kangas; +Cc: Tassilo Horn, Eli Zaretskii, Anand Tamariya, emacs-devel

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

AFAICT so far people were only able to fix my code but not answer my
question, which suggests the answer is no, which would be a good
argument in favor of staying with the current behavior: the change
wouldn't save us from going down the list twice.


        Stefan




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Negative nth index
  2024-12-24  5:11       ` Stefan Monnier
                           ` (2 preceding siblings ...)
  2024-12-24 15:18         ` Stefan Monnier
@ 2024-12-24 15:24         ` Sebastián Monía
  2024-12-24 21:33         ` Pip Cet via Emacs development discussions.
  4 siblings, 0 replies; 12+ messages in thread
From: Sebastián Monía @ 2024-12-24 15:24 UTC (permalink / raw)
  To: Stefan Monnier
  Cc: Stefan Kangas, Tassilo Horn, Eli Zaretskii, Anand Tamariya,
	emacs-devel


Hello,

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.

And for this reason I _would not_ expect it to work with negative
numbers in Elisp.

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

This is "modern"? behaviour that I would expect from seq-elt, because it
is the newest, bell and whistles library.

I have a feeling that keeping `nth' more limited for reasons of
tradition is silly, but...it feels right.

Regards,
Seb

-- 
Sebastián Monía
https://site.sebasmonia.com/



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Negative nth index
  2024-12-24  5:11       ` Stefan Monnier
                           ` (3 preceding siblings ...)
  2024-12-24 15:24         ` Sebastián Monía
@ 2024-12-24 21:33         ` Pip Cet via Emacs development discussions.
  4 siblings, 0 replies; 12+ messages in thread
From: Pip Cet via Emacs development discussions. @ 2024-12-24 21:33 UTC (permalink / raw)
  To: Stefan Monnier
  Cc: Stefan Kangas, Tassilo Horn, Eli Zaretskii, Anand Tamariya,
	emacs-devel

"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




^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2024-12-24 21:33 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-22  5:43 Negative nth index Anand Tamariya
2024-12-22  7:39 ` Eli Zaretskii
2024-12-22  9:01   ` Tassilo Horn
2024-12-23  5:40     ` Stefan Kangas
2024-12-24  5:11       ` Stefan Monnier
2024-12-24  6:42         ` Teemu Likonen
2024-12-24  6:48         ` Thierry Volpiatto
2024-12-24 13:09           ` Philip Kaludercic
2024-12-24 15:18         ` Stefan Monnier
2024-12-24 15:24         ` Sebastián Monía
2024-12-24 21:33         ` Pip Cet via Emacs development discussions.
2024-12-22 10:16 ` Andreas Schwab

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.