emacs-devel
[Top][All Lists]
Advanced

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

Re: Noob question: why sequences, lists in particular start at index 0 i


From: Arthur Miller
Subject: Re: Noob question: why sequences, lists in particular start at index 0 in Elisp?
Date: Sun, 08 Nov 2020 17:04:44 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

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.



reply via email to

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