Comment by Ygg2
4 months ago
> fight the borrow checker
I see this and I am reminded when I had to fight the 0 indexing, when I was cutting my teeth in C, for class.
I wonder why no one complains about 0 indexing anymore. Isn't it weird how you have to go 0 to length - 1, and implement algorithm differently than in a math book?
The ground floor in lifts isn't "1", it is "G". Same thing.
Country dependent. Like there are 1-based indexing languages (Lua, Matlab, et al)
And others like Pascal linage (Pascal, Object Pascal, Extended Pascal, Modula-2, Ada, Oberon,...), that have flexible bounds, they can be whatever numeric subranges we feel like using, or enumeration values.
Actually some lifts don't start with G but -2, because basement exists.
Not in Lua.
Most languages have abstractions for iterating over an array so that you don’t need to use 0 or length-1 these days
Because the math books are the ones being weird. https://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831...
Maths books aren't being weird. They are counting in a way most people learn to count. One apple, two apples, three apples. You don't start zeroth apple, one apple, two apples, then respond the set of apple contains three apples.
But computers are not actually counting array elements, it's more accurate to compare array indexing with distance measurement. The pointer (memory address) puts you at the start of the array, so the first element is right there under your feet (i.e. index 0). The other elements are found by measuring how far away from the start they are:
1 reply →
I believe it’s a practicality to simplify pointer arithmetic
Yes but why does no one talk here about fighting the 0 indices. Or how they are switching to Lua, because 0 indices are hard?
Am I the only person that remembers how hard it was to wrap your head around numbers starting at 0, rather than 1?
I find indices starting from zero much easier. Especially when index/pointer arithmetic is involved like converting between pixel or voxel indices and coordinates, or indexing in ring buffers. 1-based indexing is one of the reasons I eventuallz abandoned Mathematica, because it got way too cumbersome.
So the reason why you don't see many people fighting 0-indexing is because they actually prefer it.
> 0 indices are hard?
I started out with BASIC and Fortran, which use 1 based indices. Going to C was a small bump in the road getting used to that, and then it's Fortran which is the oddball.
2 replies →
Yes, I think you are. The challenges people describe with Rust look more difficult than remembering to start from 0 instead of 1…
11 replies →
For languages with 0-based array element numbering, say what the numbers are: they're offsets. 0-based arrays have offsets, 1-based arrays have indices.
I sometimes work on creating my own programming language (because there aren't enough of those already) and one of the things I want to do in it is 1-based indexing. Just so I can do:
...and get "Alex" instead of "Kim".
Or take a lesson from languages where this isn't a religious question and do
or if being flexible,
Bonus points for adding a macro or function, to make the second form available as first as well.