Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why do Julia arrays use 1-based indexing? #558

Closed
lygstate opened this issue Mar 10, 2012 · 12 comments
Closed

Why do Julia arrays use 1-based indexing? #558

lygstate opened this issue Mar 10, 2012 · 12 comments

Comments

@lygstate
Copy link

For most language they are start with 0. Why Julia choose zero.
And from mathematical point of view, start with 0 is better.

1 1 Java 17.050% -1.43% A
2 2 C 16.523% +1.54% A
3 6 C# 8.653% +1.84% A
4 3 C++ 7.853% -0.33% A
5 8 Objective-C 7.062% +4.49% A
6 5 PHP 5.641% -1.33% A
7 7 (Visual) Basic 4.315% -0.61% A
8 4 Python 3.148% -3.89% A
9 10 Perl 2.931% +1.02% A
10 9 JavaScript 2.465% -0.09% A

@lgarron
Copy link

lgarron commented Mar 10, 2012

Depends. Mathematica uses 1-indexing, and it makes perfect sense mathematically and code-style-wise.

I haven't been able to compile / run julia, though, so I can't give a julia-specific defense. :-/

@JeffBezanson
Copy link
Member

There is a huge discussion about this on the mailing list; please see that. If 0 is mathematically "better", then why does the field of mathematics itself start indexes at 1? We have chosen 1 to be more similar to existing math software.

@lygstate
Copy link
Author

Can you give me the URL of the discussion?

@JeffBezanson
Copy link
Member

http://groups.google.com/group/julia-dev/browse_thread/thread/b4d37bd859d86d84?hl=en

You might have to join the list to be able to view it.

It really isn't that interesting a topic though. We all work with both 0-based and 1-based languages and I don't think it matters that much.

@filmackay
Copy link

Sorry about bringing up a painful memory? What about sidestep the issue of 1-ness with a 'begin' just like 'end':

a[begin] = 1 # first item
a[begin+1] = 2 # second

You could leave the integral (non-begin-or-end) as is 1-based, but allow those who want to think in 0-ness to use begin in that style?

@johnmyleswhite
Copy link
Member

I don't really see how pretending that Julia isn't 1-indexed will help people learn to use Julia, given that Julia is unambiguously 1-indexed and will stay that way. Unlike end, this way of using begin is an exact synonym for 1 in all contexts, which violates the generally desirable design guideline that there should be only standard way to do something.

@simonzack
Copy link

We have chosen 1 to be more similar to existing math software.

From a programmer's perspective, I find 1-based indexing harder to wrap around compared to python (mod operations on indexes, -1), etc.

I also thought julia's goal was to unite the mathematical and general purpose languages. In this regard, 0 based indexing is much more popular. Indexing may seem like a small thing, but you deal with it daily, more often than anything else.

@johnmyleswhite
Copy link
Member

I don't really think this topic is open for discussion anymore.

@pao
Copy link
Member

pao commented Apr 4, 2015

From a programmer's perspective, I find 1-based indexing harder to wrap around compared to python (mod operations on indexes, -1), etc.

See also mod1(), which specifically addresses this need.

@kmactavish
Copy link

Julia recommends pyplot for plotting. Pyplot uses 0-based indexing by default, so plotting plot(1:10) gives you the line y = 1:10, x = 0:9. However, since the x-axis is supposed to just be the indices when left unspecified, you would expect y = 1:10, x = 1:10 from Julia. It is troublesome that Julia would not conform to the standard set by the language that it's borrowing to fill its gaps.

@johnmyleswhite
Copy link
Member

I'm locking this issue as there's no real possibility of this being changed.

@JuliaLang JuliaLang locked and limited conversation to collaborators Aug 22, 2015
@ihnorton
Copy link
Member

See also: JuliaPy/PyPlot.jl#117 (comment)

@simonster simonster changed the title Why julia Array start with 1 not 0? Why do Julia arrays use 1-based indexing? Aug 22, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants