List Handling Features

The macro preprocessor provides features for declaring and processing lists. A list variable is declared as in the following example.

'list 'listvar

The 'gbllist statement can be used to declare global list variables. The built-in list variables 'ilist, 'olist and 'args have already been discussed. A list constant is simply a list of elements separated by commas and enclosed in parentheses. Thus to assign a three-element list to the variable declared above, use the following statement.

'assign 'listvar,(a,bbb,qed)

The first element of a list can be accessed using the 'first function, so the function call 'first((a,b,c)) returns the string "a". The 'rest function can be used to remove the first element of the list and return the rest. Thus 'rest((a,b,c)) returns the list (a,b), and 'rest((a,b)) returns the string "b". If the list contains only one element, 'rest will return the null string. The 'select function can be used to select a particular element of a list. This function requires two arguments, a list and a number indicating which element to select. Elements are numbered starting with zero, so 'select((a,b,c),0) returns "a" while 'select((a,b,c),2) returns "c". If the second argument is greater or equal to the number of elements in the list, the null string is returned.

Lists may be nested, so the following is a valid example of a list constant.

(a,b,(c,(d,dd),e),f)

Because of list nesting, the functions 'first, 'rest and 'select may return either a list or a string. To determine whether the object returned by one of these functions is a string or a list, use the 'atom function. The function 'atom('first((a,b,c))) will return true, while 'atom('rest((a,b,c))) will return false. As for other conditionals, if these true and false values are used in arithmetic expressions they are treated as one and zero respectively. You can test for the null string (or null list, which is the same thing) by using the function 'nil. The function 'nil returns true if its argument is the null string (or null list) and false otherwise.

Lists can be constructed one element at a time by using the functions 'cons and 'consr. These functions take two arguments, the second of which must be a list, and the first of which must be an element to be added to the list. If the second argument is not a list, it is coerced to a single-element list. The first argument may be an integer, a string, or another list. The function 'cons adds the element to the beginning of the list, while the function 'consr adds the element to the end of the list.

When a list is used as a string it is coerced to a string. If it is a single-element list, the result is the value of the single element. If it is a multi-element list, the result is a string that begins with "(" and ends with ")" and contains the values of the elements separated by commas. Thus the list (a,b,c) is coerced to the string "(a,b,c)". When a list is used as an integer, it is first coerced to a string then the string is coerced to an integer. When an integer or a string is used as a list, it is coerced to a single-element list.