Tables are a strong unifying data constructor. They directly implement a multitude of data types, like ordinary arrays, records, sets, bags, and lists.
Arrays need no explanations. In Lua, it is conventional to start indices from 1, but this is only a convention. Arrays can be indexed by 0, negative numbers, or any other value (but nil ). Records are also trivially implemented by the syntactic sugar a.x .
The best way to implement a set is to store its elements as indices of a table. The statement s = {} creates an empty set s . The statement s[x] = 1 inserts the value of x into the set s . The expression s[x] is true if and only if x belongs to s . Finally, the statement s[x] = nil erases x from s .
Bags can be implemented similarly to sets,
but using the value associated to an element as its counter.
So, to insert an element,
the following code is enough:
if s[x] then s[x] = s[x]+1
else s[x] = 1 end
and to remove an element:
if s[x] then s[x] = s[x]-1 end
if s[x] == 0 then s[x] = nil end
Lisp-like lists also have an easy implementation. The ``cons'' of two elements x and y can be created with the code l = {car=x, cdr=y} . The expression l.car extracts the header, while l.cdr extracts the tail. An alternative way is to create the list directly with l={x,y} , and then to extract the header with l[1] and the tail with l[2] .