1. 18
  1. 6

    The 0 vs 1 indexing is just bike shedding at this point IMO. It is very simple to understand what a language uses and go with it. There is absolutely no need to go over the top with rage because a language is not using one’s preferred indexing.

    0-based indexes have their place. Especially when dealing with C, arrays, and pointer addresses. 1-based indexes also have their place, I for one find them easier to reason about. I don’t go on C forums demanding that C25 becomes 1-based…

    Really tired of those arguments. And it is always with Lua. I posted about it in the past: Lua, a misunderstood language.

    1. 5

      0-based indexes have their place. Especially when dealing with C, arrays, and pointer addresses.

      I’ve more or less settled on the opinion that “having to care about what index your data structure starts at” is the mark of a low-level language. High level code uses iterators and destructuring to access the data structures such that the details of where the indexing starts at is almost never relevant. For instance, this code:

      (let [color (. colors idx)
            color-str (string.format "#%02X%02X%02X" (. color 1) (. color 2) (. color 3))]

      should be instead

        (let [[r g b] (. colors idx)
              color-str (string.format "#%02X%02X%02X" r g b)]

      The author of the article got confused because they were used to using languages which have been influenced by C, not because of any inherent advantage of one system over another.

      1. 2

        Just for the sake of completion, I just want to mention that Lua has iterators. You can iterate using pairs() and ipairs(), example:

            a = {"one", "two", "three"}
            for i, v in ipairs(a) do
              print(i, v)
      2. 1

        Cool to see NewtonScript as the metatable example, thank you :). Interesting perhaps to note that NS originated from a Lua-like goal, a simple language used to glue together “native” components to construct an app. It grew to be a little more complex than Lua because of its context (e.g., dual/proto inheritance to save RAM), but still the Newtonian functionality was arguably all library/interop.

        1. 1

          omg @wrs, I really like your language. I carried a Newton way into 2006, just stopped using the thing because someone sat on it. Still have an eMate 300 here, it is one of my favourite writing machines. NewtonScript deserves way more love than it gets. I remember it very fondly. The dual proto inheritance was genius, I often think about it when developing components for web usage, it was so much simpler.

          Anyway, thanks for building one of my favourite devices.

          1. 1

            I’d like to know more about “dual proto inheritance.” Is it like in Io, which has a list of parents? https://iolanguage.org/guide/guide.html#Objects-Prototypes

            1. 1

              It is not exactly like Io. The inheritence in Io is more flexible. In NewtonScript, the dual inheritance has a purpose that makes it easier to write GUI code as you can have something like a form in which the fields and buttons all inherit from their prototype objects, but also from the form container thus allowing you to handle them in the form container script. Err, I think I’m not explaining it well. You might have a better understanding from checking Chapter 5 of https://www.newted.org/download/manuals/NewtonScriptProgramLanguage.pdf

              1. 1

                Thanks for replying!

      3. 3

        Just something I did for fun last weekend. The output of this code can be viewed here: https://alopex.li/temp/palette.html

        1. 2

          Nice. Here’s a visualization I did a while ago for the VGA palette on x86: http://akkartik.github.io/mu/html/vga_palette.html

          1. 1

            comfy. thanks for sharing

          2. 2

            By an interesting quirk of mathematics, it seems the Plan9 palette happens to include the traditional IBM PC RGBI 16-colour palette colours exactly, so a DOS emulator running under Plan9 would produce more accurate visuals than a command prompt window in Windows 95.

            1. 1

              I find it interesting that the longest comment on there is about 1-based indexing.

              I very much agree with the author about the strength of 0-based indexing for general purpose programming.

              I wonder if fennel the language, and its compiler, could hypothetically easily allow one to use 0-based indexing and transform it to the lua-native 1-based index.

              1. 2

                That would run counter to what Fennel, as a language, intends to do. Fennel is intended as a small layer over Lua, not to use Lua only a compilation target.

                This enables some nice things, like being able to easily use existing Lua libraries without having to write interop layers. A change like you’re suggesting would turn Fennel into something fundementally different, because it’s not just Fennel that you have to change, it’s also how you interact with the greater Lua ecosystem.

                1. 1

                  I did not mean to say that I thought it should use 0-based indexing.

                  I totally understand the fact that it is a small layer, aiming to provide easy interoperability. I was mostly wondering about what it would entail, from a PL/compiler design standpoint, to create a language which uses a different basis. I think the rote translation of array[i] to array[i + 1] is no challenge. The problem begins once you need to interact with the rest of the ecosystem : any foreign function call could be using an integer as an index!

                  1. 2

                    Well, and I think it’d fundamentally be at odds with the rest of the Lua ecosystem in two ways:

                    1. Most Lua libraries don’t have the typing annotations that it’d take to get this sort of information in place.
                    2. Fennel itself doesn’t track types closely enough to enable this, as far as I’m aware.

                    Maybe the Type Script To Lua compiler might be able to annotate enough type info to do that? But I’d expect that you’d need language that tracks indices, and very few languages do that, and compile to Lua. Rust comes to mind as an example of something that might track types enough for this sort of thing?

                    It is an interesting thought experiment.

                    1. 1

                      It’s a reasonable question, just to consider what it would take to do this.

                      You can’t do this fully at compile time, which is the way Fennel works. You would have to change the compiler to turn table literals to emit zero-indexed Lua data structures and change destructuring to work that way, but you would also need to override the ipairs iterator and the table.unpack function (and maybe a couple more I’m overlooking) which could only be done at runtime. And of course as you’ve noted, now crossing language boundaries becomes a big headache.

                      In practice no one actually does this, because by the time you learn enough about Lua to understand how to do it, you also understand that there’s no reason to.