4 ms·
Dijkstra's answer (linked in the article) is best: "When dealing with a sequence of length N, the elements of which we wish to distinguish by subscript, the ne
by hcrisp 4y ago
Dijkstra's answer (linked in the article) is best:
"When dealing with a sequence of length N, the elements of which we wish to distinguish by subscript, the next vexing question is what subscript value to assign to its starting element. Adhering to convention a) yields, when starting with subscript 1, the subscript range 1 ≤ i < N+1; starting with 0, however, gives the nicer range 0 ≤ i < N. So let us let our ordinals start at zero: an element's ordinal (subscript) equals the number of elements preceding it in the sequence. And the moral of the story is that we had better regard —after all those centuries!— zero as a most natural number."
https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E...
- goatlover 4y agoI don't understand why 0 ≤ i ≤ N wouldn't have worked instead, since you already have a less than or equal operator at 0.
- weavejester 4y agoThat would give a sequence of N+1 elements, though. Confusing if you had a function like range(N), for example.
- goatlover 4y agoWhy, it would be an inclusive instead of exclusive range. N is the last element, so no reason for N+1.
- weavejester 4y agoIf we use inclusive ranges where 0 ≤ i ≤ N, then len(range(N)) == N+1. e.g. len(range(3)) == len([0, 1, 2, 3]) == 4
- goatlover 4y agoWait, why would range(3) start at 0 and not 1 in a 1-based language?
- weavejester 4y agoYou said: "I don't understand why 0 ≤ i ≤ N wouldn't have worked instead". Was that a typo? Did you mean 0 < i ≤ N?
- intrepidhero 4y agoIt could just as easily be 1 ≤ i ≤ N and then I wouldn't have to remember that the lower bound is inclusive and the upper exclusive.
- layer8 4y agoThen the delta of both bounds (N – 1) wouldn’t equal the length of the range (N). The inclusive-exclusive convention is used in order for `end = start + length` to hold.
- Jtsummers 4y agoWhy does that matter when the starting point is 1? In that case, you don't need the delta because you have the length already. In the case of a 0-based range you also don't need the delta, though you will want to use the inclusive-exclusive convention so that you get the length "for free". 1 <= a <= N -- N = length, no delta 0 <= a < N -- N = length, no delta You only calculate the length when dealing with other than 0- or 1-based ranges. There, the inclusive-exclusive convention is very handy as you point out. So if we're fixing the initial offset at either 0 or 1, then use the appropriate convention for that offset. If we let the initial offset float, then the inclusive-exclusive makes sense.
- layer8 4y agoIt matters whenever you need to process a proper subrange of an array, and that shouldn’t be different from when the subrange happens to be the whole range. It’s simpler if the same convention is used in all cases. E.g. in Java, a typical example is that OutputStream has the following two methods, where the first can delegate to the second, and the second (which write the specified subrange of the array) can easily calculate the number of bytes to write: int write(byte[] bytes) { return write(bytes, 0, bytes.length); } int write(byte[] bytes, int start, int end) { int length = end - start; ... }
- Jtsummers 4y ago