unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Noob question: why sequences, lists in particular start at index 0 in Elisp?
@ 2020-11-08 13:32 Arthur Miller
  2020-11-08 14:42 ` Stefan Monnier
  2020-11-08 15:42 ` Philipp Stephani
  0 siblings, 2 replies; 5+ messages in thread
From: Arthur Miller @ 2020-11-08 13:32 UTC (permalink / raw)
  To: emacs-devel

I understand why they do so in C; but why in Elisp?

Just a curious question.



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

* Re: Noob question: why sequences, lists in particular start at index 0 in Elisp?
  2020-11-08 13:32 Noob question: why sequences, lists in particular start at index 0 in Elisp? Arthur Miller
@ 2020-11-08 14:42 ` Stefan Monnier
  2020-11-08 16:04   ` Arthur Miller
  2020-11-08 15:42 ` Philipp Stephani
  1 sibling, 1 reply; 5+ messages in thread
From: Stefan Monnier @ 2020-11-08 14:42 UTC (permalink / raw)
  To: Arthur Miller; +Cc: emacs-devel

> I understand why they do so in C; but why in Elisp?

Why should it be different in ELisp?

AFAIK, indexing should always be 0-based, so we don't get into silly "is
1900 in the 19th century or the 20th century".  And indeed computer
science got this mostly right, except for Fortran arrays and
Emacs buffers.


        Stefan




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

* Re: Noob question: why sequences, lists in particular start at index 0 in Elisp?
  2020-11-08 13:32 Noob question: why sequences, lists in particular start at index 0 in Elisp? Arthur Miller
  2020-11-08 14:42 ` Stefan Monnier
@ 2020-11-08 15:42 ` Philipp Stephani
  1 sibling, 0 replies; 5+ messages in thread
From: Philipp Stephani @ 2020-11-08 15:42 UTC (permalink / raw)
  To: Arthur Miller; +Cc: Emacs developers

Am So., 8. Nov. 2020 um 14:33 Uhr schrieb Arthur Miller
<arthur.miller@live.com>:
>
> I understand why they do so in C; but why in Elisp?
>
> Just a curious question.
>

See https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html
for a classic language-independent argument.



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

* Re: Noob question: why sequences, lists in particular start at index 0 in Elisp?
  2020-11-08 14:42 ` Stefan Monnier
@ 2020-11-08 16:04   ` Arthur Miller
  2020-11-08 16:16     ` Eli Zaretskii
  0 siblings, 1 reply; 5+ messages in thread
From: Arthur Miller @ 2020-11-08 16:04 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> I understand why they do so in C; but why in Elisp?
>
> Why should it be different in ELisp?
Because ELisp is not designed to be machine-close language easily
implemented in almost a one-pass compiler that translates it's references to
memory addresses directly? I am not sure it even makes sence for C; what
does it save? An extra addition? Is it worth?

But more because we humans thing in terms of first, second, etc.
Algorithms look much nicer when we can write [1,length] instead of
[0,length-1]. I could even find Pascal indexing to be useful sometimes.

Numerical Recipes had nice practice for their code; they would type
pointer-- on base pointer so they could write algorithms as in
[1,length] (and yes I know they abandoned the practice in C++ edition).

I have red Dijkstra's paper on the issue from 1982, and I am not sure I
agree. He writes:

"the subscript range ( ... ); starting with 0, however, gives the nicer
range 0<= i < N". Well, yes; but only when iterating so we can write i <
N, instead of i <= N, or i != N. Yes he derives it from the mathematical
notion of smallest natural number; but then is 0 a number? More
important does it really matter?

When accessing the element in subscript one has to write array[N-1]; so
I am not sure that is so much nicer then to type i <= N. As I
understand, that is his main argument, and I don't think it is
strong enough. Actually I think it is more practical to use [1,N] then
[0,N-1] in a higher level language. Yes, we can also just define
something like "first" and "last" in our program; but then it is just
our "local" convention, which opens for missunderstanding and misstakes.
If it is supported on the language level, then users of that language should
have it as a rule, rather then just a package convention (opposite what
Dijkstra argues in his paper).

> AFAIK, indexing should always be 0-based, so we don't get into silly "is
> 1900 in the 19th century or the 20th century".
I am sure we can construct examples that support both arguments, since
bot versions make sense in different scenarios.

> And indeed computer
> science got this mostly right,

I dunno what to say; I am probably too pragmatic to support statements
of kind that there is always one right way. I find it a bit dogmatic to
say computer science got it right. 

> except for Fortran arrays and
> Emacs buffers.
Njah, I prefer moving to first line, not 0th line. I would refer to
Monnier's first paper, not 0th paper, I would probably direct someone to
first street to the right, not the 0th street. No?

Please, forgive me if I am annoying; I am just trying to understand it
better. Personally I think that higher level languages should probably
be closer to natural language and human reasoning, while assemblers or
maybe a language like C, very close to machine should probably be closer
to the machine.



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

* Re: Noob question: why sequences, lists in particular start at index 0 in Elisp?
  2020-11-08 16:04   ` Arthur Miller
@ 2020-11-08 16:16     ` Eli Zaretskii
  0 siblings, 0 replies; 5+ messages in thread
From: Eli Zaretskii @ 2020-11-08 16:16 UTC (permalink / raw)
  To: Arthur Miller; +Cc: monnier, emacs-devel

> From: Arthur Miller <arthur.miller@live.com>
> Date: Sun, 08 Nov 2020 17:04:44 +0100
> Cc: emacs-devel@gnu.org
> 
> Numerical Recipes had nice practice for their code; they would type
> pointer-- on base pointer so they could write algorithms as in
> [1,length] (and yes I know they abandoned the practice in C++ edition).

Numerical Recipes started in Fortran, didn't they?



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

end of thread, other threads:[~2020-11-08 16:16 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-08 13:32 Noob question: why sequences, lists in particular start at index 0 in Elisp? Arthur Miller
2020-11-08 14:42 ` Stefan Monnier
2020-11-08 16:04   ` Arthur Miller
2020-11-08 16:16     ` Eli Zaretskii
2020-11-08 15:42 ` Philipp Stephani

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).