arr[(i++ % arr.length)] = foo;
Is certainly nicer than the equivalent in one-based subscripting arr[(i++ % arr.length) + 1] = foo;
(The above is actually wrong, which helps the idea)I'll concede that it's not all that significant as a difference, but at least IMO it's nicer.
Also could argue that modular arithmetic and zero-based indexing makes more sense for negative indexing.
last := vector[vector-length(vector)];
is nicer than last := vector[vector-length(vector) - 1];
so in the end I'd say 'de gustibus non est disputandum' and people who prefer 0-based indexing can use most languages and I can dream of my own. base-element := some-vector[i];
left-child := some-vector[i * 2];
right-child := some-vector[i * 2 + 1];
where the root element is `some-vector[1]'.`l[:n]` gives you the first `n` elements of the list `l`. Ideally `l[-n:]` would give you the last `n` elements - but that doesn't work when `n` is zero.
I believe this is why C# introduced a special "index from end" operator, `^`, so you can refer to the end of the array as `^0`.
Then you couldn't do thing like l[-4:-2] to get a range of elements which seems slightly useful.
I'd claim 0-based indexing actually throws an annoying wrench in that. Consider for instance:
for n in [3, 2, 1, 0]:
start_window = arr[n: n+5]
end_window = arr[-n-5: -n]
The start_window indexing works fine, but end_window fails when n=0 because -0 is just 0, the start of the array, instead of the end. We're effectively missing one "fence-post". It'd work perfectly fine with MatLab-style (1-based, inclusive ranges) indexing.Assuming `a` is the address of the beginning of the array, the 0-based indexing on the left is equivalent to the memory access on the right (I'm using C syntax here):
a[0] == *(a + 0)
a[1] == *(a + 1)
a[2] == *(a + 2)
...
a[i] == *(a + i)
For 1-based indexing: a[1] == *(a + 1 - 1)
a[2] == *(a + 2 - 1)
a[3] == *(a + 3 - 1)
...
a[i] == *(a + i - 1)
This extra "-1" costs some performance (through it can be optimized-away in some cases).> but always assumed it traces back to memory offsets in an array rather than any principled stance because 0-counting sequences represents a crazy choice.
Also, the origin is at 0, not at 1. If you begin at 1, you've already moved some distance away from the origin at the start.
Besides, in the rare cases where it does matter you're free to pick whichever is convenient.
Zero based indexing had nothing to do with Dijkstra's opinion but the practical realities of hardware, memory addressing and assembly programming.
> I'm sure there is a culture that counts their first finger as 0
Not a one because zero as a concept was discovered many millenia after humans began counting.
So you claim this is just an appeal to authority and as a rebuttal you give appeal to emotion without being an authority at all?
> 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 finger as 0 and I expect they're mocked mercilessly for it by all their neighbours
> 0-counting sequences represents a crazy choice
5G chess move.
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 finger as 0 and I expect they're mocked mercilessly for it by all their neighbours. I've been programming for too long to appreciate it myself, but always assumed it traces back to memory offsets in an array rather than any principled stance because 0-counting sequences represents a crazy choice.