6 ms·
> We usually start to count from one. When a baby completes its first roundtrip around the Sun, they’re one year old, not zero years old. And before they compl
by tromp 4y ago
> We usually start to count from one. When a baby completes its first roundtrip around the Sun, they’re one year old, not zero years old.
And before they complete the first trip, they're 0 years old, showing that you start from a count of 0. The one is the result from the first increment.
If I ask you to count the number of red balls in a bag with only 3 yellow balls, then the initial count in your head is 0, you inspect the balls one by one, never encountering a red ball, and thus never incrementing the count. And then you pronounce your final count of 0. So that's counting starting from 0.
What you call "starting at 1" is not so much the start as it is the first increment, which need not arise.
For arrays, the first element is the difference between a size 0 array and a size 1 array. So the choice between 0-indexed and 1-indexed is like the difference between pre-increment and post-increment. Do we associate the element with the array size up to the element, or to the array size up to and including the element? The choice seems arbitrary
(personally I prefer using the same range of non-negative numbers for both, but language has a bias for associating "first" with index 1).
- ghosty141 4y agoWhile I agree about the points you made, you cant take the zeroth element out of your shopping bag, you take the first one. The array indexing operator gives you access to the nth element of your data store. IMO OPs point is valid.
- smaudet 4y agoThe point is valid, but the rationale is not, let me explain: Caring about 0 based or 1 based indexing is, to me, a sign of someone who struggles with programming in general, or is stuck doing a lot of finicky conversion between the two. Most modern, higher level languages have generally abandoned using indexing, instead (even c++) they have something like: "for x in y do z" 1-based indexing is a bit more readable, but in the end, you have hardly any benefit. There are better paradigms, algorithms, which don't require indexing at all, which IMO is the majority of what programmers are doing anyways. Even if you need to process two same-size collections at once (the majority of the remaining legitimate uses for index based looping), you are likely working pre-sorted data and should consider using a zip or pair which eliminates the need for managing indexes. You say "but aren't you just imposing your own style on others"? Not really, if we want clean, minimal code, there should be as few references to the underlying architecture as possible, even the fact that we are dealing with a list is an implementation detail (is it actually a list, a linked list, a stream, a dictionary, an event, etc...), so in the context of implementing a higher-level language, this point is not only irrelevant, but shortsighted. If you are creating and looping over lists, you are likely not doing anything interesting, which is the whole point of programming in higher level languages, to do interesting things simply, right? As for the remaining use case when we do actually want array based access, typically you find this in high-performance, architecture aware applications - then we actually want random memory access. We may even want to deal with explicit memory offset, which is what 0-based arrays are good at doing (often times the array is syntax sugar and we are literally assigning to/from pointer offsets). To bring this around to my previous statement: "Caring about 0 based or 1 based indexing is, to me, a sign of someone who struggles with programming in general, or is stuck doing a lot of finicky conversion between the two." The reason this comes up in the first place is that there is a divide. 0-based arrays are arguably much better for low level activities, and higher level languages generally left them in as a familiarity. Translating from 1 to 0 based arrays is not any easier than translating from 0 based to 1 based arrays, 1-based arrays provide just as much confusion and less familiarity in these cases. This is not a good thing. Now I suppose Lua is trying to be something weird, a "high level" low-level language. Maybe that's fine, but it is weird, and people are right to be put-off by the change. If you are looping over bananas in lua, maybe you're not really using it the way it was intended, if you are doing memory-level access then its unnecessary language cruft.
- __ryan__ 4y agoYou created an unfair definition of the array indexing operator. I can just as easily say that the array indexing operator gives you access to the element at index I, which starts counting at 0. That's not an argument.
- adrian_b 4y agoThe sequence of the cardinal numbers, i.e. of the equivalence classes of sets having the same number of elements, is 0, 1, 2, 3 and so on and those words in any language have been used originally for cardinal numbers, not for ordinal numbers. For the sequence of the ordinal numbers, whose purpose is to identify the position of the elements of an arbitrary sequence, any sequence of arbitrary symbols may be chosen and fixed by convention. Most languages had at least in the beginning special words for the first and for the second elements of a sequence, without any relationship with the cardinal numbers. Even in English that remains true, even if "second" is a more recent substitution of the older word used previously. Many languages have special words for the last element and for the element before it. Some languages had special words for the third element and for the third element going backwards from the last. So in some languages it was possible to identify the elements of a 6-element sequence without using words derived from the cardinal numbers. However inventing a very long sequence of words to be used as ordinal numbers, in order to identify positions in sequences with more than 2 to 6 elements, would have been difficult, so in most languages someone noticed that there already is a sequence of words that everybody had to memorize when learning how to count and which had rules for being extended to any length. So the ordinal numbers were derived by using a suffix or some other derivation rule from the cardinal numbers. There is no logical reason for using 1 for the first ordinal position, this is just a historical accident. The reason is that the children have always been taught to count by saying 1, 2, 3 and so on, instead of being taught to recite the sequence of the cardinal numbers from zero. All the languages have always had a word for zero, but those words were normally created by applying a negation to words meaning "something", "one" or the like. Because of this, the words for zero were not perceived as having an independent meaning and there was no need to learn them separately when the recitation of the cardinal numbers was learnt. Nowadays we have a much better understanding of the meaning of the cardinal numbers and we are aware that 0 is a cardinal number like any other, so the children should really be taught to count 0, 1, 2, 3 ... and not 1, 2, 3, ... like 5000 years ago. In the natural languages there is a huge inertia. Even if one would decide that since tomorrow the ordinal numbers should be 0th, 1th, 2th, 3th, 4th and so on, everybody would still have to know that whenever reading older writings the sequence of the ordinal numbers was 1st, 2nd, 3rd, 4th and so on, so a change of the convention to a more logical one would bring no simplification. On the other hand, in the programming languages you can ignore the legacy conventions and choose the best conventions. Using for ordinal numbers the sequence 0, 1, 2, 3 ... is the best choice for many reasons, which have been explained in the literature many times, e.g. by Dijkstra. Choosing to start the ordinal numbers from 1 in a programming language just demonstrates a lack of understanding of what the cardinal numbers and the ordinal numbers are and a lack of practical experience in programming and of understanding of how the programming language will be translated into machine language. The first programming language for which I have studied the machine code generated by its compiler, when I was young, happened to be Fortran, which uses indices starting from 1. Until today I remember how I considered ugly and error prone all the tricks that the compiler was forced to use in order to avoid in many cases to make extra computations due to the poor choice of the origin of the indices.
- jcelerier 4y ago> While I agree about the points you made, you cant take the zeroth element out of your shopping bag, you take the first one. where does the first centimeter / inch start in a physical ruler ?
- thunky 4y agoYou're both right. You want to measure the distance from a starting point (zero), the person you're replying to wants to count/label things from a starting point (one). The distance method doesn't work as well for picking items from a bag, and the counting method doesn't work as well for calculating offsets. They can both work for arrays, so neither is wrong.
- topaz0 4y agoI'm mad at myself for getting dragged into this, but. Leaving aside the fact that array indexes are not continuous in the way ruler measurements are, what does it say that you just called it the 1st inch, not the 0th?
- jcelerier 4y ago> Leaving aside the fact that array indexes are not continuous in the way ruler measurements are, I have always seen a ruler as a discrete sequence of centimeters even if the physical object is continuous > what does it say that you just called it the 1st inch, not the 0th? but it's literally what I'm saying aha. The index of the first element is zero, even on basic household objects.
- topaz0 4y ago> I have always seen a ruler as a discrete sequence of centimeters even if the physical object is continuous I have trouble believing this. Does this mean you think of a ruler as a tool to assign lengths (continuous) to discrete intervals, rather than as a tool to (perhaps imprecisely) measure the (continuous) length.
- 4y ago
- zdragnar 4y agoNobody says that a baby is 0 years old. They use other words instead of numbers, or if they do use a number, it is X hours / days / weeks / months old. 0 in normal usage is always a negation of something existing. You don't start counting things that exist at 0 (as you pointed out in your example of number of red balls in a bag that have none). However, position zero at an array in zero-indexed languages is the first position within the array. There's no getting around the fact that higher level languages inherited 0 by counting byte offsets, rather than positions, even if those languages don't required that every index within an array have the same size in memory (or occupy a contiguous block of memory), and that this inherited usage is at odds with the actual plain-language and true meaning.
- mfost 4y agoOf course they don't. They use decimals and say the baby is one week old or so. As for indexing from zero, it's best understood as indexing the position just before elements in an array. Where things will be inserted if you insert an element etc...
- zdragnar 4y agoSo, to use some pseudocode: var item = my_array[2] should be read as "assign to item the value in my_array in the position where you would place something new just before the third item" 1-indexing is, strictly speaking, a better design. The only real argument for it anymore in a higher level language is "it's what I'm used to", or alternatively, "0 already won the popularity contest" (which is actually not a terrible argument in context, though it is a bad one from a purely objective design perspective).
- mfost 4y agoSaying it's strictly better is exaggerated. The benefits of 0 indexing are well known and mostly revolve around the fact you tend to have far less fiddling with -1 and +1 as you write code. How do you access the item in a 2D array represented on a flat array? my_array[x+ywidth] But what is all that starts at 1? my_array[x+(y-1)width] I did a lot of code in both systems and I remember distinctly that 1.. indexing just was more work to do, in turn bringing in more off by 1 bugs.
- changoplatanero 4y agoExcept in Korea, where babies are considered to be age 1 starting at birth! https://en.wikipedia.org/wiki/East_Asian_age_reckoning#Korea https://en.wikipedia.org/wiki/East_Asian_age_reckoning#Korea
- shoemakersteve 4y agoI wonder if it has something to do with a cultural perception of how life and the world in general is perceived. As in, "He's currently experiencing his first year of life" vs. "He has lived for one year", kind of "the journey" vs. "the destination". Seeing the present more than looking back the past. In the same line of thinking as "What you're doing" vs. "what you've done", or maybe even "making progress towards goals" rather than "accomplishments". Seems like a healthier and maybe even more productive way to see the world. I don't know enough about Korea to say whether that has anything at all to do with the way they count ages, but I feel like it's an interesting thought regardless.
- topaz0 4y ago1-based vs 0-based indexing is such a tired controversy.
- sph 4y agoAnd a solved problem. Pretty much all the most popular languages have settled on zero-based indexing, who the hell cares what's more "correct". What's closer to what programmers expect is a better choice. Being pedantic about "1 is actually better" helps no one. It's the age old theory vs practice problem. Physicists vs engineers. Etc. This is why no one really cares about tau either.
- ok_dad 4y ago0 based indexing was arbitrarily chosen due to the memory model of old programming languages.
- Joker_vD 4y agoIf you have an array A with indices from S to T, you can use as its base address the address of A[S] minus S multiplied by sizeof(A[S]), then your address calculations are the same as with 0-based arrays. So I'd say it's a problem of a memory model of one old programming language, that is, C.
- nocman 4y agoI strongly disagree. The decision was not in the least bit arbitrary. Language designers chose zero-based indexing because they believed its advantages outweighed the advantages of one-based indexing. And it still does, in my opinion. In the end, no matter how high level your programming language is, you'll eventually be computing offsets to a base address in RAM using indexes that are zero-based. Having that be consistent at every level has great value.
- ok_dad 4y agoIt was arbitrary in that they chose to use a leaky abstraction to make it easier for themselves to make arrays using their memory model. The first person to do this could also have subtracted one from the user’s index to get the memory offset.
- karmakaze 4y agoI'll trust math more than human language evolution as a solid basis for indexing. Also Julia's (default) 1-based indexing has become a mess.
- nequo 4y ago0-based indexing is not universal in math either. We often index time from 0, but when we refer to components of a vector, we often index those from 1. For example, I'd write or see things like $\sum_{k=1}^N f(x_k)$ where $x$ is a vector. In these cases, 0-based indexing would look out of place.
- jhbadger 4y agoPretty much all programming languages designed for math are 1-based. Besides Julia, there's R, Mathematica (or "Wolfram language" as it is called these days), and Matlab.
- systems 4y agoI am curious, what mess did 1-based indexing create for Julia ???
- Jtsummers 4y agoMy understanding (outside the community, I use it but am not hugely familiar with it) is that the 1-based indexing is the default, but you can select any range. Routines that assume 1-based indexing then break on anything that, well, isn't 1-based. See: https://docs.julialang.org/en/v1/devdocs/offset-arrays/ https://docs.julialang.org/en/v1/devdocs/offset-arrays/ Discusses how to generalize the code so it handles arbitrary ranges.
- ModernMech 4y agoIMO 1 based and 0 based is a wash as to which is better, but the only definitive wrong answer is an arbitrary base. It may seem like a good compromise (just use whichever you prefer!), but it opens the door to a mars climate orbiter disaster waiting to happen.
- aidenn0 4y agoI have no problems with people advocating from 1-based indexing, and I often disagree with EWD, but advocating for 1-based indexing with out referencing EWD's reasonably well known take[1] is going to damage the ethos of your argument. 1: https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E...
- f1shy 4y agoThank you so much. I love the end: "In corporate religions as in others, the heretic must be cast out not because of the probability that he is wrong but because of the possibility that he is right."
- jrm4 4y agoI can't believe people are still seriously trying to argue this. You count "things." You don't count "not things." Arrays are containers of things. Starting with 1 is overwhelmingly likely the thing that makes sense when using them.
- shoemakersteve 4y agoArrays aren't just used for counting though. For example, the origin in cartesian coordinates is "0,0" and not "1,1". Not to say that I disagree, I don't think I've come across a situation where 0-based indexing would have been better for me personally. But there are definitely a lot of situations where I can imagine it would be preferable, like pretty much whenever you're doing math with them.
- programmer_dude 4y agoI'd say containing things is just one function of arrays. The other arguably more important function is "naming" things using numbers.
- jrm4 4y agoSure, but it seems like the latter isn't hurt by 1 indexing either, especially if the "names" are arbitrary, and if not, use associative?
- dbtc 4y ago"Give me the 1st item" --> a[1] "give me the 2nd item" --> a[2] "how many items are in the array?" 5 "what is the index of the last item?" 5 I am used to 0-indexed and prefer consistency... yet there is an undeniable consistency to 1-indexed. You're the person who brings back a dozen bottles of milk from the store when you see that they have eggs ;)
- tromp 4y ago> "what is the index of the last item?" 5 Usually, the more interesting question is: What is the index of the next item (when extending the array)? Which is 5 in 0-based indexing.
- shoemakersteve 4y agoIs it? If you're doing low-level stuff maybe, but I really haven't done much of that so I'm not even sure I know what you mean by "extending" an array (presumably has something to do with allocating contiguous memory?) and I can't actually think of any situation off the top of my head where I would ever need "the last index + 1" what with array methods existing and all.
- wielebny 4y ago> If I ask you to count the number of red balls in a bag with only 3 yellow balls, then the initial count in your head is 0, you inspect the balls one by one, never encountering a red ball, and thus never incrementing the count. And then you pronounce your final count of 0. So that's counting starting from 0. Well, just like lua: > a={} > print(#a) < 0
- deleted 4y ago[deleted]
- brokenkebab2 4y ago>If I ask you to count the number of red balls in a bag with only 3 yellow balls, then the initial count in your head is 0, Sorry, no. Humans count from 1. That's just a basic fact reflected in the history of numbers, which at early stages often didn't treat 0 as a number, but as a special case. And if you would say "I counted zero red balls" most people around will find it an unusual wording. Normal way of saying it doesn't involve mentioning 0 at all: "It's empty", "There are no red balls" etc.
- WorldMaker 4y agoThat special casing in English of no/none/empty is as much an artifact of lost germanic grammar cases in English as it is anything "natural" or inherent to how English speakers count.
- brokenkebab2 4y agoI'm a member of multilingual family, and I'm inclined to insist it's not about Germanic grammar cases, because it's true for non-Germanic languages as well.
- WorldMaker 4y agoI just went "nearest ancestor up the stack" as a short hand, because the evolution of languages is a huge tree and a lot to talk about. If we want to get into it deeper, Proto-Indo-European had some truly fascinating grammar cases from what we think we've reconstructed of them. Most of the stuff that PIE did seems like "natural laws" simply because of how many modern languages we regularly see branched from it and how deeply rooted a tree in the language forest it is. But then we also have had chances to study non-PIE rooted languages and the "universals" are fewer than we think they are.
- brokenkebab2 4y agoI mentioned history of numbers, and it starts not from PIE speaking peoples, so I'm a bit lost as for what exactly is your point.