Comment by VWWHFSfQ

1 year ago

> Doesn't make the mistake of 1-based indexing.

A lot of people would call this a feature, not a mistake. 0-based indexing came from programming languages typically calculating addresses in the memory layout of an array. Whereas in mathematics (the background of the Lua inventor) sequence positions typically start at 1. Also, humans generally start counting from 1. This is intuitive in languages like SQL which also use 1-based indexing.

It could be argued that 0-based indexing was the mistake since it actually conflates two concepts, the memory layout in the machine, and the actual sequence you want to index.

Those people are wrong. Maths is wrong too but it doesn't matter so much for maths because you can hand wave syntax to do whatever you want. Guess what style of indexing formal mathematical proof systems use...

Fundamentally either work, but 1-based indexing is a mistake mainly because it leads to much less elegant code.

A simple example is accessing rows in a flattened matrix (or 2D array).

With 0-based intervals and right-open intervals it is

  array[stride*y .. stride*y + width]

With 1-based it is

  array[stride*(y-1)+1 .. stride*(y-1)+1+width]

Ouch. Code dealing with intervals is simpler with 0-based indexing & right-open intervals 99.9% of the time.

Take a look at the example code here:

https://www.lua.org/pil/11.2.html

What index does the flattened `mt` start at?

  • I think you're still conflating two different concepts, but this is mostly natural at this point since I assume you program in languages that use 0-based indexing. 1-based indexing focuses on the conceptual sequence rather than the underlying implementation details. Even your matrix flattening example is conflating concepts, although that's "normal" at this point. 0-based indexing certainly lends itself nicely to interval arithmetic, I agree.

    > What index does the flattened `mt` start at?

    I'm not sure if I missed a point you were getting at with this? Lua array table indexes start at 1.

    • My first boss who started in the late 60's said 1 based indexing exists because the early programmers with mathematics training thought ordinary people are too stupid to get zero based indexing.

      1 reply →

    • > I'm not sure if I missed a point you were getting at with this? Lua array table indexes start at 1.

      The code example is

          mt = {}          -- create the matrix
          for i=1,N do
            for j=1,M do
              mt[i*M + j] = 0
            end
          end
      

      What index do I use to access the first element in mt? It isn't 1!

      If this was written using 0-based indices then the answer would be zero.

I agree that it's wrong to label Lua's choice a mistake—when it was created in 1993 there wasn't as overwhelming a consensus in favor of zero-based indexing as there is now. But now the consensus is there, so whether it's a mistake or not it's not worth fighting against. Programmers today learn zero-based indexing and trying to get someone who's used to that to adapt to one-based indexing is not trivial.

>It could be argued that 0-based indexing was the mistake since it actually conflates two concepts, the memory layout in the machine, and the actual sequence you want to index.

This could be argued to be a feature. That it isn't conflating the concepts but communicating both.