4 ms·
In scientific-oriented languages (R, MatLab, Julia, Fortran, &tc.), array indicies tend to start at 1. I think it is a culture thing. Software engineers prefer
by snicker7 7y ago
In scientific-oriented languages (R, MatLab, Julia, Fortran, &tc.), array indicies tend to start at 1. I think it is a culture thing. Software engineers prefer 0. Scientists prefer 1.
- metalliqaz 7y agoThe scientists can do what they want until they start using computers, then they should listen to the software engineers.
- Certhas 7y agoThis is silly. Different domains have different needs. If you think of arrays as pointer + offset calculation, and your only for loop has an explicit index increment in it, then starting at 0 is natural. If you are a systems programmer then a language targeting you should accommodate that. If you are anyone who doesn't want to care (too much) about how things are implemented, then it's a lot less error prone to mark the 1st element of an array a[1]. Having taught and written fairly complex scientific code in both 0 and 1 indexed languages, there really is no good reason to do anything else. for a in array[3:6] Should iterate over the third to sixth element of the array.
- metalliqaz 7y agoI dunno man, it's Dijkstra's essay. He has a pretty good reputation.
- deleted 7y ago[deleted]
- contravariant 7y agoNah everyone should just listen to the mathematicians and allow for arbitrary index sets. Seriously though you save yourself a lot of trouble if you don't worry that much about what range your indices have but rather what they represent. The fact that they're integers starting at 0 is just an implementation detail.
- goatlover 7y agoSoftware engineers made the first high level programming language called Fortran, which starts indexing at 1 by default.
- pjmlp 7y agoSoftware engineers using languages from Algol, Pascal, Ada, Modula familes get to choose which array indices to use.
- enaaem 7y agoIn science indexing can be quite inelegant too. We often denote time steps as t0, t1,...,tn. But if we put these steps in a vector v, we would get: v[1]=t0,...,v[n+1]=tn.
- mkl 7y agoI think that's the case only if we're stuck in a programming language like Matlab. Usually we would get v[0] = t0, ..., v[n] = tn. Mathematicians are perfectly happy starting with zero, even for things like matrix indices, when it makes sense.
- baddox 7y agoBut note that the potential for underlying confusion is not limited to the choice of indexing in programming language syntax. Casually ask someone who lives on the 8th floor of an apartment building how many floors they go up in the elevator and you'll probably discover that people don't think in detail about the precise labeling of elements in a collection or how arithmetic with intervals work.
- samatman 7y agoTo add to the confusion, you’ll get genuinely different answers in America and Europe.