4 ms·
-- Indices start at 1 !! SO CRAZY! As of a few years ago, I use Lua regularly in my day job and this one still baffles me. In Lua, the first element in a list
by lattalayta 6y ago
-- Indices start at 1 !! SO CRAZY!
As of a few years ago, I use Lua regularly in my day job and this one still baffles me. In Lua, the first element in a list is index 1, rather than 0.
- corysama 6y agoLua was originally created to be used by a group of FORTRAN programmers. Lua starts with 1 because FORTRAN starts with 1. FORTRAN starts with 1 because Math starts with 1 (Sum of f(Xi) for i in 1 to N)
- tzs 6y agoSometimes math starts with 1 and sometimes with 0. I was once implementing some math algorithm whose description had various subscripted variables. Some ran from 0 to whatever, and some ran from 1 to whatever. I was implementing in C++, with each of these variables becoming an array. I didn't want to write var[i-1] all over the place for the ones that ran from 1 in the book--it was a tricky algorithm and for maximum clarity I wanted my code to read as close to the book as possible. What I did was overload the () operator to make it a 1-based array access operator. I.e., var(i) == var[i-1]. That worked out reasonably, although it probably would have been better just to make the arrays for the 1-based variables one longer and just ignore var[0]. In other words, if var in the text has subscripts that run from 1 through 10, make the var array in C++ 11 long, and just use var[1] through var[10].
- lordgrenville 6y agoSame story with Julia. Julia is 1-indexed because MATLAB is 1-indexed, and MATLAB is 1-indexed because maths is 1-indexed.
- mdorazio 6y agoIn contrast, indices starting at 0 always baffled me in other languages. In pretty much any real-world scenario, you always start at 1 (it's literally "the first element" as in "1st"). It always seemed to me like language developers for some odd reason decided to apply spatial coordinates to arrays.
- bo1024 6y agoIt's not surprising: in these languages like C, arrays were essentially just pointers (i.e. spatial coordinates).
- Pelic4n 6y agohttps://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E...
- ufo 6y agoThis always seems to pop up in these discussions but I don't think I am terribly convinced by his argument. For example, he doesn't mention that option (c) is better than option (a) if you need to iterate backwards. In that case option (a) becomes option (b), which everyone can agree objectively sucks :)
- pansa2 6y agoYes! Reverse ranges in Python are awkward for this reason. The reverse of `range(0, 5)` (i.e. [0, 1, 2, 3, 4]) isn’t `range(5, 0, -1)`, it’s `range(4, -1, -1)`. I think it would be better if the end-points were “low” and “high” rather than “start” and “stop”, so the reverse of `range(0, 5)` would simply be `range(0, 5, -1)`.
- bad-apple 6y agoI think Rust has another good alternative to this, although it might be cheating a little: you can just write (lo..hi).rev(). That way, the common case of going forwards is nice, and the case of going backward isn't much worse.
- bspammer 6y agoLet's say you want every 5th element of an array, with zero indexing your code can be very elegant. Your indexes are 5 * 0, 5 * 1, 5 * 2. With 1-indexing you have to do (5 * 0)+1, (5 * 1)+1, (5 * 3)+1 etc.
- VWWHFSfQ 6y agoI think it's because the creator of the language was originally targeting mathematicians as the primary users. And they consider 1 to be the start of a numerical range. It didn't start to become a "general purpose" programming language until later.
- rstupek 6y agoPascal had indexes start at 1 if I remember correctly
- dvh 6y agoYou remember wrong. var a : array of integer; b : array[0..9] of integer; c : array[1..10] of integer; begin writeln(low(a)); // 0 writeln(high(a)); // -1 writeln(low(b)); // 0 writeln(high(b)); // 9 writeln(low(c)); // 1 writeln(high(c)); // 10 end.
- skocznymroczny 6y agoStrings started at 1 because 0th byte was holding the string length.
- deleted 6y ago[deleted]
- nezirus 6y agoIf you want to treat your table (list) as integer indexed array (i.e. this is just called "array" in many other languages), and use length '#' operator, yes, you need to start at index 1, however, you can use 0 otherwise, or even strings. Lua is a small language, and tables are the basic building blocks for arrays, dictionaries, classes, modules, ... 1-based indexing is IMHO a small price to pay for such flexible and efficient syntax, with small cognitive load)