Comment by umanwizard
1 year ago
I find that argument to be written in a terse "mathy" style that makes it a bit hard to follow. So let me try to restate it in more concrete "programmy" terms.
To iterate over an array with "len" elements, it’s most elegant if “len” appears as a loop bound, rather than "len+1" or "len-1". Thus, in 0-based languages we use half-open ranges, whereas in 1-based languages we use closed ranges:
// real C
for (int i = 0; i < len; ++i)
process(array[i]);
// C-like language with 1-based indexing
for (int i = 1; i <= len; ++i)
process(array[i]);
But the second is inelegant when len is zero, because 0 isn’t a valid index at all, so it’s weird for it to appear as a bound.
No comments yet
Contribute on Hacker News ↗