6 ms·
This is not meant to be tongue-in-cheek - I thought this argument was settled ages ago. Are there any respected languages that use 1 based indexing? This is an
by dpratt 13y ago
This is not meant to be tongue-in-cheek - I thought this argument was settled ages ago. Are there any respected languages that use 1 based indexing? This is an honest question - every single one I've ever used (C, C++, Java, Scala, Python, JS, Objective-C, a bunch of academic ones) have been zero based. 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.
- deleted 13y ago[deleted]
- esrauch 13y agoLua is probably the strongest candidate for a respected 1-based language. Smalltalk, Fortran, and MATLAB are other examples of widely used languages that are 1 based.
- desireco42 13y agoI am going to learn Lua and use it much more now, thanks for pointing this out. As someone said, having 0 based indexes is inhuman way of dealing with arrays, only made some sense in C, others just copied.
- deleted 13y ago[deleted]
- theseoafs 13y agoOff the top of my head, Lua uses primarily 1-based arrays. (Of course, Lua arrays are actually associative arrays, so negative numbers and 0 are also valid indices, but all the standard library functions except your arrays to start at 1.)
- russellsprouts 13y agoLua. However, you are allowed to use 0 based indexing as well -- but the # length operator and table.remove() will not work correctly.
- aiiane 13y agoAlso loops will potentially behave awkwardly, due to the table having shifted into dictionary-esq indexing.
- anonymoushn 13y agoThis won't be a big deal. The 0 key will be in the hash part, and the 1-#t keys will be in the array part. The language doesn't expose the distinction, so the loop-related awkwardness is limited to a tiny performance hit.
- ufo 13y agoIf you try to use ipairs to iterate then it will skip the zero-th index but if you use a numeric for loop (for i=0,N do) then you should be fine.
- kevincrane 13y agoMatlab does (or at least it did when I used it for a class a few years ago). I was trying to iterate through some array for a project and kept hitting an error, and was totally stumped until some EE major came over and fixed it. "Haha did you forget how to count? You're starting the index at 0, that's the problem."
- ArbitraryLimits 13y agoDid the same guy remind you that "i" is a poor choice for an index in Matlab because it's actually sqrt(-1)? And _then_ tell you that you're supposed to do array reshaping instead of loops in Matlab? Because if the same guy had hit me with all three I might have killed him.
- Dylan16807 13y agoHahahaha respecting the matlab scripting language, good one.
- ihuman 13y agoI had to use matlab the other day. It still starts at one. The first time I used it, that confused me too.
- aidos 13y agoI've just started using Octave (for the coursera machine learning course) and it also starts at 1. Takes a few exercises to reaclimatise. More annoying is that it doesn't auto broadcast matrix operations like numpy does (and the slicing doesn't feel as powerful either).
- fdej 13y agoLua, Julia, Mathematica, Matlab (ok, not so respected as a language)
- hcarvalhoalves 13y agoI've found Matlab (the language) great for it's intended purpose.
- yoodenvranx 13y agoIn my opinion Matlab the language sucks even for its intended use. What makes the complete package arely acceptable to work with is the environment, the documentation and the included libraries. 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.
- hcarvalhoalves 13y agoInteresting. I make a living developing backend systems in Python, but picked up Matlab (Octave) for machine learning, and really enjoyed doing linear algebra on it. For PoCs and general experimentation I find it more amenable than messing with Numpy. Of course it isn't great for building complete systems, but for self-contained programs - which I believe is the intended purpose - it works pretty well.
- atondwal 13y agoMathematica is LISP, so it's really 0-based indexing, where the first element is the function you're applying. All the sugar and literals are made to hide this, but you can code like you're dealing with a real LISP.
- agrona 13y agoFORTRAN and Algol60 are also not-0-indexed. 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.
- swang 13y agoPascal
- anonymoushn 13y agoMatlab does this, and Julia is following suit out of a desire to be attractive to Matlab users. I would really rather it didn't. 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.
- eps 13y agoDelphi?
- agumonkey 13y agoDoes ADA `First` counts ?
- ArbitraryLimits 13y ago> since the first element of an array is zero distance away from the beginning of the array. 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).
- dpratt 13y agoI'm just thinking logically - not even on a pointer level. If I'm doing index/offset math to calculate an array position, it's foolish to start at 1 instead of 0.
- anonymoushn 13y agoAdding things that aren't conceptually indices to indices works the same way in both conventions (so a[idx] and a[idx+k] have k-1 things in between). You miss out on stuffing multiple dimensions into one with a[x*xlen+y], but there's generally no reason to do that. Could you elaborate about the uses you have in mind?.
- calpaterson 13y agoI know you said "respected", but just for completeness with all the other suggestions: XML and friends.
- n00b101 13y agoR?
- philh 13y agoPerl lets you choose whatever index you like for the first element, including negative ones.
- marcosdumay 13y agoThat's because Perl does not have arrays. You've been using hashes. In practice, Perl is not expected to be fast, so I don't think there's any difference.
- neild 13y agoThis is not correct. Perl has arrays and, as the person you are replying to said, allows the index of the first array element to be changed globally by setting a special variable. $[, if I recall correctly.
- tzs 13y ago> Are there any respected languages that use 1 based indexing? 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.
- EpicEng 13y agoIgnoring the fact that equating what "normal people" would think in contrast to what a trained professional would think in the context of said profession, 1-indexing leads to other problems. 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.
- EpicEng 13y agoCan't edit now, but I meant to say: "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..."
- daviddlhg 13y agofortran really lets you start from any integer index, including negative ones, 1 is just a default if you omit the lower bound. But it's not half-open when you declare (or use slicing) i.e. it uses inclusive lower and upper bounds, so A(-17:-17, 10:10) has one element not none. It might or might not have been nicer if they'd been half-open. Or one could also imagine a start+length type declaration, but meh. http://orion.math.iastate.edu/burkardt/papers/fortran_arrays.html#Array_Declarations http://orion.math.iastate.edu/burkardt/papers/fortran_arrays...
- davidw 13y agoErlang is 1 based. For those who know it, is this something from its Prolog heritage?