Up | Tail |

Several operators in the system return their results as lists, and a user can create new lists using braces and commas. Alternatively, one can use the operator LIST to construct a list. An important class of operations on lists are MAP and SELECT operations. For details, please refer to the chapters on MAP, SELECT and the FOR command. See also the documentation on the ASSIST (chapter 16.5) package.

To facilitate the use of lists, a number of operators are also available for manipulating
them. PART(⟨list⟩,n) for example will return the n^{th} element of a list. LENGTH
will return the length of a list. Several operators are also defined uniquely for lists. For
those familiar with them, these operators in fact mirror the operations defined for Lisp
lists. These operators are as follows:

The operator LIST is an alternative to the usage of curly brackets. LIST accepts an arbitrary number of arguments and returns a list of its arguments. This operator is useful in cases where operators have to be passed as arguments. E.g.,

list(a,list(list(b,c),d),e); -> {{a},{{b,c},d},e}

This operator returns the first member of a list. An error occurs if the argument is not a list, or the list is empty.

SECOND returns the second member of a list. An error occurs if the argument is not a list or has no second element.

This operator returns the third member of a list. An error occurs if the argument is not a list or has no third element.

REST returns its argument with the first element removed. An error occurs if the argument is not a list, or is empty.

This operator adds (“conses”) an expression to the front of a list. For example:

a . {b,c} -> {a,b,c}.

This operator appends its first argument to its second to form a new list. Examples:

append({a,b},{c,d}) -> {a,b,c,d}

append({{a,b}},{c,d}) -> {{a,b},c,d}.

append({{a,b}},{c,d}) -> {{a,b},c,d}.

The operator REVERSE returns its argument with the elements in the reverse order. It only applies to the top level list, not any lower level lists that may occur. Examples are:

reverse({a,b,c}) -> {c,b,a}

reverse({{a,b,c},d}) -> {d,{a,b,c}}.

reverse({{a,b,c},d}) -> {d,{a,b,c}}.

If an operator other than those specifically defined for lists is given a single argument that is a list, then the result of this operation will be a list in which that operator is applied to each element of the list. For example, the result of evaluating log{a,b,c} is the expression {LOG(A),LOG(B),LOG(C)}.

There are two ways to inhibit this operator distribution. Firstly, the switch LISTARGS, if on, will globally inhibit such distribution. Secondly, one can inhibit this distribution for a specific operator by the declaration LISTARGP. For example, with the declaration listargp log, log{a,b,c} would evaluate to LOG({A,B,C}).

If an operator has more than one argument, no such distribution occurs.

Some of the natural list operations such as member or delete are available only after loading the package ASSIST (chapter 16.5).

Please note that a non-list as second argument to CONS (a "dotted pair" in LISP terms) is not allowed and causes an "invalid as list" error.

a := 17 . 4;

***** 17 4 invalid as list

***** 17 4 invalid as list

Also, the initialization of a scalar variable is not the empty list – one has to set list type variables explicitly, as in the following example:

load_package assist;

procedure lotto (n,m);

begin scalar list_1_n, luckies, hit;

list_1_n := {};

luckies := {};

for k:=1:n do list_1_n := k . list_1_n;

for k:=1:m do

<< hit := part(list_1_n,random(n-k+1) + 1);

list_1_n := delete(hit,list_1_n);

luckies := hit . luckies >>;

return luckies;

end;

% In Germany, try lotto (49,6);

procedure lotto (n,m);

begin scalar list_1_n, luckies, hit;

list_1_n := {};

luckies := {};

for k:=1:n do list_1_n := k . list_1_n;

for k:=1:m do

<< hit := part(list_1_n,random(n-k+1) + 1);

list_1_n := delete(hit,list_1_n);

luckies := hit . luckies >>;

return luckies;

end;

% In Germany, try lotto (49,6);

Another example: Find all coefficients of a multivariate polynomial with respect to a list of variables:

procedure allcoeffs(q,lis);

% q : polynomial, lis: list of vars

allcoeffs1 (list q,lis);

procedure allcoeffs1(q,lis);

if lis={} then q else

allcoeffs1(foreach qq in q join coeff(qq,first lis),

rest lis);

% q : polynomial, lis: list of vars

allcoeffs1 (list q,lis);

procedure allcoeffs1(q,lis);

if lis={} then q else

allcoeffs1(foreach qq in q join coeff(qq,first lis),

rest lis);

Up | Front |