8 ms·
> 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 langua
by zAy0LfpBZLC8mAC 10y ago
> 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.
- anigbrowl 10y agoThere is nothing that inherently links "one" to "first", or "two" to "second" $10 says you don't have any children.
- jstimpfle 10y agoOf 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.
- zAy0LfpBZLC8mAC 10y agoWell, 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.
- cousin_it 10y agoI 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.
- theoh 10y agoMaybe 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.
- cousin_it 10y agoThe 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"
- jstimpfle 10y agoSo 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.
- edblarney 10y ago"There is nothing that inherently links "one" to "first", or "two" to "second"-" Uhm, no, just the opposite.
- ruraljuror 10y agoTo 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.
- ben_bai 10y agoI just think of it as offsets. Array X at offset 0, which is the first entry.
- zAy0LfpBZLC8mAC 10y agoWell, 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.
- JadeNB 10y agoIn 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?"
- zAy0LfpBZLC8mAC 10y ago> 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.
- 51Cards 10y agoI 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.
- zAy0LfpBZLC8mAC 10y ago> 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.
- andrewflnr 10y agoLinking 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.
- caf 10y ago...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).
- belovedeagle 10y agoTo 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.
- andrewflnr 10y agoOnly if you phrase it awkwardly like that. Adding the fifth element creates a set of five elements.
- derefr 10y agoI 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.)
- gshubert17 10y agoIn 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.
- sparky_z 10y ago...unless you're working with indexes higher than 3. Or am I missing something?
- smnc 10y agoPerhaps they accentuate the difference via pronunciation or inflection. Otherwise I also fail to spot the difference.
- zAy0LfpBZLC8mAC 10y agoHmm, 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?
- caf 10y agoYou have convinced me to stop using the misleading abbreviations "1st", "2nd", ...