lua-users home
lua-l archive

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


No, 0 is not a valide sequence number... Sequences in Lua are restricted to ordinals from 1 to N (without holes).

Note that the first comment about length of numbers was still accurate: it said it used a default conversion of numbers to string. But all the problem is the formatting of this number. PHP made a choice for such representation of numbers as strings, Java as well, for their builtin converters. C/C++ did not make any choice, as the result is dependant of the context and there's no default format.

In Lua there's also a default format, the one you see in consoles when printing numbers: this is this format that allows a length to be meaningful.

But string lengths are conceprtually the same as sequence lengths in Lua tables (strings in lua are a compact form to store an indexed table of characters enumerable as a valid sequence).

For Lua numbers however you need a converter to generate a sequence (of digits in some base, or signs, or decimal and grouping separators, or exponent prefix). Without it you cannot enumerate numbers with a well defined positional index for use as if they were table keys.

Note that even if sequence numbers cannot be 0, the sequence length can be 0 of the sequence of empty or is equal to the last key of the enumerated sequence.



Le mar. 7 mai 2019 à 20:56, Coda Highland <chighland@gmail.com> a écrit :
On Tue, May 7, 2019 at 1:12 PM Philippe Verdy <verdy_p@wanadoo.fr> wrote:
In my opinion the "length" of a number can only be the length of the string generated when converting this number to a string with the default string conversion.
Note that keys in tables are not necessarily integers, they can be any number. It is an integer only to restrict tables to their sequence, but not all tables have a unique sequence, if there are "holes" such that there exists a integer key k in table t, where k >= 2, t[k] is not null, and t[k-1] is null).
Codes that expect sequences to give a meaning to "#t" cannot be portable if t is not a sequence with no holes. But there's no easy builtin properties of tables that can assert it forms a unique predictable sequence (note that the presence of non-integer keys in the table does not invalidate a sequence: a table is a valid sequence depending only on the subset of its keys that are integers, and keys below 1 are ignored: you can safely add a key 0 or -1 to any table that is a sequence without invalidating the sequence and what #t will return).
This behavior of tables in Lua and the very weak definition of "sequences" is very tricky.

Except that #s being the length of a string makes sense, because a string is a sequence of characters. But if numbers are considered to be scalar values (as they usually are) then that means they aren't sequences, and the Lua definition of # being the length of a sequence is meaningless.

That said, if you step outside of the concrete implementation of numbers in computers, you could use the set-theoretic definition of the natural numbers to come up with another interpretation of #n -- which would be n itself. Under the Zermelo-Fraenkel axioms, the natural numbers are built up of sets. 0 is defined as the empty set, and then every natural number afterward is defined as the set of all natural numbers smaller than it. This means the number of elements in the set representing a natural number IS that natural number, and since natural numbers have a defined ordering, that means that a natural number is a sequence.

What of negative numbers and non-integer numbers? Well, if we consider a sequence to be something iterable, and the length of a sequence to be the number of elements inspected when iterating over it, then #r would be floor(r) for positive non-integer numbers, as the default notion of iterating over the numbers involves incrementing by 1 and stopping once you've exceeded that number. (It WOULD be ceil, since Lua includes the upper loop bound, except Lua is also 1-indexed, which means that the iteration count ends up being the same.) #r would be 0 for negative numbers (whether integer or not) as a for loop with 1 as its lower bound and with a negative number as its upper bound will iterate 0 times.

So #n = (n > 0 and floor(n) or 0) is the only definition of # on a number that I can think of that would be consistent with the other sequence-related uses of the # operator in Lua.

/s/ Adam