Why not Lua?

Post all other topics which do not belong to any other category.
PunPun
Long Handed Inserter
Long Handed Inserter
Posts: 69
Joined: Sun Mar 27, 2016 7:08 pm
Contact:

Re: Why not Lua?

Post by PunPun »

neoc wrote: Sat Sep 22, 2018 6:16 pm
darkfrei wrote: Sat Sep 22, 2018 9:36 am
WeirdConstructor wrote: Fri Sep 21, 2018 6:55 pm Yes, the weird 1 based indexing and ~= are warts, and they hurt experienced programmers like me. But on the other hand JavaScript and Python have even more warts if you look closely.
I found that indexing from 1 is much easier for real practice, but normally I don't use the index, just the values of elements. But here can be also "a-b-c" indexing, when I want it.
At first I felt like any language that doesn't use zero-based indexing is just bad and I wouldn't want to use it, but after a while you realize that it's really no big deal. For example if you switch from zero-based to 1-based in an indexing loop, you only have to change the comparism operator:

Code: Select all

for(i = 0; i < 10; ++i)

Code: Select all

for(i = 1; i <= 10; ++i)
Once you get used to it, it's just a context switch in your thinking.
The "indexing starts from zero" actually comes from how memory is accessed in c/c++ programs(actually it comes from assambly but the same reasons still aply). In c/c++ and a lot of other languages the following two lines of codes are functionally identical

Code: Select all

print(table[i])

Code: Select all

print(table+i)
This is because the pointer for an array is actually a pointer to its first object. The pointer does not know that it is pointing to an array. So for a language to have nonzero indexing you either need to add padding so the index lines up with the memory, applying an offset to each call to access the array or a more complex object to hold the array that handles the memory and indexing.

The zero based index comes from a purely practical reason of not having to do extra steps or waste memory on padding. This was especially important back in the 80's and early 90's when computers were slow and memory was expensive.

Also two dimensional arrays become simpler with zero based indexing. The memory location for an array[xsize][ysize] is array+xindex+yindex*xsize. When a 1 based index array it becomes array+(xindex-1)+(yindex-1)*xsize unless you add an xsize+ysize+1 worth of padding to the array.

The cost of nonzero based indexing is nonzero.
User avatar
CDarklock
Filter Inserter
Filter Inserter
Posts: 342
Joined: Sun Dec 03, 2017 2:17 am
Contact:

Re: Why not Lua?

Post by CDarklock »

caHarkness wrote: Wed Feb 27, 2019 11:17 pm Unlike Python, Lua's syntax is very flexible and virtually understood by every programmer.
There are two kinds of programmers in the world: programmers who find ALGOL-60 intuitive, and programmers who find LISP intuitive.

Lua is for the latter.

The problem is that when you dig into the way those people think, it's kind of horrifying in terms of how they solve problems. Frequently, the correct solution isn't even possible, because you would need to dramatically restrict the possibilities.

LISP descendants don't do that; they're artsy hipster languages that sit around smoking weed and being relaxed and groovy about everything. You say something like "for dinner, we will be having dictionary with a side of light bulbs," and they go "wow, far out, man."

In the ALGOL-60 lineage, the language comes stomping up to you with a buzzcut and screams "THAT IS NOT DINNER. THAT IS NOT HOW YOU MAKE DINNER. YOU GO TO HELL. YOU GO TO HELL AND YOU DIE." And all the artsy hipster people burst into tears and run away.

Nobody likes to watch anyone burst into tears and run away, but every time someone shows you their dinner and it's a dictionary with light bulbs, you die a little inside. And when you're choosing a scripting language, it's really common to say "let's not make people cry; they are smart, nobody is going to make dinner out of OH GODDAMMIT" because there is literally already someone in the corner with a dictionary and some light bulbs.
nerhee
Manual Inserter
Manual Inserter
Posts: 3
Joined: Wed Feb 27, 2019 6:11 am
Contact:

Re: Why not Lua?

Post by nerhee »

