Arrays in the Julia programming language are somewhat different from arrays in other programming languages. Not because of different behavior, but because they are start at 1 instead of 0. It's because Julia is used for mathematics , machine learning and scientific etc. Where calculation as mentioned, maybe a bit of confusion when interfacing to the external (non Fortran/Matlab) world but this interfacing doesn't have to be done with arrays if this should be a problem.
If you try to access the array by using zero then it will result an error like the above code:
> [2,3,4,5][0]
ERROR: BoundsError: attempt to access 4-element Array{Int64,1} at index [0]
Stacktrace:
[1] getindex(::Array{Int64,1}, ::Int64) at ./array.jl:729
[2] top-level scope at none:0
But using one works fine
> [2,3,4,5][0]
1
Arrays should start at 1
because as people count starting from one. The thing with 0-based indexing is that you always then have to write code of the type for i=0:len(a)-1
when iterating. Either ways, there is always some indexing arithmetic.
Most of people would hate Julia arrays because they do not start with 0
like other languages. Most languages are inspired from C so they have array index from 0
. There's a special reason especially for C i.e. the name of an array is a pointer, which is a reference to a memory location. Therefore, an expression *(arr + n) or arr[n] locates an element n-locations away from the starting location because the index is used as an offset.
But all languages do not have start with index 0
such as MATLAB, Lua, R etc because most of the tasks that Matlab is used for, e.g. physics and engineering, use the convention of indices starting at 1
in most of the literature, so people working in these fields do not need to convert their algorithms by subtracting 1 from everything.
Hence, I believe that Julia arrays should start from one because it's made for these reasons. But I think array index with 0
are needed because of representation of memory offset as they are stored in continuous addresses especially languages like C, C++ and Java.
Discussions:
https://discourse.julialang.org/t/whats-the-big-deal-0-vs-1-based-indexing/1102
https://github.com/julialang/julia/issues/558
https://groups.google.com/forum/?hl=en#!topic/julia-dev/tNN72FnYbYQ
https://en.wikipedia.org/wiki/Comparison_of_programming_languages_(array)#Array_system_cross-reference_list
http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html
Learn more about Julia Arrays:
https://docs.julialang.org/en/v1/base/arrays/
https://en.wikibooks.org/wiki/Introducing_Julia/Arrays_and_tuples
Top comments (1)
C is essentially a high-level assembly language, so it reflects the nature of the underlying hardware. Why poison an entire generation of languages for this reason? C++, Java, JavaScript, C#, PHP, etc.
Smalltalk, Julia, Lua, MATLAB, R, etc. were created for human beings, not for the underlying hardware. Human-oriented languages do not concern themselves with memory layout but with how humans think about problems.