Elaborating on the reasons given in Dijkstra's memo on the topic, we observe the following:
- Ranges are of the form 1 ≤ i < N + 1.
- The last element of a sequence has index N.
- How do we represent the index of the last element if N is not known? We need a new symbol!
- There is no obvious choice for this symbol, so we choose something like
end
(Matlab). Ugly.
- Ranges are of the form 0 ≤ i < N.
- The last element of a sequence has index N – 1.
- How do we represent the index of the last element if N is not known? We need a new symbol!
- But now there is an obvious choice:
-1
. It comes directly from the expression for the index of the last element,N-1
. - And, we can now count backwards using only the usual numerical symbols, so that the index of the second-to-last element is
-2
, etc., instead of something ugly likeend-1
.