Fortran, Lua, Mathematica, MATLAB, Julia, and Smalltalk among current languages. Among historically important languages, COBOL, Algol68, PL/I.
> It's quite clear that it's the right solution, since the first element of an array is zero distance away from the beginning of the array.
You think it is clear because you are used to thinking of array indexes as offsets. If you think of them as ordinal numbers for describing the position in a list, then it is clear they should start at 1. If someone's grocery list is:
bread
milk
eggs
flour
and you ask them "what item is #1 on your list?", most people will say bread.If you look at that list I gave of 1-based languages, you might note that many of them are oriented toward math/science/engineering. In math, both 0-based and 1-based are used. For example, matrix elements are specified by row and column number, and both of those usually start at 1. On the other hand, polynomial coefficients usually start with 0.
foreach is great, until you need an index. When you do, 0-indexing is more natural. Take, for example, looping over an image.
unsigned char *buf = get_image();
for(int y = 0; y < height; ++y) {
for(int x = 0; x < width; ++x) {
// simple
unsigned char pixel = buf[y * width + x];
}
}
That doesn't work when y begins at 1. All of a sudden you need to add and/or subtract 1 everywhere to keep things in line. I used C here, but this applies to many languages in many different circumstances where an index/count is required."Ignoring the fact that equating what "normal people" would think in contrast to what a trained professional would think in the context of said profession isn't always an apt comparison..."
http://orion.math.iastate.edu/burkardt/papers/fortran_arrays...
That's much less of a win in any language but C, where "array" means something more than "pointer." If you're checking array bounds on every access then the extra assembler instruction to subtract one from your index doesn't matter in comparison, whereas if you're just dereferencing a pointer it could potentially double the cost of array accesses (two instructions instead of one if your instruction set has base + offset addressing modes).
Could you elaborate about the uses you have in mind?.
The languages that do this seem to pretty uniformly use closed intervals, so that [1:length] is the whole thing, while in python [0:length] is.
From the exple.tive.org page Guido links to:
> On top of that other languages that antedate BCPL and C aren’t zero-indexed. Algol 60 uses one-indexed arrays, and arrays in Fortran are arbitrarily indexed – they’re just a range from X to Y, and X and Y don’t even need to be positive integers.
It was actually a pretty interesting read, I recommend it if you have the time.
I did 5 years of image processing in Matlab but then switched to Python 4 years ago. One of the best choices I did in my programming career.
In practice, Perl is not expected to be fast, so I don't think there's any difference.