PunPun wrote: Thu Feb 28, 2019 8:28 am
neoc wrote: Sat Sep 22, 2018 6:16 pm
darkfrei wrote: Sat Sep 22, 2018 9:36 am
WeirdConstructor wrote: Fri Sep 21, 2018 6:55 pm Yes, the weird 1 based indexing and ~= are warts, and they hurt experienced programmers like me. But on the other hand JavaScript and Python have even more warts if you look closely.
I found that indexing from 1 is much easier for real practice, but normally I don't use the index, just the values of elements. But here can be also "a-b-c" indexing, when I want it.
At first I felt like any language that doesn't use zero-based indexing is just bad and I wouldn't want to use it, but after a while you realize that it's really no big deal. For example if you switch from zero-based to 1-based in an indexing loop, you only have to change the comparism operator:

Code: Select all

for(i = 0; i < 10; ++i)

Code: Select all

for(i = 1; i <= 10; ++i)
Once you get used to it, it's just a context switch in your thinking.
The "indexing starts from zero" actually comes from how memory is accessed in c/c++ programs(actually it comes from assambly but the same reasons still aply). In c/c++ and a lot of other languages the following two lines of codes are functionally identical

Code: Select all

print(table[i])

Code: Select all

print(table+i)
This is because the pointer for an array is actually a pointer to its first object. The pointer does not know that it is pointing to an array. So for a language to have nonzero indexing you either need to add padding so the index lines up with the memory, applying an offset to each call to access the array or a more complex object to hold the array that handles the memory and indexing.

The zero based index comes from a purely practical reason of not having to do extra steps or waste memory on padding. This was especially important back in the 80's and early 90's when computers were slow and memory was expensive.

Also two dimensional arrays become simpler with zero based indexing. The memory location for an array[xsize][ysize] is array+xindex+yindex*xsize. When a 1 based index array it becomes array+(xindex-1)+(yindex-1)*xsize unless you add an xsize+ysize+1 worth of padding to the array.

The cost of nonzero based indexing is nonzero.
Not true at all. You don't need to actually allocate padding, and any difference between zerobased[0] and onebased[1] will disappear with constant folding.
ratchetfreak
Filter Inserter
Filter Inserter
Posts: 952
Joined: Sat May 23, 2015 12:10 pm
Contact:

Re: Why not Lua?

Post by ratchetfreak »

nerhee wrote: Thu Feb 28, 2019 9:05 am

Not true at all. You don't need to actually allocate padding, and any difference between zerobased[0] and onebased[1] will disappear with constant folding.
There are a plethora of minor things that 0 based vs. 1 based indexing changes. But they mostly boild down to where you are going to make off by one errors.

For example when you need to add indices, with 1 based you have to consider whether both indices are 1-based index (where is element n of the array starting at index m: at index n+m-1) or one is an offset from the other, with 0 based all indices are also offsets so that becomes a no-brainer.

The one data structure that I can think of that is helped by 1-based indexing is the min heap.
PunPun
Long Handed Inserter
Long Handed Inserter
Posts: 69
Joined: Sun Mar 27, 2016 7:08 pm
Contact:

Re: Why not Lua?

Post by PunPun »

nerhee wrote: Thu Feb 28, 2019 9:05 am
PunPun wrote: Thu Feb 28, 2019 8:28 am Snip.
Not true at all. You don't need to actually allocate padding, and any difference between zerobased[0] and onebased[1] will disappear with constant folding.
Except that then for a 2d array you need to allocate several smaller blocks of memory and there is a good chance they are not close together and you lose performance as opposed to having one continous block of memory. Or you need to add some extra instructions to find the correct memory location again losing performance.

Constant folding is a compile time optimisation that can only be done when the values are known at compile time. What if you have a pointer to a dynamically allocated array with a size unknown until runtime and have to access random elements in it. There is no way to do constant folding on that at all. And for a lot of programs this is a very real and frequent thing. Besides when zerobased indexing was "invented" there weren't really any compilers that could do it even if the values were known in advance.

Performance and complexity wise when the first c compiler was being made zero based indexing was just better in every way.
Post Reply

Return to “General discussion”