Live data from Hacker News

Why do arrays start at 0?

buttondown.email

571–580 of 702 posts

Re: Why do arrays start at 0?

#571

Earlier quoted context omitted.

> when we count, we start at 1 That is false; counting begins by initializing an accumulator to zero. When you register the first item, the count jumps from 0 to 1.

Who sets an accumulator to zero and then adds one in everyday language?

Not saying "zero" doesn't mean we don't mean it. All counting starts with 0. We just say "one" out loud as the first number. We probably should say "zero" too, but why say a word when you can say no words?

Re: Why do arrays start at 0?

#573
post #474

Earlier quoted context omitted.

Dijkstra's argument ( https://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF ) is that the index counts the number of predecessors. I think an age-based way to phrase it: in your 1st year, your age is 0; in your 2nd year, your age is 1; and so on. We can assign people numbers indicating what year of their lives they're in, or how many years they have lived, and both are fine, but we've settled on the latter.

> Dijkstra's argument ( https://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF ) is that the index counts the number of predecessors. That's not an argument. It's a coincidence. There are applications where what you care about is the number of predecessors (indeed, that's what the compiler cares about, which is why we have 0-indexing in the first place), but they are a tiny minority of all indexing. > I think an age-…

> That's not an argument. It's a coincidence. There are applications where what you care about is the number of predecessors (indeed, that's what the compiler cares about, which is why we have 0-indexing in the first place), but they are a tiny minority of all indexing.

I think there's nothing to say to the first two sentences but that I regard it as an argument that may be more or less convincing. I don't know exactly what it means for something to be an argument vs. a coincidence; it is a coincidence that, say, my name is what it is, but it is nonetheless correct for me to argue that that is my name.

These are all conventions anyway, and there is not much use (or even meaning) in arguing about which one is the right or wrong convention, just which one makes more or less sense; and this is one way to make sense of 0-based indexing, though of course there are also ways to make sense of 1-based indexing.

I'm not sure I buy that these cases are a tiny minority, but I'm certainly in no position to produce any data to the contrary.

> > I think an age-based way to phrase it: in your 1st year, your age is 0; in your 2nd year, your age is 1; and so on.

> But that isn't even true. No one ever reports the age of their new child as 0; instead, they will report a positive number of months, or -- if it's an extremely new child -- of weeks or days.

I can believe that ages aren't reported that way, although I think that a child who will be 1 year in 1 year should logically be said to be 0 years; but we can avoid that debate by considering future years: in, to pick the example that applies to me, my 42nd year of life, I am 41 (I could say 41 and 1 month, but I don't—in fact, there's a Seinfeld joke about that). Similarly, the 42nd entry in a 0-indexed array is indexed 41. It doesn't have to be that way, but I don't think one can argue that there's anything logically amiss about it (nor about 1-based indexing … but we do have to pick one).

Re: Why do arrays start at 0?

#574

Earlier quoted context omitted.

What if there was no t0 for the universe, as Hawking and a few other physicists have argued? There was just the first plank second and no meaningful before.

Well then clearly you'd have to index at -1, for the time state before the point at which there is no meaningful before. I'm not sure why this answer didn't occur to you.

I think Hawking reference imaginary time, so you might have to index at i.

Re: Why do arrays start at 0?

#575

Earlier quoted context omitted.

Why, it would be an inclusive instead of exclusive range. N is the last element, so no reason for N+1.

If we use inclusive ranges where 0 ≤ i ≤ N, then len(range(N)) == N+1. e.g. len(range(3)) == len([0, 1, 2, 3]) == 4

Wait, why would range(3) start at 0 and not 1 in a 1-based language?

Re: Why do arrays start at 0?

#576
post #571

Earlier quoted context omitted.

Who sets an accumulator to zero and then adds one in everyday language?

Not saying "zero" doesn't mean we don't mean it. All counting starts with 0. We just say "one" out loud as the first number. We probably should say "zero" too, but why say a word when you can say no words?

Or we just start counting with 1, since 1 is the first number we start with. Do small kids start with a concept of zero and then adding one to it, or do they just start at one?

Re: Why do arrays start at 0?

#577
post #256

Earlier quoted context omitted.

What counter? You're saying a counter exists before people start counting? Is this a form of mathematical Platonism?

What does your stopwatch say right now? Your pedometer? Your traffic clicker?

Those are devices, not the spoken language.

Re: Why do arrays start at 0?

#578
post #536

Earlier quoted context omitted.

Which floor is the first floor in your building? Depends which country your building is in, and more! My apartment building is 1,2… but my mall is G,M,1,2… (same country different architects). For many years I lived in Europe where it’s usually G,1,2… (where G can also be E or Fsz or whatever) and when I was in the US I had to remember that 1 is G, though L,2,3… is also common. City people live with index ambiguity a…

Quick complement. Where I’m from (Portugal) we use the literal number 0 for the ground floor.

In Europe is common for elevator to use zero for ground, even if language/culture has a specific name for it (piano terra/piano rialzato here)

Re: Why do arrays start at 0?

#579
post #45

Other advantages of zero based indexing, beyond being 'closer to the machine': It works better with the modulo operator: `array[i%length]` vs `array[(i+length-1)%length+1]`. Or you would have to define a modulo-like operator that maps ℕ to [1..n]. It works better if you have a multi-dimensional index, for example the pixels in an image. With 0 based indexing, pixel `(x,y)` is at `array[x+width y]`. With 1 based index…

A rare case where 1-based indexing is more convenient is complete binary trees laid out breadth-first (as in a standard binary heap): parent is i div 2 and children are 2i and 2i+1 when starting at one and who knows what when starting at zero. But that’s the only one I know.

An incredibly unrare case happens all the time in my work: array[length - 1], or variations of this. Anything involving the last element of the array, and often iterating through the whole array will use something similar at some point.

Re: Why do arrays start at 0?

#580
post #571

Earlier quoted context omitted.

Not saying "zero" doesn't mean we don't mean it. All counting starts with 0. We just say "one" out loud as the first number. We probably should say "zero" too, but why say a word when you can say no words?

Or we just start counting with 1, since 1 is the first number we start with. Do small kids start with a concept of zero and then adding one to it, or do they just start at one?

If you start at 1, how do you answer the question "how many apples are in the basket", when the basket is empty?

Or do you believe that the answer "none" or "zero" is then given without the activity of counting having taken place?

What do we call the meta-activity then: the procedure that results either in the "empty" answer or "one", "two"? Whatever that activity is called, it starts with a concept of zero. Let's call that activity "quanting". Quanting starts with a motivation to enumerate items, and an initially empty result. When no items are present, quanting terminates, reporting that zero/nothing/none result. Otherwise quanting branches into a subprocedure called counting, and that begins at 1.

Post reply on HN