-
-
Notifications
You must be signed in to change notification settings - Fork 807
Better explanation for base-0 indexing #176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The argument "that’s simpler for computers to do" is not credible. Compilers and interpreters can easily handle an offset of one. The choice of index base is made with human convenience in mind.
|
I agree that the existing explanation is not correct, but I don't think novice students will understand your replacement explanation. What about providing no justification at all? |
|
They may not fully understand it, but they get some idea of why this is not just some historical accident. The point is not very important, since none of the later material depends on an understanding of this explanation. So yes, we might simply say nothing. But my experience is that people like being given an explanation for surprising facts, even if it is unclear, and in fact even if it is wrong. |
|
I agree that the new explanation is confusing. Can you elaborate on the last sentence of your commit message? |
Add more lables to to setup labels script
|
@abostroem By "human convenience" I mean that zero-based indexing reduces the amount of "+1" and "-1" in the source code when indices are computed. This also means that there are fewer occasions for introducing a mistake by forgetting +/-1. I can't offer anything but anecdotal evidence for this, unfortunately. Presumably one could do a study on Fortran/Matlab vs. C/Python code to verify this claim. There is a short paper by Dijkstra on this subject that also argues for zero-based indexing. It refers to experiments with a language called Mesa from Xerox PARC that support this argument. I haven't seen anything more precise on these experiments. |
|
I think it's hard to get it across in relatively few words, but in the past I have tried to explain why it's easier by referring to centuries. Most people are either confused or were confused about centuries, as we say that the years 0-99 are century 1, 100-199 are century 2, and so on. Most people, when they hear a year like 1990, look to the leading two digits (1990) to determine the century, and through enough reinforcement, they come to learn the rule that you have to add one to that get the actual century (20). If we had instead started with a century 0, then we would no longer have to learn that rule, that many people still have to explicitly remember, as it is not intuitive. Similarly, with computers, it is often handy to be able to be able to just look at the digits to determine the "century" (or whatever you're figuring out), rather than having to remember to add one all the time. I'm not sure if this is worth adding to the lesson as a callout or what have you; probably it's best to find a nice reference or blog post and link to that. I can make such a blog post if one doesn't already exist. |
|
I like @tbekolay's explanation, though I agree that it's counterproductive to spend too much time on this point. I'm a mathematician and there have been many times when I've appreciated zero-indexing, but I had to think hard to follow Dijkstra's careful justifications. Note that he justifies zero-indexing after and because of the fact that one wants to use half-open intervals (i.e., omit the value referenced by the upper index). |
|
The most straight-forward explanation of 0-based indexing I've found (though I have no idea how well it works for new learners) is that the index does not refer to the item. The index instead refers to the space between elements. That is, in the string If you use a single index ( Though that explanation isn't really a justification for 0-based indexing, it does help show things like the half-open interval. |
I think (CS people, correct me if I'm wrong) this is the actual technical explanation. The index was actually the shift in memory to get the element from the beginning of the data structure. The first element To get the Maybe in this case telling the real thing is a good way to explain it. |
|
Actually, to advocate a bit for zero-indexing, I think it's what it makes sense also from a mathematical point of view. The index labels things inside the structure, so it's a relative index for the elements. This means that if I move the structure as a whole somewhere else the indexing stays be the same. Given that, it makes sense to use a significant point in the structure as the "origin" of my indexing system and index the remaining elements with the relative position to this significant point. I'm free to use any value to index this significant point, but the most natural thing is set it to zero. In maths/physics we do this all the time. For example, we set the origin of coordinates of the Solar System to be the center of the Sun (the center of mass of the whole Solar System), so the Sun's position is indexed as In a Python (ordered) sequence |
|
Mike Hoye from Mozilla dug into this a while back, and the real |
|
Merged, thanks @khinsen! I also added a commit linking to Mike Hoye's blog post. |
The argument "that’s simpler for computers to do" is not credible.
Compilers and interpreters can easily handle an offset of one.
The choice of index base is made with human convenience in mind.