Live data from Hacker News

Numbering should start at zero (1982)

cs.utexas.edu

41–50 of 309 posts

Re: Numbering should start at zero (1982)

#41
1-based numbering is nonsense. How many years old are you when you’re born?

I notice almost all defenses of 1-based indexing are purely based on arbitrary cultural factors or historical conventions, e.g. “that’s how it’s done in math”, rather than logical arguments.

Re: Numbering should start at zero (1982)

#42

1 or 0-based index... I recently picked up Lua for a toy project and I got to say that decades of training with 0-based indexes makes it hard for me to write correct lua code on the first try. I suppose 1-based index is more logical, but decades of programming languages choosing 0-based index is hard to ignore.

> I suppose 1-based index is more logical

Why?

Re: Numbering should start at zero (1982)

#43

  Numbering should start at π (2025) (umars.edu)

Seriously, it all depends on whether u're counting the items themselves (1-based) or the spaces btwn them (0-based). The former uses natural numbers, while the latter uses non-negative integers

For instance, when dealing with memory words, do u address the word itself or its starting location (the first byte)?

The same consideration applies to coordinate systems: r u positioning urself at the center of a pixel or at the pixel's origin?

Almost 2 decades ago I designed an hierarchical IDs system (similar to OIDs[1]). First I made it 0-based for each index in the path. After a while I understood that I need to be able to represent invalid/dummy IDs also, so I used -1 for that. It was ugly - so I made it 1-based, & any 0 index would've made the entire ID - invalid

---

1. https://en.wikipedia.org/wiki/Object_identifier

Re: Numbering should start at zero (1982)

#44
post #17

I appreciate Dijkstra's arguments, but the fact remains that no non-technical user is ever going to jibe with a zero-indexed system, no matter the technical merits. Languages aimed at casual audiences (e.g. scripting languages like Lua) should maybe just provide two different ways of indexing into arrays: an `offset` method that's zero-indexed, and an `item` method that's one-indexed. Let users pick, in a way that's…

There is no good reason to cater to non-technical users when designing programming languages. In what way is Lua “aimed at casual audiences”?

Re: Numbering should start at zero (1982)

#45
post #17

I appreciate Dijkstra's arguments, but the fact remains that no non-technical user is ever going to jibe with a zero-indexed system, no matter the technical merits. Languages aimed at casual audiences (e.g. scripting languages like Lua) should maybe just provide two different ways of indexing into arrays: an `offset` method that's zero-indexed, and an `item` method that's one-indexed. Let users pick, in a way that's…

Dijkstra's objective was to make programming into an intellectually respectable, rigorous branch of mathematics, a motivation he mentions obliquely here. He was, generally speaking, opposed to non-technical programmers and languages aimed at casual audiences, such as BASIC and APL; they were at best irrelevant to his goal and at worst (I suspect, though he never said) threats to its credibility.

Re: Numbering should start at zero (1982)

#46
Perhaps ideally we'd change English to count the "first" entry in a sequence as the "zeroth" item, but the path dependency and the effort required to do that is rather large to say the least.

At least we're not stuck with the Roman "inclusive counting" system that included one extra number in ranges* so that e.g. weeks have "8" days and Sunday is two days before Monday since Monday is itself included in the count.

* https://en.wikipedia.org/wiki/Counting#Inclusive_counting

Re: Numbering should start at zero (1982)

#47

1 or 0-based index... I recently picked up Lua for a toy project and I got to say that decades of training with 0-based indexes makes it hard for me to write correct lua code on the first try. I suppose 1-based index is more logical, but decades of programming languages choosing 0-based index is hard to ignore.

Interestingly, it also poses great challenges for LLMs. GPT-4 can translate Perl into Python almost flawlessly, but its Lua is full of off-by-one errors.

Re: Numbering should start at zero (1982)

#48
post #39
post #27

Earlier quoted context omitted.

It makes more sense if you think of indices as pointing between elements: 0 1 2 3 4 ----------------- | A | B | C | D | ----------------- -4 -3 -2 -1 -0 Except, of course, -0 doesn't exist. AFAIK that's why C# chose to add a special "index from end" operator, `^`, instead of using negative indices.

> Except, of course, -0 doesn't exist. Not for integers on modern hardware. If only hardware used ones’ complement ( https://en.wikipedia.org/wiki/Ones'_complement)… ;-) Meanwhile, the workaround is to use -1 through -5 to index from the end of the array.

That makes sense; in the diagram of the 4-element array, 3 points at the same point as -1.

Re: Numbering should start at zero (1982)

#49
post #17

I appreciate Dijkstra's arguments, but the fact remains that no non-technical user is ever going to jibe with a zero-indexed system, no matter the technical merits. Languages aimed at casual audiences (e.g. scripting languages like Lua) should maybe just provide two different ways of indexing into arrays: an `offset` method that's zero-indexed, and an `item` method that's one-indexed. Let users pick, in a way that's…

There is no good reason to cater to non-technical users when designing programming languages. In what way is Lua “aimed at casual audiences”?

From Roberto Ierusalimschy himself [0]:

"one of the design goals, was for Lua to be easy for nonprogrammers to use"

[1] https://www.reddit.com/r/lua/comments/w8wgqb/complete_interv...

Re: Numbering should start at zero (1982)

#50
post #3

The 1980s were not a particularly enlightened time for programming language design; and Dijkstra's opinions seem to carry extra weight mainly because his name has a certain shock and awe factor. It isn't usual for me to agree with the mathematical convention for notations, but the 1st element of a sequence being denoted with a "1" just seems obviously superior. I'm sure there is a culture that counts their first fing…

For math too, 0-based indexing is superior. When taking sub-matrices (blocks), with 1-based indexing you have to deal with + 1 and - 1 terms for the element indices. E.g. the third size-4 block of a 16x16 matrix begins at (3-1)*4+1 in 1-based indexing, at 2*4 in 0-based indexing (where the 2 is naturally the 0-indexed block index). Also, the origin is at 0, not at 1. If you begin at 1, you've already moved some dista…

Just speaking anecdotally, I had the impression that math people prefer 1-based indexing. I've heard that Matlab is 1-based because it was written by math majors, rather than CS majors.
Post reply on HN