7 ms·
I actually like 1-based for numerical work. A lot of great languages (Smalltalk, APL, Lua...etc) use it too. It makes sense with matrices.
by 4thaccount 8y ago
I actually like 1-based for numerical work. A lot of great languages (Smalltalk, APL, Lua...etc) use it too.
It makes sense with matrices.
- lloda 8y agoAPL let you choose. It was only trouble and then Iverson made J 0-based.
- 4thaccount 8y agoYeah you can change when needed of course (forgot about that). Iverson's J did a lot of things (chief of them was removing the awesome symbols) to make it more appealing. A lot of folks understand the cause, but also realize it takes away one of the best reasons to use APL. So moving to 0-base wasn't necessarily because he thought it was better.
- ModernMech 8y agoThis is something I don't fundamentally understand. As someone who works a lot with MATLAB I'm very used to and like 1-based indexing. But when I use C or Python, 0-based indexing is not something I complain about or hold against the language. It's just the way things are. Maybe if you don't think of it in terms of a different index basis and instead you think of it as indexing vs. offsets then it becomes easier to switch between the two?
- ssivark 8y agoIf that's someone's biggest complaint against the language, then I'd say the language must be pretty awesome ;-) It's basically bikeshedding. "But I look at the shed all day..." "But I'm used to looking at reddish colors..." "Red is more correct than green because <yadda yadda>..." Let's all move on to more important things :-)
- zimablue 8y agoIt's not bikeshedding because: 1) it is pervasive and fairly importsnt 2) it's kind of a deep indication that something is amiss. It's like a language written in 2019 that has default dynamic not lexical binding, not in terms of importance but in terms of it's an argument that was had and decided 20 years ago. If this is wrong then you can probably assume that lots of other things which are important, decisions which can't be "proven" correct but have both convincing arguments and weighty empirical evidence are wrong (I'm not having that argument here but google why 0-based makes sense by Djikstra). I might have to learn Julia at some point but 1-based indexing was a fairly heavy indicator to me to hold off.
- improbable22 8y agoIndeed it's starting to sound rather more like a church than a bikeshed! I am curious what people write all day for this to be such a big issue. It's not like you need to re-invent N-dimensional array indexing every morning -- we have abstractions. If translating code then it could be from either convention... or from a mathematics book. (Where you may note that mathematicians are unconvinced by the CS arguments... admittedly not a group of people known for taste in all matters, but taste in notation they have thought about quite a bit.)
- ModernMech 8y agoI'm not convinced. I've written a great deal of software in C++ and MATLAB. I don't see what the big deal is either way. You say this thing has been "decided 20 years ago" (by whom?) but that's clearly not the case, as myriad languages that use 1-based indexing are in existence, continue to come into existence, and are used to great effect. You cite Djikstra's argument (well, you didn't actually cite it [1]) but his reasoning isn't exactly air tight; he appeals to subjective ideas of "ugliness" and "unnaturalness" and "preferences" and cites a single anecdote for Mesa as proof of his correctness, then dismisses FORTRAN, ALGOL, and PASCAL out of hand as a "pity". Again, I'm not convinced. [1] https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E...
- 8y ago
- alexeldeib 8y agoI don't hold it against any language, but it certainly breaks with common convention. The number of simple off by one errors I had when I started writing Lua was a bit irritating.
- ModernMech 8y ago> but it certainly breaks with common convention That depends on which circles you run in. If you're a programmer, yeah, it breaks with common convention. If you're a scientist or engineer, not so much. When writing a programming language for scientists and engineers you have a choice: do I stick with programmers' conventions or do I stick with engineers' conventions? I've had the pleasure of teaching both MATLAB and C++ to freshmen engineers. For them, they get off-by-one errors in C++, not in MATLAB.
- repsilat 8y agoFor "2D indexing" into a 1D array it's actually a little awkward. With an n×m matrix, - zero-indexed: i×m+j - one-indexed: (i-1)×m+j OTOH one-based is slightly better for trees stored in 1D arrays: - zero-indexed: parent=(child-1)/2; children=2×parent+(1, 2). - one-indexed: parent=child/2; children=2×parent+(0, 1). My favourite fact about this stuff: in VB (or was it VBA?) when you asked for an array of size n, you actually got an array of size n+1. So people could do 0-based or 1-based indexing and be none the wiser...
- ska 8y agoAll fair enough but not particularly relevant here (in the context of "why Swift"). For doing numeric work in a language actually defined for that purpose, you will have proper 2D (and hopefully n-D) arrays, and hopefully slicing operations on them. So you'll never do this. In this context doing 2D indexing in 1D arrays is a code smell. It does come up in the case of writing libraries for general purpose language with poor support for numerics and linear algebra, but then you should be abstracting this away from your callers.
- overgard 8y agoWell, the nice thing about doing 2d indexing in 1d arrays is the 1d array sits in one contiguous block of memory, which can be important for performance, and also makes it a lot easier to pass the array to C libraries (which I think you alluded to). Whereas in a lot of languages, int[3][3] might be stored as 3 pointers to 3 arrays. I imagine Julia optimizes this?
- improbable22 8y agoYes, in julia A = rand(2,2) is a matrix, and reshape(A,:) is a vector view of the same continuous block of memory. You may also index A[i] directly, as if it were a vector; eachindex(A) is an iterator which uses whichever style of indexing is going to be quickest (and in cache-friendly order).
- ufo 8y agoMany of the low level libraries for numerical computation are coded in Fortran, which is 1-based. So it is a wash in this aspect.