5 ms·
1-based arrays are one of the more lambasted traits of Lua (an otherwise supremely elegant language), and as a long-time C/C++ programmer, I personally find 0-b
by snogglethorpe 14y ago
1-based arrays are one of the more lambasted traits of Lua (an otherwise supremely elegant language), and as a long-time C/C++ programmer, I personally find 0-based arrays very natural.
However what I've found from doing lots of Lua programming, is that I just don't notice very much in practice.
To some degree, at least, I guess this reflects that one writes different sorts of code in different programming languages. The sort of offset-based address calculations one might do a lot in C just don't seem to happen very much in Lua.
So while my natural inclination is to dislike 1-based arrays, I no longer consider them an automatic demerit....
- davidw 14y agoErlang has 1-based lists as well.
- davidw 14y agoI'm not sure who downvoted this, but: > lists:nth(1, [a, b, c]). a > lists:nth(0, [a, b, c]). ** exception error: no function clause matching lists:nth(0,[a,b,c]) (lists.erl, line 117)
- klibertp 14y agoIt's worse in Erlang: "Mainly, Regex and Binary indexes are zero-based, List and Tuple are one-based." I don't remember now but one of the collections in stdlib is zero-based too. It's much worse to be inconsistent than to choose 0 or 1-based indexes for everything.
- davidw 14y ago> one of the collections in stdlib is zero-based too. Yep, array: http://www.erlang.org/doc/man/array.html http://www.erlang.org/doc/man/array.html It's definitely a mess.