Guido explains his choice best:
https://plus.google.com/115212051037621986145/posts/YTUxbXYZ...
Second, I don't find Guido's argument convincing. Yes, half-open ranges can be mathematically more elegant (and that's actually Dijkstra's argument), but that doesn't mean that the code necessarily becomes more readable. For example, to construct an array without the element at index i, you'd do the following with Python-style indexing:
a[0:i] + a[i+1:n]
and the following with closed intervals and indexing starting at 1: a[1:i-1] + a[i+1:n]
While there is an element of subjectivity to it, I at least find the latter option more readable (possibly because of habituation to mathematical notation).While the notation for the specific example of i:i+k-1 might be less elegant with closed ranges, closed ranges are something that you find in every math textbook, because sums, products, unions, intersections from a to b (and other operators in that style) operate on closed ranges normally. Closed ranges are the norm in conventional mathematical notation and it makes sense to pick the option that minimizes the overhead when transcribing between mathematical texts and code.
- the 1D index of element (i,j) in a matrix is i+(j-1)*m instead of i+j*m
- the i'th 3-element subvector of a vector is v[3*(i-1)+1:3*i]
instead of v[3*i:3*(i+1)]
- if you have vector of indices that partitions an vector into chunks,
the i'th chunk is v[ind[i]:ind[i+1]-1] instead of v[ind[i]:ind[i+1]]
Perhaps small issues, but these are all real examples from my most recent Matlab project that were annoying.But maybe, like the static typing issue, my opinion on this topic is distorted because I spent a lot of time programming in C++ and comparatively little time reading math papers.
Or maybe it would be equally easy to make a list of tasks that are ugly with [0:n) indexing.
>, my opinion on this topic is distorted because I spent a lot of time programming in C++
Mathematics-related programming[1] in MATLAB, R Language, Mathematica, SAS, etc all use 1-based indexing. Given that the originators of Julia are MATLAB users, it makes sense that they made a deliberate choice to keep 1-based indexing.
In other words, it was more important to grab mindshare from those previous math tools rather than appeal to C/C++/Java/etc programmers.
One outlier in the landscape of numerical programming is Python+NumPy/SciPy in the sense that it uses 0-based indices. While Julia also wants to be attractive to Python programmers, it still seems like the bigger motivation was programmers of MATLAB and other math software.
[1]https://www.youtube.com/watch?v=02U9AJMEWx0&feature=youtu.be...
With half-open ranges, for example, you will need different code to address a segment and the last element of a segment. E.g. if you have some structure with start_of(i) and end_of(i) expressions, then you can do a[start_of(i):end_of(i)] with closed indexing and a[end_of(i)] to access the last element, while with open intervals, you have to break the abstraction and use a[end_of(i)-1].
You can also iterate over start_of(i) .. end_of(i) in a for loop naturally if ranges are closed. (See how Python's iteration is defined in terms of half-open ranges and how iterating over closed ranges – which happens often enough when the values aren't indices – is a bit of a pain in Python.)
Not only does it have sound mathematical reasoning but also some anecdotal evidence of problems caused by one-based indexing in programming languages.