News for hackers. Stuff that matters.
Re:Why numbering should start at zero (1982) by zAy0LfpBZLC8mAC on Thursday December 15, 2016 @02:22PM (#13187351)
> But having L[2] be the third index in an array is horribly counterintuitive and leads to countless mistakes.

Really, that's just a confusion caused by language and historical accidents.

There is nothing that inherently links "one" to "first", or "two" to "second"--it just so happens that for some weird reason, it's common in English to abbreviate "first" as "1st", "second" as "2nd", and so on.

But just as "A" is the first letter of the alphabet (and not, say, the Ast), "0" is the first cardinal number, and "first" is the first ordinal number.

And there is a very simple reason why zero is the correct default first array index: Zero is the neutral element of addition, and (sub)array indexing is an additive operation.

Re:Why numbering should start at zero (1982) by 51Cards on Thursday December 15, 2016 @02:48PM (#13187610)
I have to offer a counter view to this. Using letters of the alphabet as an analogy isn't accurate to me. Each letter we accept as an element... so a true analogy would be to put a Space as the first character of the alphabet, to indicate the absence of a letter.

Zero indicates the absence of a value in our numbering system. If I have a single apple, I have 1 Apple, not 0 Apples. Thus if I have 1 element in an Array I have element[1], not element[0]. It is more natural for us to use numbers not by their cardinal position in a set, but by their inferred value... because they represent quantities.

I am one of those who feels the confusion around zero bounded indexes leads to countless issues because it is counter-intuitive to how we use numbers in real life. I agree it is beaten into developers that this is how is should be, but to me we would have been further ahead to let developers accept what comes intuitively.

Re:Why numbering should start at zero (1982) by zAy0LfpBZLC8mAC on Thursday December 15, 2016 @03:00PM (#13187742)
> I have to offer a counter view to this. Using letters of the alphabet as an analogy isn't accurate to me. Each letter we accept as an element... so a true analogy would be to put a Space as the first character of the alphabet, to indicate the absence of a letter.

So, you would say that "A" is not the first element of the alphabet or that "0" is not the first cardinal number?

> Zero indicates the absence of a value in our numbering system.

You are utterly confused. If I have an account balance of zero, that indicates an absence of a value? How is zero mathematically any different then +1000 or -1000 that justifies making it a special case?

> If I have a single apple, I have 1 Apple, not 0 Apples. Thus if I have 1 element in an Array I have element[1], not element[0].

You are mixing up ordinal and cardinal numbers.

> It is more natural for us to use numbers not by their cardinal position in a set, but by their inferred value... because they represent quantities.

There is no such thing as a "cardinal position", positions are ordinal, cardinality describes sizes.

> I am one of those who feels the confusion around zero bounded indexes leads to countless issues because it is counter-intuitive to how we use numbers in real life.

Nope, what leads to problems is that people don't understand the difference between cardinal and ordinal numbers. You cannot fix that by changing the first index, all that causes is a shift to different bugs. Including stuff like equating zero with the absence of a value.

Re:Why numbering should start at zero (1982) by ivanhoe on Thursday December 15, 2016 @03:53PM (#13188286)
> How is zero mathematically any different then +1000 or -1000 that justifies making it a special case?

Zero is actually always a special case in math, specially in algebra, the only number where division by it is not defined.

> Nope, what leads to problems is that people don't understand the difference between cardinal and ordinal numbers.

Actually, by Von Neumann definition of ordinals, ordinal 0 represents an empty set, thus it's equivalent of null, not single element set. So strictly mathematically speaking for non-empty sets we should use ordinals starting at 1.

But using zero-based indices is just a matter of convention, so lets not make a big philosophy out of it...

Re:Why numbering should start at zero (1982) by zAy0LfpBZLC8mAC on Thursday December 15, 2016 @04:10PM (#13188418)
> Zero is actually always a special case in math, specially in algebra, the only number where division by it is not defined.

And what does any of this have to do with division?

> Actually, by Von Neumann definition of ordinals, ordinal 0 represents an empty set, thus it's equivalent of null, not single element set. So strictly mathematically speaking for non-empty sets we should use ordinals starting at 1.

So, strictly mathematically speaking, we should use "2 apples" to describe an apple within another apple, where both of those apples also contain nothing?

I'm sorry, but this just makes no sense whatsoever. What relevance does von Neumann's construction of the natural numbers have to this question? Especially so, given that the empty set, aka "0", is the first(!) von Neumann ordinal. We should use 1-based ordinals because von Neumann constructed 0-based ordinals, maybe?

> But using zero-based indices is just a matter of convention, so lets not make a big philosophy out of it...

No, it's not. You yourself stated that the algebraic/arithmetic behaviour of 0 is quite different from that of 1, so how do you suddenly come to the conclusion that it's merely a matter of convention?

Re:Why numbering should start at zero (1982) by ivanhoe on Friday December 16, 2016 @03:35PM (#13196538)
> No, it's not. You yourself stated that the algebraic/arithmetic behaviour of 0 is quite different from that of 1, so how do you suddenly come to the conclusion that it's merely a matter of convention?

Because in programming it is, it's just a number chosen by some languages to start enumerating from because it makes things easier in some real-life cases. In pascal you could set the arbitrary start and end index and it worked just as well. It's like arguing which is a more precise way to measure temperature, in fahrenheit or celsius scale...

> What relevance does von Neumann's construction of the natural numbers have to this question? Especially so, given that the empty set, aka "0", is the first(!) von Neumann ordinal. We should use 1-based ordinals because von Neumann constructed 0-based ordinals, maybe?

You talked of ordinal numbers, so I cited one of the definitions of ordinal numbers, the one from the set theory. The similar reasoning applies for other definitions as well. Ordinal number zero is always the ordinal of an empty set. We use null for that in programming. So the next one is a single element set and it has ordinal 1. So how from this you conclude that index 0 is "the correct way" to count non-empty sets? You completely ignore the empty array that way...

Re:Why numbering should start at zero (1982) by zAy0LfpBZLC8mAC on Friday December 16, 2016 @04:21PM (#13196970)
> It's like arguing which is a more precise way to measure temperature, in fahrenheit or celsius scale...

Nah, bad analogy. Let me suggest a better version: It's like arguing about whether degree Fahrenheit or Kelvin is a more useful default unit to use for physics calculations. What do you think?

It's not just a number that's used to start enumerating from, it's a set of numbers used to calculate with as well, and array indexing, as I mentioned, is an additive operation, and thus using a set of indices that's not a group, for lack of an identity element, is as sensible as doing most of your physics calculations in degree Fahrenheit.

> You talked of ordinal numbers, so I cited one of the definitions of ordinal numbers, the one from the set theory. The similar reasoning applies for other definitions as well. Ordinal number zero is always the ordinal of an empty set.

Exactly. And you realize that that is the first ordinal number, right? "First" means "the one without a predecessor", and the empty set, also represented as a zero in that context, is the element without a predecessor, and as such the first ordinal number of that construction. Now, what is your point?

> We use null for that in programming.

We use null to represent the first ordinal number? What kind of weird languages are you using?!?

> So the next one is a single element set and it has ordinal 1. So how from this you conclude that index 0 is "the correct way" to count non-empty sets? You completely ignore the empty array that way...

First of all, it seems like you are utterly confused about the relevance of von Neumann's constructions of ordinal numbers to how to index arrays. There is none. It's simply an exercise in finding an isomorphism between sets and "normal" numbers.

Also, no, I don't ignore the empty array. An empty array has no positions, therefore, there is no ordinal number that could refer to a position in an empty array. If you have an index pointing into an empty array, you are doing something wrong. You cannot point to the fifth element in a four-element array, you cannot point to the second element in a one-element array, and you cannot even point to the first element in a zero-element array.

If you want to be able to have a data structure that can represent an index into an array, or alternatively the absence of an index (for whatever reason--one reason might be because the array is empty), that's your problem, not the problem of the index type. Why should we reserve perfectly useful values of data types to represent stuff that's logically separate from the type? How about we reserve the value "2" in floating point values for cases where you want to store the word "honey" as a floating point value? That's just crazy. You can possibly use such sentinel values in your software for performance reasons or whatever, but limiting sensible use by the general public because you want to mix concerns?!?

The third element of the sub-array starting at the second element of an array is which element of the base array?

With zero-based indices, that is a perfectly normal additive operation: 1 + 2 = 3 --the element is to be found at index 3.

With one-based indices, you resurrect all the crazyness of algebra and arithmetics before the zero was invented, just because you want to use the zero for something else: 2 + 3 = 5 ... and the index is 4, obviously!

Re:Why numbering should start at zero (1982) by thomnific on Thursday December 15, 2016 @03:20PM (#13187939)
I agree with you and also with the person you replied to. But more generally -- is it fair to say that cardinality describes size as opposed to quantity? If you go with the quantity concept instead then 1-based numbering makes sense and agrees with ordinary usage.

(Disclaimer: I used to hate 1-based indexing, for reasons similar to what you are saying, but I'm brainwashed by R these days and now rather like it.)

Re:Why numbering should start at zero (1982) by zAy0LfpBZLC8mAC on Thursday December 15, 2016 @03:30PM (#13188062)
> is it fair to say that cardinality describes size as opposed to quantity?

Nope, size is equivalent to quantity in this context. But ordinals describe position in an order as opposed to size/quantity. If you want to connect them mathematically, you would usually map a scale to a quantity measure, where the first unit position on the scale is the zero-position, the second unit position is the one-position, and so on--so, a quantity of size three units reaches from the position 0 on the scale to the position 3 on the scale.

> (Disclaimer: I used to hate 1-based indexing, for reasons similar to what you are saying, but I'm brainwashed by R these days and now rather like it.)

Well, it doesn't matter unless you have to to arithmetic with the indices, you might as well label them with letters from the alphabet, so if you don't need that, it's simply a matter of getting used to it. But in general-purpose programming languages, array indices are often computed, and 1-based indexing is as sensible there are having an integer type that doesn't have a zero. It's not that you cannot work with it, but it makes things unnecessarily complicated.

Re:Why numbering should start at zero (1982) by caf on Thursday December 15, 2016 @09:28PM (#13190603)
I am one of those who feels the confusion around zero bounded indexes leads to countless issues because it is counter-intuitive to how we use numbers in real life.

What's counter-intuitive to me is that we habitually call the second floor in a building the "first floor"!

Re:Why numbering should start at zero (1982) by 91bananas on Thursday December 15, 2016 @03:19PM (#13187922)
Never thought about it in terms of what the value of the number actually meant. You make an interesting argument.
Re:Why numbering should start at zero (1982) by jstimpfle on Thursday December 15, 2016 @02:35PM (#13187477)
Of course there is something that links "third" to "three", "fourth" to "four", etc. It's called language. So it's not surprising that we abbreviate "third" with "3rd".

The historical "accident" is that we don't start with "zeroth". That's probably what you wanted to say but the reasoning tripped me up.

Re:Why numbering should start at zero (1982) by zAy0LfpBZLC8mAC on Thursday December 15, 2016 @02:45PM (#13187577)
Well, yeah, I didn't want to go that deep into it, of course there is a reason why they are linked. But none that links them inherently, which is what I wrote. And the accident really simply is that "zero" wasn't an accepted concept yet when ordinal numbers were invented, which led to the first ordinal number being called "first" and not "zeroth". Meanwhile, humanity largely has accepted zero as a useful and coherent concept, but just didn't bother to update the names of ordinals to fit better with that, which is where the confusion comes from.
Re:Why numbering should start at zero (1982) by jstimpfle on Thursday December 15, 2016 @05:15PM (#13189034)
So many words, of vague meaning (e.g. "inherently"), and confusing levels of quoting (e.g. first vs "first"). What's wrong with just saying "Unfortunately we don't start with 'zeroth'"? Can't discover more meaning in these paragraphs.
Re:Why numbering should start at zero (1982) by cousin_it on Thursday December 15, 2016 @04:15PM (#13188462)
I think this hits the nail on the head. Counting things from 0 instead of 1 would be nice in the real world too, not just in programming. For example, in a base 10 system, you could have exactly 10 pigs with single-digit numbers painted on their sides. That makes much more sense than the current system, where the last pig requires a two-digit number for some reason. Tonight I'll go to bed happy in the knowledge that programming isn't wrong, it's the world that is buggy.
Re:Why numbering should start at zero (1982) by theoh on Thursday December 15, 2016 @06:53PM (#13189862)
Maybe you are being sarcastic, but surely the history and etymology of 0 really makes it ineligible to be used to name the first ordinal.

How can the "empty" placeholder value represent the presence of an item? It's just even more confusing than the existing situation.

Re:Why numbering should start at zero (1982) by cousin_it on Friday December 16, 2016 @06:02AM (#13192415)
The change is a bit mind-blowing, but in the end I really think it could work. Consider this:

"The first year of the third millennium begins at 2001/01/01 00:00:00"

Wouldn't this make much more sense:

"The zeroth year of the second millennium begins at 2000/00/00 00:00:00"

Re:Why numbering should start at zero (1982) by rogual on Friday December 16, 2016 @07:02AM (#13192673)
Not to mention it would eliminate the always-confusing "17th century == 1600s" thing.
Re:Why numbering should start at zero (1982) by cousin_it on Friday December 16, 2016 @07:41AM (#13192837)
Yeah. I expanded this to its own post: https://news.ycombinator.com/item?id=13192826
Re:Why numbering should start at zero (1982) by edblarney on Thursday December 15, 2016 @02:36PM (#13187496)
"There is nothing that inherently links "one" to "first", or "two" to "second"-"

Uhm, no, just the opposite.

Re:Why numbering should start at zero (1982) by ruraljuror on Thursday December 15, 2016 @02:46PM (#13187589)
To expand on my comment below, I assumed zero-based indexing was a natural extension from binary. When using bits (in this case three bits) as a representation, you start with 000. Naturally that would be referred to as 0.
Re:Why numbering should start at zero (1982) by zAy0LfpBZLC8mAC on Thursday December 15, 2016 @03:49PM (#13188253)
Well, yes, but: That ultimately is also an arbitrary definition, in a way. You could also build a computer where 000 would represent the integer 1. And the reason why that is not the case, but 000 instead represents the integer 0 is essentially what I said above: It's simpler to build a binary computer that computes 000 + 000 = 000 and 000 + 001 = 001 and 001 + 001 = 010, instead of one that computes 000 + 000 = 001 and 000 + 001 = 010.
Re:Why numbering should start at zero (1982) by JadeNB on Thursday December 15, 2016 @06:11PM (#13189571)
In this sense everything is an arbitrary definition—which is true, but makes arguments about naming sort of useless. (Perhaps the correct answer is that they are, and this is just revealing that ….) At a certain point the argument, if it is to be at all productive, must become not "what is the one and only true way to do this?"—there never is one for naming—but "what are some other conventions that might guide us to a well determined consistent, rather than arbitrary, convention in this case?"
Re:Why numbering should start at zero (1982) by zAy0LfpBZLC8mAC on Thursday December 15, 2016 @08:45PM (#13190416)
> In this sense everything is an arbitrary definition

Hmm, no, not really. While definitions are, indeed, by definition arbitrary, different concepts are of different usefulness, and as such, definitions that use more useful concepts can reasonably be considered better definitions of given terms.

There is no such advantage in using the binary 000 to represent the first index, though, except in so far as it results from the binary 000 representing the neutral element of addition, which corresponds to the natural number 0. The latter is the part that's actually not arbitrary.

> there never is one for naming

Well, the whole point kindof is that it's not about naming, but about arithmetic. If you only want to name stuff, you can use strings and hash tables, or letters of the alphabet, or whatever other set of symbols that you can come up with, it's really to a large degree a matter of taste. But there is a distinctive feature of arrays vs. other collection data types: You can do arithmetic on its indices. And arithmetic with a number type that doesn't have a zero is braindead.

Re:Why numbering should start at zero (1982) by ben_bai on Thursday December 15, 2016 @02:51PM (#13187641)
I just think of it as offsets. Array X at offset 0, which is the first entry.
Re:Why numbering should start at zero (1982) by andrewflnr on Thursday December 15, 2016 @02:50PM (#13187632)
Linking ordinals to cardinals is done by the same abstraction process that first led us to think of numbers as distinct objects. We noticed there was a property in common between the set of our five fingers and a pile of five sticks. And hey, it's not so different from the property of the fifth object in some sequence, so let's lump those together. They're formally different, but intuitively similar.
Re:Why numbering should start at zero (1982) by caf on Thursday December 15, 2016 @09:39PM (#13190651)
...but isn't the most salient point of the fifth object that there are 4 objects preceding it? (there could be any number of object after it, and it would remain the fifth).
Re:Why numbering should start at zero (1982) by belovedeagle on Thursday December 15, 2016 @11:10PM (#13191000)
To play devil's advocate -- and let's be clear, confusing ordinals and cardinals really is the devil's work ;) -- there are 5 items not after the fifth item.

The ridiculousness of that argument should be evidence enough that the fifth item is item 4.

Re:Why numbering should start at zero (1982) by andrewflnr on Friday December 16, 2016 @04:37PM (#13197087)
Only if you phrase it awkwardly like that. Adding the fifth element creates a set of five elements.
Re:Why numbering should start at zero (1982) by gshubert17 on Thursday December 15, 2016 @02:59PM (#13187728)
In my teaching I try to distinguish between conventional-language numbering by saying "first", "second", "third"; from zero-based indexing by saying "zero-th", "one-th", "two-th", and so on.

It's a bit awkward, but once explained it is a quick way to tell the two numbering schemes apart.

Re:Why numbering should start at zero (1982) by zAy0LfpBZLC8mAC on Thursday December 15, 2016 @03:33PM (#13188087)
Hmm, I dunno, I guess it might be a useful tool to explain the distinction? Not sure whether, in order to avoid long-term confusion, it would still be good to explain that, really, "first", "second", and so on, are the common words other people use for what you call "zero-th", "one-th", and so on?
Re:Why numbering should start at zero (1982) by sparky_z on Thursday December 15, 2016 @03:04PM (#13187787)
...unless you're working with indexes higher than 3. Or am I missing something?
Re:Why numbering should start at zero (1982) by smnc on Thursday December 15, 2016 @03:25PM (#13187980)
Perhaps they accentuate the difference via pronunciation or inflection. Otherwise I also fail to spot the difference.
Re:Why numbering should start at zero (1982) by caf on Thursday December 15, 2016 @09:36PM (#13190640)
You have convinced me to stop using the misleading abbreviations "1st", "2nd", ...
Re:Why numbering should start at zero (1982) by derefr on Thursday December 15, 2016 @02:52PM (#13187648)
I understand the mindset that creates this, but I have a different one, I think?

Consider the physical world. Let's say you've got three fenceposts (of unit width) in a line standing before you, each separated by 0.5 fencepost-widths of space.

It makes sense to say "the first fencepost." That's the post can be found in the (0.0, 1.0) region of the fence.

But "fencepost 0" or "fencepost 1" are both ambiguous to me: neither 0 nor 1 is inside the region the first fencepost occupies, physically. The mapping isn't intuitive. Both of those numbers point to where the fencepost isn't: just past its ends. Depending on your mindset, you might want to refer to the fencepost either to the left or the right in the row of that imaginary separating line: either "the fencepost that was just completed" or "the fencepost that is about to start" in your visual stream. Even worse if you're from a culture that reads right-to-left.

In physical terms, if you wanted to give "the first fencepost" an address... it'd be fencepost 0.5! Likewise, "the second fencepost" would be fencepost 2.0, and "the third fencepost" would be fencepost 3.5. These numbers refer to the centers of each fencepost. You don't have to ask the question of which "direction you head" to find the fencepost from the address: when you're at the address, you "have" a fencepost. You just need to know the origin and axes of the fence and you can "go to" the fencepost.

Now, we could get fencepost addresses of 0.0, 1.0, 2.0, and so on, by following some indexing strategy like "the position of the left edge of the Nth fencepost, measuring only fencepost-widths and not space between fenceposts." This kind of offset-based indexing is "intuitive" when talking about memory addresses, because memory addresses are like an ideal fence: every post is 1.0 units wide, with no space in-between posts. (Also, you can't actually point to the centre of an atomic unit of memory; memory addresses are quantized such that the only place the "handle" of a memory address can be attached to the memory itself is the "left edge"—the beginning—of the memory.) Great for digital logic, but we just don't have anything like that in physical space.

In reality, when we want indexes, we explicitly label things with numbers from a monotonic sequence, and then either write down mappings from the center-of-measured-interval addresses of the physical world (e.g. GPS coordinates) to those labels, or, worse, binary-search our items for one with the right address-label, hoping the labels have stayed monotonically sorted since they were issued.

There are nearly no cases where there is a natural "element zero" that exists at zero in the physical world, to create that associative intuition (perhaps, people crossing a finish line in a race: the winner is certainly "person zero" in the photo.)

Re:Why numbering should start at zero (1982) by zAy0LfpBZLC8mAC on Thursday December 15, 2016 @03:20PM (#13187934)
> But "fencepost 0" or "fencepost 1" are both ambiguous to me

Which is good, because it means that you are not confused :-)

The confusion is the idea that there is an inherent link between "1" and "first" or "0" and "first", which there isn't, because one of them is a label, while the other is an ordinal number.

You need a definition of how to map one to the other, and as long as you don't have that, the labels do indeed remain ambiguous, which you correctly recognize.

Now, there is an argument to be made for why array indexing should use a definition that maps the first element to label 0, and that is, as I wrote, that array indexing is an additive operation.

As for how that connects to your fence example: What you necessarily need in addition if you want to model your fence with an array(!) is a definition that maps your fence posts' positions to ordinal numbers, which you then can map to array indices via the definition above. That exact mapping is necessarily arbitrary to a degree, and the only really important thing is that you are consistent.

But within the implementation, you are likely to encounter problems such as "find the third fencepost of the part of the fence that starts at the second fencepost". And that would be the fourth fencepost of the overall fence, not the fifth, or in indices: 1 + 2 = 3 vs. 2 + 3 != 4.

> There are nearly no cases where there is a natural "element zero" that exists at zero in the physical world

Well, nor is there a natural "element one" that exists "at one".

Re:Why numbering should start at zero (1982) by derefr on Thursday December 15, 2016 @03:59PM (#13188332)
> Well, nor is there a natural "element one" that exists "at one".

Right. I'm on the "no natural mapping" side as well, here, not with the "first = 1" people.

Actually, consider for a moment, a programming language that tried to avoid both of these mappings, at least in its programmer-facing syntax (it might compile down to one or the other mapping, but that's an implementation detail.) It might not be so bad!

Of course, you wouldn't be able to index arrays by a single ordinal number at all, instead being forced to do speak in terms of intervals:

   letters = char{'A', 'B', 'C'}

   letters[0ch .. 1ch] # => ['A']
   letters[1ch .. 2ch] # => ['B']

   pairs = short{'AB', 'CD', 'EF'}

   pairs.slice(0sh .. 1sh) # => ['AB']
   pairs[0sh .. 1sh] # => 'AB'
   pairs[0ch .. 1ch] # => ['A'] or a type error, as the language author pleases
   pairs[0sh .. 2sh] # => argument error
   pairs.slice(0sh .. 2sh) # => ['AB', 'CD']
Effectively, the single-element index operation would just be a specialization of a slice operation. Both operations would take a mantissa indicating where the "left edge" and "right edge" containing the value you want is in the array's memory space. And you'd be forced to specify explicit units for the "addresses" of the mantissa, even though the language might reject a mantissa of the wrong type.

This seems kind of annoying to write. But, I swear, it feels so pleasant to read. Much moreso than any language I've used. It's not hard at all to "keep the logic in my head" when I read the above back.

Re:Why numbering should start at zero (1982) by zAy0LfpBZLC8mAC on Thursday December 15, 2016 @04:26PM (#13188569)
> pairs[0sh .. 1sh] # => 'AB'

['AB'], no? Or the letters without []?

> This seems kind of annoying to write. But, I swear, it feels so pleasant to read. Much moreso than any language I've used. It's not hard at all to "keep the logic in my head" when I read the above back.

Well, yeah, but you conveniently left out index arithmetic :-)

If it's only about hard-coded indices, it really doesn't matter, IMO, as long as it's consistent, and you could just use any ordered set of symbols for indexing, and let the compiler figure out the mapping to binary numbers and machine code that's functionally equivalent.

The interesting case is when you, as the programmer, want to do arithmetic with indices. Of course, you could have a separate number type for array indices, say. But there really is no way around making its algebraic behaviour isomorphic to 0-based indices, because 1-based arithmetic is hell, and if you do that, it's probably a good idea to use the established names such as "0" or "1" for the established concepts such as "neutral element of addition" or "neutral element of multiplication", in order to avoid confusion.

Re:Why numbering should start at zero (1982) by derefr on Thursday December 15, 2016 @05:04PM (#13188914)
> ['AB'], no? Or the letters without []?

Without. The dyadic arr[range] operator would always return one (unwrapped) element, a bit like how SQL ORMs frequently have a Query#get_one method to match Query#get_all. `letters` was a char[], `pairs` was a short[], so you're grabbing a short by its interval-address (+0 shorts from origin, +1 shorts from origin).

(Or, to clarify if you were confused by the not-quite-C pseudocode, by 'AB' I meant the short 0x4142.)

> But there really is no way around making its algebraic behaviour isomorphic to 0-based indices

Oh, sure, it's "zero-based arithmetic" because we're still fundamentally dealing with offsets (and when you slice the array, you take offsets from the beginning of the slice, etc.)

I think it's just better—in some vague human-factors sense—to be forced to mention, in each indexing operation, both where the offset (position of hyperplane separating the element from its previous) and the offset+size (position of hyperplane separating the element from its successor) will land, so that you realize explicitly what two numbers those are in your program, as you write them.

Humans—especially beginner programmers—don't intuitively realize that array indexes are offsets. They just want to talk about cardinal-numbered positions, and find themselves unable to, so they guess at a mapping and make mistakes.†

This syntax sort of short-circuits that by giving them an indexing operation that doesn't ask them to decide on any cardinal:ordinal mapping at all. It just asks for two numbers that are clearly "offsets of the positions between array elements", rather than offsets (or counts) of elements.

But even for experienced programmers who understand that indexing operations are pointer offset math, such an indexing-by-continuous-slicing scheme is potentially still a bit helpful, somewhat like being asked to write out a deductive proof for a hand-wavy argument: it makes you aware of when you've made a leap of logic that has no rule allowing you to make it. Or, in this case, it makes you aware of what element you're actually capturing with your hand-wavy math.

> The interesting case is when you, as the programmer, want to do arithmetic with indices.

I think—and this I find even weirder to say than my first thought—that it might be good to restrict this "humane" indexing operation from containing compound expressions.

Instead of this:

   foo[(a * 3 + 2)ch .. (a * 3 + 5)ch]
...you'd be forced to write this:

   offset = a * 3 + 2
   foo[(offset)ch .. (offset+1)ch]
or maybe the x86 LEA-alike:

   pos = a * 3 + 2
   foo[(pos)ch : 0ch..1ch]
This forces the programmer to give a name to the number they're using as the "base" in the indexing operation—which pushes them even further toward noticing when they've borked up the fencepost math.

---

† A tangent: "pointers are hard" for beginning programmers because C tries very hard to make array access, and even plain pointer math, "act like" reference-by-cardinal-rank, but then still uses zero-based offsets, munging up the ability to deduce a coherent semantics of pointers as memory-address-valued variables.

In C, when you add an (ordinal!) integer to a pointer, it multiplies that integer by the sizeof() the pointer's referent type because it wants you to model the pointer as being an iterator-handle for a collection of things, rather than just being a memory-address-valued variable. It makes sense, for practical reasons, that you can't create a short︎*-typed variable containing a non-short-stride-aligned memory-address... but that should be a domain error on the result of the addition, not something you're mathematically unable to express.

Re:Why numbering should start at zero (1982) by zAy0LfpBZLC8mAC on Thursday December 15, 2016 @08:31PM (#13190361)
> But even for experienced programmers who understand that indexing operations are pointer offset math, such an indexing-by-continuous-slicing scheme is potentially still a bit helpful, somewhat like being asked to write out a deductive proof for a hand-wavy argument: it makes you aware of when you've made a leap of logic that has no rule allowing you to make it. Or, in this case, it makes you aware of what element you're actually capturing with your hand-wavy math.

Well, I'm not convinced, but who knows? My guess would be that it wouldn't really reduce mistakes much. People would simply memorize the rule that for a single element, you always have to give some index and then follow that by index + 1 --and if the index is computed incorrectly, the result is just the same wrong result as before. You cannot force people to prove that the offset is correct by forcing them to write down the expressions for two values that have to always differ by exactly 1.

> In C, when you add an (ordinal!) integer to a pointer, it multiplies that integer by the sizeof() the pointer's referent type because it wants you to model the pointer as being an iterator-handle for a collection of things, rather than just being a memory-address-valued variable. It makes sense, for practical reasons

Nah, I think that's perfectly sensible, even theoretically. A pointer is a perfectly sensible memory-address-valued variable, it's just that it's not in "machine code addresses", because that is an implementation detail, but rather in "abstract address space addresses", with a separate address space per object (by which I mean an allocated memory region), and obviously, with arithmetic in that abstract space counting in elements of that respective address space's type. Why would counting in bytes make any more sense? The size of a byte (or a char, rather) is mostly an arbitrary implementation detail that abstract code shouldn't be bothered with.

BTW, what you write is kindof wrong: As per the abstract definition of the language, nothing is being multiplied by sizeof()s. That tends to be how it's often implemented on the usual platforms, but the abstract definition simply says that arithmetic with pointers works in units of elements, and that consistently: If you subtract pointers, you get the number of elements between them, not the number of bytes/chars/octets/words/whatever between them, the internal value is not really visible anywhere, and could relate to the abstract value in arbitrarily complicated ways anyway (like, you could have pointers that are implemented with tags to differentiate pointers to different machine address spaces and stuff like that).

> that you can't create a short︎*-typed variable containing a non-short-stride-aligned memory-address...

That's actually not the goal nor the case. Whether a pointer is aligned is an implementation detail, there is nothing in C that would require short objects to be aligned to short boundaries, and what implementations do simply depends on (a) whether the platform can handle non-aligned pointers, and (b) if it can, what makes sense as a matter of weighing performance vs. memory usage. I haven't checked it, but my guess would be that the AVR GCC, for example, does not align anything, because it only needs more memory and makes no difference whatsoever for performance (I think ... maybe I am forgetting something).

Re:Why numbering should start at zero (1982) by goatlover on Thursday December 15, 2016 @04:13PM (#13188446)
This sounds like sophistry. There are no 0 things in the world, it's just an abstract concept. I have 1 car, not 0. If I had a lineup of cars, the first one would be 1, not 0.
Re:Why numbering should start at zero (1982) by zAy0LfpBZLC8mAC on Thursday December 15, 2016 @04:33PM (#13188639)
> There are no 0 things in the world, it's just an abstract concept.

So, empty water bottles don't exist? Or do you object to saying "this bottle contains 0 ml of water"? Why? Or do you think that no water bottle is the same thing as a water bottle with no water in it?

> I have 1 car, not 0.

And what about someone who has no car? Are there no people who don't have cars? Or do you object to saying "this person has zero cars"? Why? Or do you think that no garage is the same as a garage with no car in it?

> If I had a lineup of cars, the first one would be 1, not 0.

No, it wouldn't. Unless you arbitrarily decided to label it as such. It would be the first. That's an ordinal number. Also, you would have 1 car. That's a cardinal number.

Re:Why numbering should start at zero (1982) by goatlover on Friday December 16, 2016 @03:56PM (#13196729)
>> So, empty water bottles don't exist? Or do you object to saying "this bottle contains 0 ml of water"? Why?

They exist conceptually, because a water bottle could be empty of anything that fits inside. We say it has 0 ml of water and not coke or sand because we've designated it to be a water bottle, but nature obviously doesn't care what goes in the bottle, as long as it fits. It's just a useful idea we have to say there is zero something to designate a lack of something. But there isn't zero water in nature, anymore than there are zero unicorns, or zero Kings of France.

>> And what about someone who has no car? Are there no people who don't have cars? Or do you object to saying "this person has zero cars"? Why? Or do you think that no garage is the same as a garage with no car in it? <<

Are there no people who don't have dinosaurs or time machines? The things we have zero of are infinite. But you don't actually have zero pink elephants in your backyard.

>> No, it wouldn't. Unless you arbitrarily decided to label it as such. It would be the first. That's an ordinal number. Also, you would have 1 car. That's a cardinal number.

So if we were to label to cars in increasing numeric order from one end to the other, we would start with 0 instead of 1 at the first car? We don't do that with building levels. You enter on the first floor, go to the elevator, and you push a button for the floor level. The first floor is 1. 0 Would be the basement level.

Re:Why numbering should start at zero (1982) by Nadya on Friday December 16, 2016 @05:27PM (#13197466)
I feel zAy0LfpBZLC8mAc responded to the rest of your post fine - so I'm only going to add my own opinion regarding your last statement:

>So if we were to label to cars in increasing numeric order from one end to the other, we would start with 0 instead of 1 at the first car? We don't do that with building levels. You enter on the first floor, go to the elevator, and you push a button for the floor level. The first floor is 1. 0 Would be the basement level.

We opted for the word "Ground Floor" instead of "Zeroth Floor" likely due to the associations people make of "zero" (ie. not existing) and because it may have sounded weird but the concept is the same.

The U.K and U.S refer to different storeys as the "first floor". In the U.S, the first floor is considered the ground floor and in the U.K the first floor is considered the second storey. A source of headaches when told the place you're looking at is on the first floor and you spend ages looking for it on the ground floor.

Re:Why numbering should start at zero (1982) by zAy0LfpBZLC8mAC on Friday December 16, 2016 @05:18PM (#13197410)
> It's just a useful idea we have to say there is zero something to designate a lack of something. But there isn't zero water in nature, anymore than there are zero unicorns, or zero Kings of France.

That's just confusion on your part. All of language is just a useful idea we have to designate whatever we designate with it, and you are just confusing things for no reason at all. You obviously understand what "zero something" means, and that what it means is indeed something that can describe aspects of reality, and then you go on and claim that somehow it doesn't. When I say "there are 0 ml of water in this bottle", you understand what it means, right? And if there is indeed no water in the bottle, what is there to be confused about? It does mean that there is no water in the bottle, but it also means something else at the same time? Or there is no water in the bottle, but there is also not no water in the bottle?

> The things we have zero of are infinite. But you don't actually have zero pink elephants in your backyard.

Yes, I actually have. "having zero <whatevers>" is, as you evidently know, a synonym for "having no <whatevers>" or "not having <whatevers>", and I absolutely certainly actually do not have pink elephants in my backyard.

> So if we were to label to cars in increasing numeric order from one end to the other, we would start with 0 instead of 1 at the first car?

There is no such thing as "we would start". You can start wherever you want. That is, as derefr has already explained to you, an exercise in assigning serial numbers. You can use whatever numbering scheme you like. Obviously. Some might be more useful than others, though.

> We don't do that with building levels. You enter on the first floor, go to the elevator, and you push a button for the floor level. The first floor is 1. 0 Would be the basement level.

Except you don't enter on the first floor. The "first" thing in any order is defined to be the thing without a predecessor. Obviously, if there is a basement, the ground floor is not the first floor, because the basement precedes it. Just because the ground floor is commonly called the "first floor" for historical reasons, doesn't make it the mathematically first floor.

Re:Why numbering should start at zero (1982) by dragonwriter on Friday December 16, 2016 @05:21PM (#13197430)
> We don't do that with building levels. You enter on the first floor, go to the elevator, and you push a button for the floor level. The first floor is 1.

In some places (e.g., most of North America), that's the convention. In others, the "first floor" is the one immediately above the ground floor.

Re:Why numbering should start at zero (1982) by derefr on Thursday December 15, 2016 @04:37PM (#13188679)
You're getting confused between cardinal and ordinal numbers.

Ordinal numbers (0, 1, 2) map to the world as amounts, and certainly it seems 'real' that there can be "one of something" in a way that it isn't that there is "zero of something" (though it isn't, really.)

Cardinal numbers (first, second, third) map to the world as rankings or positions. Positions, further, often almost always map to distances from an origin. "First" really almost always means "the item that is +0 positions away from the origin." The person who wins a competition ("first place") is zero positions down the leaderboard. The "first" book on your book shelf can be found +0cm away from the left edge of your bookshelf. Etc.

With your cars, you're talking about a third thing, though: serial numbers. "Car 1" is what you might call the first car you bought (if you were planning on buying several), because we conventionally assign serial numbers to things starting with 1. But that has nothing to do with counting or ranking; it's just a convention (as can be seen when we choose to use an alternate scheme, like UUIDs, for labelling items in a database.)

What we're talking about here is the relationship between counting and ranking: between cardinal and ordinal numbers. Since they're both abstract, and not structurally connected in any important way, there's actually no "essential, required" way to relate them to one-another, and we have to just make one up. Some people relate "first" to the natural number 0; some people relate "first" to the natural number 1.

Re:Why numbering should start at zero (1982) by anigbrowl on Thursday December 15, 2016 @02:32PM (#13187452)
There is nothing that inherently links "one" to "first", or "two" to "second"

$10 says you don't have any children.