5 ms·
Shouldn't the tuple for 1-based indexing be 1,n,n ?
by zodiac 7y ago
Shouldn't the tuple for 1-based indexing be 1,n,n ?
- ben509 7y agoI think so, yeah. I copied that quote verbatim.
- lioeters 7y agoYes, you're right. I think that slip says a lot about the tricky issue at hand. First position, last position, and length for an array of [1, 2, 3]. 0-based: 0, 2, 3 (0, n-1, n) 1-based: 1, 3, 3 (1, n, n) Actually, that's a pretty convincing argument. I've played with Lua before, and did get the feeling that 1-based index has some advantages (but couldn't articulate why). Having the last position and the length be the same seems to fit the common intuition, like how children count: "What is the position of the last thing" and "How many things do we have?". So it may make the language easier to learn. On the other hand, it may make the learner struggle to pick up most other languages which use 0-based index. I suppose it's been argued in depth on both sides..
- zodiac 7y agoHaving the last index and the length be the same may not be the good thing. for instance, if you translate python's negative indices (x[-i] = x[len(x)-i]) directly the last element would be...x[-0]? But then having the last element be x[-1] in python is also not entirely obvious.