previous chapterthe pico pagenext chapter


Table indexation

Every variable bound to a table may be indexed:

name[expression]

The name identifies the variable while the expression should have a number value that is compatible with the size of the table that the variable is bound to. The result is the value contained within the slot indexed by the value of the expression (it should range from one to the size of the table).

The following transcript illustrates the use of indexation:

First of all a table p is created containing the first 20 positive integer powers of 2. Note that the value of p is printed out in the prescribed format. Next a function log2 is defined taking two parameters x and m. This function traverses p in order to find the first slot p[m] that contains a value smaller than x. Note that log2 keeps applying itself to the same value of x and the next integer value of m in order to perform this traversal. The function if is one of the predefined functions that will be explained in the coming paragraphs; intuitively it uses the validity of x<p[m] to select either the value of m or the value of log2(x,m+1).

The transcript demonstrates that log2 correctly computes the (rounded) logarithm of base 2 of the numbers 1000 and 5000.


previous chapterthe pico pagenext chapter