https://docs.julialang.org/en/latest/devdocs/offset-arrays/
Having said that, I encourage you to try out the 1-based indexing, as I think you might find a lot of things become surprisingly more intuitive.
https://docs.julialang.org/en/latest/devdocs/offset-arrays/
Having said that, I encourage you to try out the 1-based indexing, as I think you might find a lot of things become surprisingly more intuitive.
Can you provide an example? It seems that the assertion only reflects a personal point of view, similar to the egg endianness discussion in Gulliver.
PS: I am joking here of course, but seriously, try counting things with your fingers starting with a fist, then one finger, then two.
This is too often the attitude of Julia people. But zero-based indexing is not just some arbitrary offset. Programmers just like it. You say that many things are more intuitive with one-based indexing, but many things are less intuitive too. I don't think python ever would have become popular without zero-based indexing.
The thing is, we really don't know why people prefer certain types of language. All we know is that they do. Computer languages can't evolve gradually like natural languages, but if they could I really don't think we'd go to one-based indexing.
Um, that's the definition of "arbitrary".
And it's a good attitude for the Julia people to have. Their target audience is not just (or even mainly) full time developers. They're targeting scientists, statisticians etc. As such, it makes sense for Julia to use conventions that are appropriate to their audience. This pervades the whole of Julia, not just indexing although the latter is exceptionally rich for this reason.
> Programmers just like it
That's a reason for having 0-based indexing but not an especially good one. And for Python it was an arbitrary decision. That's not to say that 0-based indexing is necessarily an arbitrary decision, it wasn't for C, for example, but it often is.
I don't see how scientists or staticians would be unable to understand a basic knowledge like zero-based indexing. In fact, zero-based indexing already is widely used in basic areas such as series and sequences. Why are freshmen quite capable of understanding such a fundamental convention but somehow seasoned scientists and statitians are not?
Of course they can. In fact, they tend to have a complex relationship with indexing which is why Julia supports such a rich indexing language.
But the default the Julia team chose was the natural, mathematical default rather than the natural memory addressing default. Which, given what they were trying to achieve, was entirely sensible.
And, on a side note, just because something is physically possible, it doesn't follow that it's the most sensible choice. Otherwise language development would have stopped at the first Turing complete language.
What the Julia team is attempting to do is make the transition for their target audience as seamless as possible. This can be seen in their indexing language but also in their choice of dispatch, their type conversion architecture etc. A great deal of thought goes into these decisions (you can see it in their issue/RFC trackers). "A CS freshman could work it out" doesn't even come close.
And science has lots of 1- based indexing: Matlab, Fortran, R, most math...
I'll tell you: it's very, very, very easy. And when a language makes things easy, people tend to like it (that's one of the reasons people like Python after all).
Addressing an element T[h][w][c] in a linearized 3D tensor with 0-based indexing:
T[h*W*C+w*C+c]
with 1-based indexing: T[(h-1)*W*C+(w-1)*C+c]
Or let's say you want to take a string "abc" and repeat it until the length is 10, getting "abcabcabca". With 0-based indexing: a,b = "abc", [" "]*10
for i in range(0,10):
b[i]=a[i%3]
In a 1-based language that's: for i in range(1,11):
b[i]=a[(i-1)%3+1]
Here's what Dijkstra has to say about it:http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EW...
That being said, I also abhor 1-based indexing in julia.
Well, you get used to it, and it is imho a small price to pay for julia's other cool things.
In the end, julia needs to access arrays through pointers, and indirect adressing / LEA use zero-based indexing. Somewhat amazingly, llvm manages to remove/hoist most of the extraneous subtractions. But still, it would be much more transparent if julia simply reflected the hardware standard of zero-based addressing (when in doubt, stay as close to the silicon as reasonably possible).
Offset arrays are a bad choice when avoidable, because (1) they incur an additional pointer load, (2) most julia code uses 1-based indices (conforming to common language idioms is always good), and (3) you will trigger all sorts of bugs in all sorts of packages that falsely assume that all AbstractArray are one-based.
>with 1-based indexing: T[(h-1) * W * C+(w-1) * C+c]
Your example showing how cumbersome and ugly it is to subtract 1 -- isn't how the intended audience uses mathematics software. Instead, they would use idiomatic multidimensional access with commas or multi-brackets syntax that understands 1-based indexes. They do not manually multiply and add the offsets into a raw 1-dimensional array. (E.g. idiomatic T[x,y] or T[x][y] instead of contrived T[(x-1) * W+(y-1)])
It's also the same concept for MS Excel. In VBA macro programming code, one doesn't need to litter the code with "minus 1" everywhere. The 1st row and 1st column (cell A1) is addressed as "1,1" in the Visual Basic function "Worksheets!Sheet1.Cells(1, 1)"
Mathematics software has a tradition of 1-based indexing probably because many (most?) equations in written mathematics are 1-based. (Examples [2] [3].) It doesn't seem so unreasonable that scientists prefer that their math programming code has 1-based indexes to match the 1-based indexes in traditional math notation.
[1] https://news.ycombinator.com/item?id=11072438
[2] average : summation symbol ∑ has "i=1..n" not "i=0..n-1": https://en.wikipedia.org/wiki/Average#Arithmetic_mean
[3] linear algebra matrices : i and j subscripts begin at 1,1 and not 0,0: https://en.wikipedia.org/wiki/Matrix_(mathematics)
This isn't common in 1-based indexing languages. Very common in 0-based indexing languages[3].
If the 0-based indexing was objectively superior, wouldn't there be fewer instances of such bugs?
[1] https://en.wikipedia.org/wiki/Off-by-one_error
[2] https://en.wikipedia.org/wiki/Zero-based_numbering
[3] https://softwareengineering.stackexchange.com/questions/4289...
Off-by-one can show up in a bunch of ways that are agnostic to the underlying indexing, so it isn't obvious to me that there would be a substantial difference at all. Also, given what's said below about the ease of writing code which fails to consider custom indices, I'm not sure I believe just switching to 0-based or anything else in Julia is as painless as you suggested earlier - it could introduce its own off-by-one errors!
This fits my understanding. I've seen people do off by one just in the wild when counting how many posts/nails/etc are needed.
Have you used julia? You're almost never calculating your offset to a pointer. The VM does that for you. (As it does for python, ruby, erlang). Or actually in the case of julia it's more accurate to say the (extremely lazy ahead of time) compiler does it for you.
But why would you ever use linearized addressing when you have proper multidimensional access? Aside from doing the low-level implementation of multidimensional access, you shouldn't ever need to do that.
> Or let's say you want to take a string "abc" and repeat it until the length is 10, getting "abcabcabca". With 0-based indexing
Your example assumes not only the stated indexing model but also a range function that returns the integer members of a half-open range, which is a little crazy of a way to do loops, but the least unintuitive way with 0-based indexing, so programmers are used to it from C (where you do it without an actual function call) and newer zero-based languages.
With 1-based languages, the sane way is the more intuitive closed range (for i in 1:5) which doesbt require an extra mental operation to convert to the set of values that will actually be used. There are good uses for half-open ranges, but the kind of explicit loop control they get used for in 0-based languages is an artifact of 0-based languages, so arguing that it is (even more) awkward in a 1-based language isn't an argument against 1-based languages.