Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I find that the only reasonable complaint that can be made about Lua is the from-1 indexing. Like: "I found this really awesome language and it's ONLY wart is that it doesn't index arrays like every other programming language in the world".

Yet, I still admire the Lua creators for making this bold choice. Why should we use from-0 indexing for all eternity? Just because it's always been that way? This is a real question if you have the answer.



> Why should we use from-0 indexing for all eternity? Just because it's always been that way? This is a real question if you have the answer.

Well, we obviously can use 1-based indexing. Ada/VHDL allows you to define the ranges of your indicies even beyond 0 or 1 based (very useful in fixed point mathematics where you sometimes want to index your bits by a negative power of 2).

The big disadvantage is that you lose the algebraic properties of mapping to the ring of integers modulo N when your indicies are 1-based.

This leads to a bunch of off-by-one bugs in various places. One of the most pernicious is that while you can get to the next index with "next = prev % N + 1" you actually have to do "prev = (next - 1) % N". Note that the previous REQUIRES the parentheses due to precedence of operations.

0-based may be convention, but it isn't ill thought out. FORTRAN(1-based) and C(0-based) coexisted a LONG time. Pascal was(is?) 1-based. Most people had experience with both.

People who designed programming languages CHOSE the 0 convention. Overwhelmingly.


After seeing the light with Ada I never want to go back - indexing based on enums, modulos, etc. just makes so much more sense (as long as you're using something strongly typed)


> Why should we use from-0 indexing for all eternity? Just because it's always been that way? This is a real question if you have the answer.

Here is the answer: https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E...


In addition to Lua, these languages are 1-based: ALGOL, AWK, COBOL, Fortran, Julia, Mathematica, MATLAB, R, PL/I, and RPG.


BTW, FORTRAN is the big reason Lua has 1-based indexing. Lua' s first users were engineers, which were more familiar with FORTRAN.


And Smalltalk


> I find that the only reasonable complaint that can be made about Lua is the from-1 indexing.

Lack of arity checks is definitely much more problematic than this, but still... if you can count the number of tragic design flaws in your language on one hand, you're in great shape compared to the rest of what's out there!


No, we use from-zero indexing because, A) it's closer to the hardware, and B) because of mod(). There was an interesting comment thread on ESR's blog about this a while ago, but unfortunately, I can't find it.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: