4 ms·
So, I don't do a lot of Lua programming - basically just having fun with Pico-8 on occasion - so I can't say I have a lot of experience with this. Every single
by mumblemumble 6y ago
So, I don't do a lot of Lua programming - basically just having fun with Pico-8 on occasion - so I can't say I have a lot of experience with this. Every single other language I use starts at 0.
But what I have seen is that, starting from 1 felt weird to me for approximately 1 hour, tops. After that, it was no more or less of a switching challenge than switching between semicolon languages and non-semicolon languages. In other words, no big deal. And, once I got over it, I was pleasantly surprised to find out that I actually have fewer off-by-one errors in Lua than I do in other languages. Because the author is right: No matter how much professional experience I have starting with 0, starting with 1 is still the more innate way of thinking.
Also, my single biggest all time source of off by one errors is translating between mathematical notation and code in a 0-based language. That fact alone is cause for me to hope that Julia might one day supplant Python for data work.
- bsder 6y agoI'm glad that's true for you, but my personal experience suggests otherwise. Dijkstra also mentions the Mesa programming language which had all four of the conventions and that anything other than 0-based, half-open leads to errors. See: https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E... And for those claiming the world is 1-based, they're full of it. Machining measurement, for example, is ALL 0-based. We're talking a field that has multiple standards for everything and yet they converged on 0-based measurement. The bigger problem, though, is that I even have to use array-based indexing to iterate something. That's just asking for bugs. And, my favorite question for people who like 1-based is "Given index and length N, how do I create index-1?" The answer, of course, is newindex = index - 1 % N. The question is, of course, a trick because the answer is really: newindex = (index - 1) % N. Don't forget the parentheses or you have a bug. Yeah, no. I'll stay in my 0-based languages thanks. Side Note: I have other objections to Lua. The big one being that it's not really compatible between Lua installations. Everybody compiles different modules so Lua becomes neither small nor is it the same language between any two implementations.
- samatman 6y ago> The bigger problem, though, is that I even have to use array-based indexing to iterate something. That's just asking for bugs. You absolutely do not, the standard Lua idiom is to use ipairs. The numeric for loop is there, of course, and the index of the pair (index, value) returned by ipairs will be 1 based. of course. Iteration isn't why 1-based indexing is bad, it really shows its inadequacy when doing interval maths. I make abstract syntax trees in Lua, and a node in that tree with zero width at the 4th position is { left = 4, right = 3 } which is... awful.
- josephg 6y ago> my favorite question for people who like 1-based is "Given index and length N, how do I create index-1?" Create index-1? I don't think I've ever needed do that. What does come up all the time in practice in both JS and Lua is "how do I assign past the current end of an array?" In javascript (if you don't use .push) its arr[arr.length] = x In Lua its arr[#arr + 1] = x Even having worked with 0-based indexes for over 20 years, I still doubletake reading that javascript code when it’s embedded in complex code before I can convince myself its inserting. But the lua code is intuitive. Likewise "read the last element in an array": JS: arr[arr.length - 1] Lua: arr[#arr] I much prefer the lua version in all these cases. And simplicity is important here because the code gets much worse when the array isn't in a local variable, but instead is some complex expression. confabulate(foo.bar.baz[foo.bar.baz.length - 1]); <-- Simple statement. Absolute mess.
- creata 6y ago> if you don't use .push I don't think I've ever needed to avoid .push. > I still doubletake Why? "arr[i]" just means "skip i elements and get the next one," which is perfectly intuitive to me. I understand that familiarity is useful, but can you give an intuition for one-indexing that doesn't just appeal to what people are used to? > the code gets much worse when the array... is some complex expression Then... don't do that? Rust has "xs.last()", Python has "xs[-1]", and every language has variables. Anyway, even if that code is an "absolute mess" (which is a bit hyperbolic) it's hardly worse than the one-indexed equivalent, which would be confabulate(foo.bar.baz[foo.bar.baz.length]);
- bilkow 6y agoYou clearly have a different experience using 1-based index than me, it has caused me many off-by-one mistakes. Also, I almost never need to -1 with 0-based index, but in 1-based index its pretty common, specially if moving slices inside an array, the new index of the element is move_position + old_index - 1 (see example). Also see arguments by Djikstra [0]. Example: # python lov = ['l', 'o', 'v'] o_index = 1; array = ['a', 'b', 'c', 'd', 'e', 'f'] move_position = 3 # elements moved here array = ['a', 'b', 'c', 'l', 'o', 'v'] new_o_index = move_position + o_index print(array[new_o_index]) # prints 'o' -- lua local lov = { 'l', 'o', 'v' } local o_index = 2; local array = { 'a', 'b', 'c', 'd', 'e', 'f' } local move_position = 4 -- elements moved here array = { 'a', 'b', 'c', 'l', 'o', 'v' } local new_o_index = move_position + o_index - 1 print(array[new_o_index]) -- prints 'o' Note that I'm not saying 0-based indexing is always absolutely better, but I generally prefer it and believe it usually makes more sense even outside of programming (including math), we're just not taught that way. [0] https://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF https://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF transcript: https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E...
- necovek 6y agoI generally agree there are benefits to both, but even the author uses a bad example. They talk of a baby becoming one year old after one full Earth revolution, but do not acknowledge that baby is basically 0 years old until that time. Sure, we'd say it's in its first year, but age-in-years is zero-based. Part of this is using counting numbers for continuous values, part of it is philosophical, but there are a lot of mathematical properties of zero that make it really appealing (eg. being zero-based allows Python to have a very clear iterable[-1] interpretation).
- hisham_hm 6y agoThe Lua standard library also uses -1 to mean "last element" in various functions; never found it to be an issue. In fact, it feels quite symmetric, going 1,2,3 from one side and -1,-2,-3 from the other.
- necovek 6y agoSo is l[len(l) - 1] == l[-1] in those contexts? Potential for one-offs is always there, but I am highlighting how _most_ of the math is simplified when there is a zero between positive and negative numbers :)
- hisham_hm 6y ago> So is l[len(l) - 1] == l[-1] in those contexts? No, because Lua is 1-based :) The last element is l[#l] (#l is "length of l"). Negative indices aren't really used in arrays, but are used in the string library, so given s = "hello"; s:sub(1,4) gives you "hell" and s:sub(-2,-1) gives you "lo".
- necovek 6y agoSure, that was my point: s:sub(#s-2,#s-1) != s:sub(-2,-1) — if # works on s at all :) Basically, some things are more "natural" with 1-based counting, but some aren't.
- xscott 6y ago> Also, my single biggest all time source of off by one errors is translating between mathematical notation and code in a 0-based language. If you were working with polynomial coefficients, Fast Fourier Transforms and convolutions, or modulo arithmetic, you'd prefer zero based arrays. In my experience, the majority of places where math notation uses 1-based arrays are in applications where the index doesn't matter. I prefer zero based arrays specifically because they are better for doing mathematics. Yeah, it sucks that a lot of matrix algorithms in text books use one based arrays, but I blame the text book for getting it wrong.