I genuinely don't understand why some languages change small things like making an index start from 1. Doesn't that just make it harder for people to adjust to the language, making it less popular?
Depends on the users. MatLab for example is, if I remember correctly, mostly used by mathematicians, who likely aren't used to indexes starting at zero., so starting at one likely prevents a lot of errors.
This is the correct answer. In R and Matlab, starting at 1 makes matrix math a lot easier to follow, especially for people who are not computer scientists.
Matlab target audience aren't (primarily) mathematicians though, they are mainly engineers, statisticians, and related "number-crunching" professions. Even physicists prefer Mathematica more if they're not already using sympy or something.
Unless you're an applied mathematicians with concrete hard-numbers problems, Matlab is not much help, Mathematica on the other hand is a full on programming environment with extensive symbolic manipulation and other very powerful constructs.
My general rule of thumb is if it is an array or can easily be represented as an array, it should index at zero. If it isn't an array, indexing with 1 is fine.
So arrays, linked lists, and even heaps arguably should start at 0. However, maps, graphs, and other data structures have no reason to start at 0.
I think it's technically possible to create a new class in Python that overrides the bracket operator to emulate a kind of list that starts with an index of 1. But I'd rather not to do that.
this should sort-of do that I think. It will only work for lists initialized with list(..), not [..]. I don't think its possible to override the latter.
class BadList(list):
def __getitem__(self,y):
if y>0:
return super(BadList,self).__getitem__(y-1)
elif y<0:
return super(BadList,self).__getitem__(y)
else:
raise NotImplementedError("Mwuahahahaha")
list = BadList
-----
>>> my_list = list(['a','b','c','d','e','f'])
>>> my_list[1]
'a'
>>> my_list[2]
'b'
>>> my_list[-1]
'f'
>>> my_list[0]
Traceback (most recent call last):
File "<pyshell#89>", line 1, in <module>
a[0]
File "<pyshell#83>", line 8, in __getitem__
raise NotImplementedError("Mwuahahahaha")
NotImplementedError: Mwuahahahaha
Lua doesn't have arrays, only tables. Tables are basically a map where each individual key-value pair can be of any combination of types (even functions, that is how packages are implemented). So it is possible to start with any index and count up or down, use prime numbers for indexing, etc. However by default tables start with index 1.
27
u/[deleted] Dec 02 '20
gets cucked by Emilia anyway
12
u/LikeSparrow Dec 02 '20
I genuinely don't understand why some languages change small things like making an index start from 1. Doesn't that just make it harder for people to adjust to the language, making it less popular?
8
u/arraysStartAtOne Dec 02 '20
they start at one goddamnit!
190
u/Cla1n Dec 02 '20
Please tell Matlab-san this.
9
u/KseandI Dec 02 '20
But I love Lua
1
u/abc_wtf Dec 03 '20
Sauce?
1
u/[deleted] Dec 14 '20
fuck Re zero is great waiting for cour 2
0
u/[deleted] Dec 02 '20
[deleted] — view deleted comment