I was forced to create an account for this website after finding this thread while searching about perl's ability to adjust its initial index. I had to, because perl programmers are apparently retarded. They needed someone to tell them why 0 instead of 1. Hint: It's an actual reason. It's not a coincidence that almost every programming language in existence uses 0 instead of 1.
There was an interesting point brought up that the reason belongs to the physical interpretation of an index as a memory offset. While that's a nice coincidence, it's definitely not the reason modern languages still choose 0 instead of 1.
The reasons mostly swirl around the length of the array.
1. Modular arithmetic works with length
If you ever found the need to map an integer into an array, i%L will give it to you instead of the uglier (i-1)%L. For this reason, and possibly others, in general, doing looping/procedural algorithmic design for arrays that start with 1 can be a pain. I would wager that an index doubling as a distance from the beginning also supports this point as that distance might naturally come up in an algorithm.
2. The style of specified boundaries argument (Dijikstra)
First, he establishes that of the way to specify a range ) ( and (), the ) is superior: <p>2.1 L-0 = L but L - 1 != L <p>2.2 For two adjacent subsequences, the upper bound of one is the lower bound of the other. <p>These things happen if you choose (0, L as well. But as he points out, if there ever is a natural starting point, you must always denote your range unnaturally as (p-1, ...]. Hence, we prefer ) over (.
2.3 He points out that the other ways are associated empirically with error.
These seem to justify [). Then the style argument comes about easily: [0, L) is prettier than [1, L+1).
3. Symmetry about the "entry point"
Consider an array as a chain in memory. With the starting index 0, the counting with respect to it represents a distance from that starting point -- how much you are in the array or out of it. -2 is 2 out of it. 2 is 2 into it. Similarly, lobbies tend to be known as floor 0 with those beneath being P1, ... (instead of -1, ...) and those above as (1, ...).
In reply to Re^2: why the array index has to start at 0??
by tedbradly
in thread why the array index has to start at 0??
by targetsmart
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |