4 ms·
Let's see if Dijkstra can explain it. :p https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html https://www.cs.utexas.edu/users/EWD/transcripti
by moomin 5y ago
Let's see if Dijkstra can explain it. :p
https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E...
- pull_my_finger 5y agoThis seems to get posted a lot in 0 vs 1 threads as a way to end the conversation in appeal to authority, but for arguments sake, Roberto Ierusalimschy (of Lua) has explained his design choice: > When we started Lua, the world was different, not everything was C-like. Java and JavaScript did not exist, Python was in an infancy and had a lower than 1.0 version. So there was not this thing when all the languages are supposed to be C-like. C was just one of many syntaxes around. > And the arrays were exactly the same. It’s very funny that most people don’t realize that. There are good things about zero-based arrays as well as one-based arrays. > The fact is that most popular languages today are zero-based because of C. They were kind of inspired by C. And the funny thing is that C doesn’t have indexing. So you can’t say that C indexes arrays from zero, because there is no indexing operation. C has pointer arithmetic, so zero in C is not an index, it’s an offset. And as an offset, it must be a zero — not because it has better mathematical properties or because it’s more natural, whatever. > And all those languages that copied C, they do have indexes and don’t have pointer arithmetic. Java, JavaScript, etc., etc. — none of them have pointer arithmetic. So they just copied the zero, but it’s a completely different operation. They put zero for no reason at all — it’s like a cargo cult. https://habr.com/en/company/mailru/blog/459466/ https://habr.com/en/company/mailru/blog/459466/
- balfirevic 5y agoThat doesn't explain why he picked one-based arrays, it only says the reasons C has zero-based arrays don't necessarily apply to Lua.
- ufo 5y agoI have to confess that this EWD on 0-1 indexing is one of my pet peeves. Whenever the topic comes up, someone will invariably link to it uncritically, even though Djikstra's argument is biased in favor of 0-based indexing. For example, he talks about how (a < i ≤ b) is unnatural, but then fails to mention that this happens with 0-based indexing when we have to iterate backwards.