Listability of functions

Almost all of the functions in the GCL are ``listable''. This means that if there is a version of the function which takes a type T as the data type for a parameter, then it can be called with LIST(T) as well. This results in the function generating a list of the corresponding length and data type as its output. For example, consider a function

Foo[x->T,y->S] =: R
where T, S, and R represent arbitrary data types. Then if \verb+a+ is a LIST(T), and \verb+b+ is of type S, then \verb+c := Foo[a,b]+ generates \verb+c+ of type LIST(R), where the length of \verb+c+ is equal to the length of \verb+a+, and where \verb+c[[i]] = Foo[a[[i]],b]+.

If Foo[] is called with both \verb+a+ and \verb+b+ being lists of the correct data type, then the GCL checks whether the length of \verb+a+ and \verb+b+ is the same. If not, it generates a dimensionality error. If they are of the same dimension, then \verb+c := Foo[a,b]+ generates \verb+c+ of type LIST(R), where the length of \verb+c+ is equal to the length of \verb+a+, and where \verb+c[[i]] = Foo[a[[i]],b[[i]]]+.

Note that the above rules implicitly define versions of Foo of the following forms:

Foo[x->LIST(T),y->S] =: LIST(R)
Foo[x->T,y->LIST(S)] =: LIST(R)
Foo[x->LIST(T),y->LIST(S)] =: LIST(R)

The same rules apply recursively using these function prototypes as well. So if \verb+a+ is of type LIST(LIST(T)), and \verb+b+ is of type \verb+S+, the command \verb+c := Foo[a, b]+ results in a listable call to \verb+Foo[x->LIST(T),y->S]+. Thus, \verb+c+ is of type LIST(LIST(R)), where \verb+c[[i]] = + \verb+Foo[a[[i]],b]+. Hence, \verb+c[[i]][[j]] = + \verb+Foo[a[[i]][[j]],b]+.

Note that many vector and matrix math operations follow directly as listable calls to the basic arithmetic functions. In this example, listability of Times is used to provide scalar multiplication of a scalar and a matrix, and listability of Plus is used to add two conformable matrices:

GCL1:= a:={{1,2},{3,4}};
GCL2:= << 2*a
{{2,4},{6,8}}
GCL3:= << a+Transpose[a]
{{2,5},{5,8}}

Here listability of Power is used to create a list of the first ten perfect squares:

GCL4:= << List[1,10,1]^2
{ 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 }

While most functions in the GCL are listable, there are some exceptions. Built-in functions that are not listable are noted as such in the Function Reference section of this document.

Listability of functions is a powerful tool of the GCL. Many GCL programs using flow control statements can be written using listable function calls instead. It is encouraged to use listable functions as much as possible because it typically leads to more concise, easier to read GCL programs, and in addition the programs typically run faster than they would if flow control satements were used instead. This is because listable calls to functions are executed in compiled code, while flow control statements are executed in interpreted code.