lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Thu, May 3, 2012 at 12:44 AM, Dirk Laurie <> wrote:
> Miles replying to Laurens:
>>> I can't hope to convince you to use index base zero if you already
>>> like index base 1, you are in your right.
>> Actually I like base-0 better -- but not more than I like being able
>> to use existing code.... :]
> Lua is only very mildly base-1.  It affects table constructors,
> the `table` library, and (I'm told) efficiency.
> But nothing stops you from using index 0, or even index -1, -2, etc.
> This is crucial.
> I recently had to translate a program from Lua to a real base-1
> language. The central bit of Lua code is
>     p = {[-1]=0, [0]=1};
>     for k=0,n-1 do
>        p[k+1] = (x-a[k])*p[k] - b[k]*p[k-1]
>     end
> which is exactly the way the math is written in the textbook, except
> on the page it appears with subscripts.  Writing the Lua code was
> a no-brainer.
> You can imagine what a PITA it is to do that in a language that does
> not allow you to write or refer to p[-1] and p[0].  Are we going to
> increase all indices by 2, and remember to do that on all the formulas
> lower down also?  Sorry mate, the bugs won't just crawl in, they will
> call their friends and their friends' friends too.  Are we going to
> have extra variables pm1 and p0?  At least the bugs will show up as
> references to non-existent indices so you have a chance of catching
> them.  Are we going to write a nice object-oriented class?  That's
> the canonic solution.  Hours and hours of "productive" programming;
> I hope you get paid for doing it.
> The problem is much the same in an base-0 language, although the
> p0 solution is now less inconvenient than in base-1.  But it does
> not go away: the textbook author uses negative indices in many other
> places too, and the value is not always 0.
> You don't properly appreciate how wonderfully versatile a Lua table
> is until you need to go back for a while to a language that hasn't
> got it.  The Lua solution, to allow any index, is so clean and
> convenient.  It doesn't matter much what the index base is.
> Miles again:
>> In practice, I find I rarely notice the difference when writing Lua code.
> Amen to that.

For that matter, it doesn't even matter what type. Being able to
define something like:
myTable = {]-1] = 3, [0] = 'a string', 27, 42, 69, someAttribute = 'orange'}
is extremely useful.

Sent from my Game Boy.