4 ms·
I don't understand why anyone would create a one-indexed language these days. I feel like Dijkstra laid out pretty well why zero-indexing is objectively better
by bajsejohannes 7y ago
I don't understand why anyone would create a one-indexed language these days. I feel like Dijkstra laid out pretty well why zero-indexing is objectively better [1]. If that wasn't enough, it's also most common by far.
[1] https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E...
- ben509 7y agoYou'll typically see it in computer algebra systems whose syntax is intended to be typeset as traditional mathematical notation. Mathematica, for example, wants an array to look like what's being typeset, and it would be confusing (moreso than the potential off-by-one errors) to display `x[3]` while the actual code said `x[2]`. I think other languages aimed at mathematicians, like Julia[1], do this for similar reasons but, honestly, unless you're writing a CAS, I think it's a bad idea. Having lived with Dijkstra's ideas for a while, I will say that people find closed-open sequences highly counter-intuitive. I work on a financial simulator, so we use dates quite a bit. Representing a year as 1-Jan-X up to but not including 1-Jan-(X + 1) is perfectly natural, and really works well with date-time libraries. It still works if you're using dates and decide to extend it to handle datetimes, and even months 1-M-X to nextmonth(1-M-X) do what you'd expect. So he's right, the math works beautifully. You also avoid writing a successor function when you split closed-open ranges; easy with integers, but gets tricky with almost anything else. But I've had to nag people many times to not use Dec 31. I think the intuition for a closed-closed ranage is deeply ingrained and that extra "Jan 1" seems to stick out and bother people. From that discussion, here's an interesting argument in favor of 1-based indexing: > Let me try to briefly convey how I learned to stop worrying and love 1-based indexing. Basically it comes down to the fact that there are three significant numbers you have to think about when it comes to an array: it's initial index, final index, and length. In 0-based indexing, these are all different: 0, n-1, and n. In 1-based indexing, the final index and length are the same: 0, n, n. That's one less thing to think about and keep track of when coding. It seems trivial, but when you're doing something tricky and juggling a lot of complicated things in your head, even the tiniest alleviation of cognitive load helps, I find. [1]: https://groups.google.com/forum/?hl=en#!topic/julia-dev/tNN72FnYbYQ https://groups.google.com/forum/?hl=en#!topic/julia-dev/tNN7...
- zodiac 7y agoShouldn'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.
- rexpress 7y agoIn the case of Mathematica, 1-based indexing also makes sense in the context of it being a lisp-like language where everything is an expression that can be represented as a tree. So in the case of exp = x + y + z this is syntactic sugar for exp = Plus[x, y, z] As expected with 1-based indexing exp[[1]] returns x, but the zero index can also be used to access the head of the expression, so exp[[0]] returns Plus. Another nice property of 1-based indexing in Mathematica's case is that by using negative indices you can access elements in the reverse direction, so exp[[-1]] would return z, and exp[[-3]] would return x.
- bachmeier 7y agoBecause zero-indexing is in many cases not natural. Yes, a noted computer scientist made an argument years ago, and it's been cited many times. Perhaps to help you understand, nobody talks about row zero of a matrix. But yes, absolutely, there was a noted computer scientist who made that argument years ago.
- domador 7y agoThough I've grown up with programming languages that have zero-based indexing, I've never become a "native speaker", as I always mentally add or subtract a one as needed when trying to understand code involving list indices. Zero-based indexing may be common, but I'm pretty sure it's far from natural to most people. It is likely a main cause of many off-by-one bugs. I wouldn't blame a language designer for choosing one-based indexing instead.
- alpaca128 7y agoI don't have any trouble with it, I just imagine the index as what it is: an offset, and the first element's location has the offset 0 from the start of the array. For me it's far more confusing when I have to write code in a language with 1-based indexing, although I can see why e.g. Julia does it as it's more focused on mathematics and also in some cases the indexing is actually simpler when specifying a range.
- thanatropism 7y agoThat’s utterly unconvincing. Zero-based indexing makes sense only in light of the underlying mechanics of memory address offsets (which, IIRC, isn’t even how eg C literally works). Natural numbers are counting numbers. It makes “more sense” that a sequence up to N contains N numbers, that is, its members can be put in a bijection with {1,...,N} These are all conventions, of course. But the latter convention has a finer pedigree.
- yellowapple 7y agoIt depends on what you're trying to describe. If you're trying to describe an offset from a starting position, then zero-indexing makes sense. If you're trying to describe the rank/position of something in a list, then IMO one-indexing is not unreasonable.