5 ms·
1 or 0-based index... I recently picked up Lua for a toy project and I got to say that decades of training with 0-based indexes makes it hard for me to write c
by barotalomey 2y ago
1 or 0-based index...
I recently picked up Lua for a toy project and I got to say that decades of training with 0-based indexes makes it hard for me to write correct lua code on the first try.
I suppose 1-based index is more logical, but decades of programming languages choosing 0-based index is hard to ignore.
- ginko 2y ago>I suppose 1-based index is more logical I wouldn't say it's more logical. More intuitive perhaps.
- adrian_b 2y agoIt is not more intuitive. It just matches the convention used in the language that one has learned as a child, so one is already familiar with it. The association between ordinal numbers and cardinal numbers such that "first" corresponds to "one" has its origin in the custom of counting by uttering the string "one, two, three ..." while pointing in turn to each object of the sequence of objects that are counted. A more rigorous way of counting is to point with the hand not to an object, but to the space between 2 successive objects, when it becomes more clear that the number that is spoken is the number of objects located on one side of the hand. In this case, it becomes more obvious that the ordinal position of an object can be identified either by the number spoken when the counting hand was positioned at its right or by the number spoken when the counting hand was positioned at its left, i.e. either "0" or "1" may be chosen to correspond to "first". Both choices are valid and they are mostly equivalent, similarly to the choice between little-endian and big-endian number representation. Nevertheles, exactly like little-endian has a few advantages for some operations, so eventually it has mostly replaced big-endian representations, the choice of "0" for "first" has a few advantages and it is good that it has mostly replaced the "1 is first" convention. For people who use only high-level programming languages, the differences between "0 is first" and "1 is first" are less visible, exactly like the differences between little-endian and big-endian. In both cases the differences are much more apparent for compiler writers or hardware implementers. Besides "1 is first" vs. "0 is first" and little-endian vs. big-endian, there exists another similar choice, how to map the locations in a multi-dimensional array to memory addresses. There is the C array order and the Fortran array order (where elements of the same column are at successive addresses). Exactly like "1 is first" and big-endian numbers match the conventions used in writing the European languages, the C array order also matches the convention used in the traditional printed mathematical literature. However, exactly like in the other 2 cases, the opposite convention to the traditional written texts, i.e. the Fortran array order is the superior convention from the point-of-view of the implementation efficiency. Unfortunately, because much less people are familiar with the implementation of linear algebra than with the simpler operations with numbers and uni-dimensional arrays or strings, so they are not aware about the advantages and disadvantages of each choice, the Fortran array order is used only in a minority of programming languages. (An example of why the Fortran order is better is the matrix-vector product, which must never be implemented with scalar products as misleadingly defined in textbooks, but with AXPY operations, which are done with sequential memory accesses when the Fortran order is used, but which require strided memory accesses if the C order is used. There are workarounds when the C order is used, but with the Fortran order it is always simpler.)
- slightwinder 2y agoI have a similar experience with pythons negative-indexing. In Python, you can access elements counting from the back by using negative numbers. But for this, they start with 1, not 0. Which is inconsistent, as they start for the normal forward indexing at 0. I guess it comes from reducing n.length-1 to -1, but it's still kinda annoying to have two different indexing-systems at work.
- pansa2 2y agoIt makes more sense if you think of indices as pointing between elements: 0 1 2 3 4 ----------------- | A | B | C | D | ----------------- -4 -3 -2 -1 -0 Except, of course, -0 doesn't exist. AFAIK that's why C# chose to add a special "index from end" operator, `^`, instead of using negative indices.
- Someone 2y ago> Except, of course, -0 doesn't exist. Not for integers on modern hardware. If only hardware used ones’ complement (https://en.wikipedia.org/wiki/Ones'_complement)… https://en.wikipedia.org/wiki/Ones'_complement)… ;-) Meanwhile, the workaround is to use -1 through -5 to index from the end of the array.
- kragen 2y agoThat makes sense; in the diagram of the 4-element array, 3 points at the same point as -1.
- deleted 2y ago[deleted]
- slightwinder 2y agoYour visualization makes sense if you always count them going from left to right. But with negative index you naturally count them going from right to left, from the last element backward to the first element. So -0 is the natural starting-point, except it's -1 in python.
- booleandilemma 2y agoThis was the thing that turned me off from Lua unfortunately.
- dxuh 2y agoI don't use Lua as much anymore, but there were a few years where I used Lua and C++ both daily and very quickly you can easily handle both zero and one-indexing, even while switching between languages frequently. As with most things it's just practice.
- pansa2 2y ago> decades of programming languages choosing 0-based index is hard to ignore Yes - many file formats also work with zero-based indices, not to mention the hardware itself. One-based indexing is particularly problematic in Lua, because the language is designed to interoperate closely with C - so you're frequently switching between indexing schemes.
- School-Cotton 2y ago> I suppose 1-based index is more logical Why?
- InsideOutSanta 2y agoIt's interesting how most people find learning 0-based indexes confusing, but after a few years of programming, they don't even notice how odd it is. How do you number things in real life? If you have two options, do you number them "option 0" and "option 1" or "option 1" and "option 2"? If you create a presentation with numbered bullet points, do you start numbering them from 0 or 1? 1. This is my first point 2. This is my second point It would be odd to have your first point be point number 0 and your second point be point number 1, wouldn't it? Outside of programming, even most programmers use zero-based indexing only when they're making a programming joke. Zero-based indices create odd effects even in programming, although we don't really notice them anymore. Consider: $array := ['a','b','c'] for($i:=0; $i<3; $i++) { echo($array[$i]) } $array has 3 entries, but your for-loop says you should stop iterating before you reach 3. This isn't really consistent with how we intuitively understand numbers to work outside of programming.
- sim7c00 2y agoif you see how its implemented in machine code (on most modern archs) you will see how it is logical. once you see that, you cannot unsee it. people call these things an index, but really it is an offset from the base of the array. not an index into it. hence, base + 0 is the first entry. as the offset to that entry is 0. thats how it will work in generated machine code on all the machines i saw (i did not see all of them obviously). i think people struggle with these concepts because they never bother to see whats under the hood. a bit of assumption ofc on my part i cant see into ppls minds nor do i know all architectures. x86 and x64 (amd/intel) definitely work like this.
- InsideOutSanta 2y agoYes, it's the offset from a pointer in some languages. But to the user, it's presented as an index, not an offset. In normal syntax, you don't access an array value using a construct like *(arraypointer+offset) If you did that, then using 0 as the start offset would make intuitive sense. In fact, if not for this technical reason, I doubt that any programming language would have 0-based indices.
- kragen 2y agoInterestingly, it also poses great challenges for LLMs. GPT-4 can translate Perl into Python almost flawlessly, but its Lua is full of off-by-one errors.
- barotalomey 2y agoHaha, then my lua code is reaching LLM code quality...