News for hackers. Stuff that matters.
Re:Nelua Programming Language by ysleepy on Tuesday August 24, 2021 @04:31PM (#28294610)
Uses 0-based indexing, if you were wondering like me.
Re:Nelua Programming Language by 7thaccount on Tuesday August 24, 2021 @04:44PM (#28294772)
I'll never understand why this is such a big item of importance for developers :). Both 0 & 1 are pretty natural in my eyes.
Re:Nelua Programming Language by jimsimmons on Tuesday August 24, 2021 @04:57PM (#28294913)
Off by one errors are one of the most common type of errors. So it's a real gotcha for most programmers
Re:Nelua Programming Language by brabel on Wednesday August 25, 2021 @01:12AM (#28298257)
Off by one errors are also the easiest thing in the world to test. I never think much about whether I should do `n` or `n - 1`, I just write a test and then fix it if needed.
Re:Nelua Programming Language by 0-_-0 on Tuesday August 24, 2021 @05:34PM (#28295288)
Because modulo arithmetic needs to include 0, so if you do modulo arithmetic on indices 0 based indexing is better.
Re:Nelua Programming Language by romwell on Tuesday August 24, 2021 @06:11PM (#28295617)
One solution to that is have inherently modular array indexing, so that any index is valid.

If A[1] is the first element, then A[0] would be the last; etc

Re:Nelua Programming Language by mst on Tuesday August 24, 2021 @06:24PM (#28295742)
I do appreciate languages that allow -1 for the last index.
Re:Nelua Programming Language by layer8 on Tuesday August 24, 2021 @06:30PM (#28295794)
That makes it difficult to calculate the distance (length of the slice) between two arbitrary indices though.
Re:Nelua Programming Language by romwell on Wednesday August 25, 2021 @03:14AM (#28298834)
Well, that's when you can use the mod operator instead of having to do it when indexing into an array (a much more common operation).
Re:Nelua Programming Language by 0-_-0 on Wednesday August 25, 2021 @05:56AM (#28299640)
That's a good idea!
Re:Nelua Programming Language by lonk on Tuesday August 24, 2021 @04:57PM (#28294910)
Because 0 is first natural number and 1 is second :P
Re:Nelua Programming Language by dkarl on Tuesday August 24, 2021 @05:12PM (#28295063)
Very unfortunate choice of terminology in this context. Most of the math textbooks I used in college used "natural numbers" to mean the positive integers. Others used it to mean the nonnegative integers. So the term "natural numbers" presents the same uncertainty to a reader as indexing in an unknown programming language does :-)
Re:Nelua Programming Language by epage on Tuesday August 24, 2021 @07:28PM (#28296278)
When you casually program in a language, being consistent with what people are familiar with is important to avoid introducing subtle bugs.

A lot of times, languages like Lua get introduced by enthusiasts in a way that others have to touch when things break but not enough to master. Think of shops that have those one off Perl scripts and the challenge with touching them.

Re:Nelua Programming Language by moomin on Tuesday August 24, 2021 @07:14PM (#28296154)
Re:Nelua Programming Language by pull_my_finger on Tuesday August 24, 2021 @07:53PM (#28296490)
This 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/

Re:Nelua Programming Language by balfirevic on Sunday August 29, 2021 @09:30AM (#28346530)
That 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.
Re:Nelua Programming Language by ufo on Tuesday August 24, 2021 @08:47PM (#28296841)
I 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.

Re:Nelua Programming Language by IshKebab on Tuesday August 24, 2021 @05:34PM (#28295286)
I guess it's because there isn't a huge difference but 0 definitely is slightly more elegant than 1. And when you're used to programming with the elegant system it's really jarring to switch to an unusual and slightly worse system.
Re:Nelua Programming Language by void_mint on Tuesday August 24, 2021 @07:12PM (#28296140)
Elegance is a term developers love to throw around to get out of having to define why something is or is not good. Your answer is nothing more than "I like it more".
Re:Nelua Programming Language by IshKebab on Wednesday August 25, 2021 @06:15AM (#28299767)
Nonsense. Are you saying the concept of elegance doesn't exist? I didn't explain why 0 is more elegant but that doesn't mean I'm not saying that it is more elegant.

One example is when you're processing arrays or matrices in blocks. Using right-open intervals (also the most elegant option) you end up with intervals like [0, 4) [4, 8) [8, 12). Pretty trivial to calculate those bounds! It's just 4N.

With 1-based you end up adding and subtracting 1 all over the place.

Another example is indexing into flattened arrays. To get to the relevant element it's just (row_len * col + row). With 1-based indices it's (row_len * (col - 1) + row).

Basically I've done a lot of 1-based programming (in Matlab), and you definitely end up adding and subtracting 1 in places where you wouldn't need to with 0-based programming.

Re:Nelua Programming Language by void_mint on Wednesday August 25, 2021 @11:26AM (#28302830)
> Are you saying the concept of elegance doesn't exist?

No...You can feel like something is elegant. Others may not feel that way. Saying "This is elegant" doesn't explain why, and since it's totally subjective it's just kind of a meaningless thing to say. Similar to measuring something by how "interesting" it is.

The rest of your post just verifies that your original post should've been "I just like it more."

Re:Nelua Programming Language by IshKebab on Wednesday August 25, 2021 @03:24PM (#28305771)
> Saying "This is elegant" doesn't explain why

I never claimed it did!?

Re:Nelua Programming Language by void_mint on Wednesday August 25, 2021 @05:51PM (#28307560)
Your OP:

> I guess it's because there isn't a huge difference but 0 definitely is slightly more elegant than 1.

"0 definitely is slightly more elegant than 1."

Totally subjective. You like it more.

Re:Nelua Programming Language by IshKebab on Thursday August 26, 2021 @04:12AM (#28311987)
> > I guess it's because there isn't a huge difference but 0 definitely is slightly more elegant than 1.

I don't see any claim by me that I was explaining why it is more elegant.

> > "0 definitely is slightly more elegant than 1."

> Totally subjective. You like it more.

Would you say the same about beauty?

> "Margot Robbie is more beautiful than Susan Boyle"

> Totally subjective. You like her more.

Let me clear things up for you:

1. Just because you don't explain something (why 0-based is elegant) doesn't make it any less true or more subjective.

2. Lots of things like beauty and elegance are PARTIALLY subjective. There is room for debate. But that doesn't mean that everyone completely disagrees with each other and there's no shared consensus.

3. Elegance doesn't just mean "you like it more". Beauty and elegance are reasons to like things more.

This is a weird debate.

Re:Nelua Programming Language by void_mint on Thursday August 26, 2021 @10:53AM (#28315725)
> Let me clear things up for you:

If by "clear things up", you mean behave with hostility towards someone pointing out your bullshit, sure, you sure did.

> I don't see any claim by me that I was explaining why it is more elegant.

Yes, from _my_ post.

> Elegance is a term developers love to throw around to get out of having to define why something is or is not good. Your answer is nothing more than "I like it more".

> Would you say the same about beauty?

Philosophical debates are cute and this is whataboutism. You're using a subjective term to compare two concepts as though that term is shared universally by others. It's not. To quote me: You like it more.

> 1. Just because you don't explain something (why 0-based is elegant) doesn't make it any less true or more subjective.

Not don't, can't. Or more directly, it's subjective. You like it more. You saying why you think something is more elegant doesn't make any other person agree with you. Because it's subjective.

> 2. Lots of things like beauty and elegance are PARTIALLY subjective. There is room for debate. But that doesn't mean that everyone completely disagrees with each other and there's no shared consensus.

Cute whataboutism.

> 3. Elegance doesn't just mean "you like it more". Beauty and elegance are reasons to like things more.

Subjective reasons to like something more.

> This is a weird debate.

It would be a debate if it were possible to have debates about things that were subjective, but it's not, which is my whole point. You like it more.

Re:Nelua Programming Language by rxhernandez on Tuesday August 24, 2021 @04:55PM (#28294889)
It can definitely create problems when translating an algorithm from one to the other. I know it was a frustration when I translated algorithms from Matlab to C++.
Re:Nelua Programming Language by andre-la on Tuesday August 24, 2021 @06:46PM (#28295914)
Is 0-based like C, but on libraries that reimplements the Lua libraries, it's 1-based as Lua, because the language aims Lua compatibility.

Due to this, the string and "sequence" libraries (which works as a Lua-like vector), and the table library in the future, are 1-based.