A list is a heterogeneous associative array. Simply, a list is an array whose elements can be from different classes. Thus a list can contain numeric, string, function, and other list objects. Lists have many uses, only some of which we will demonstrate herein.
To create a list-object use the `<<
' and `>>'
'
operators. The list will be created, and the objects inside the
`<< >>
' will be copied into the new list. If the objects are
not renamed during the list-creation, they will be given numerical
index values.
> a = rand(3,4); b = sqrt (a); c = 2*a + b; > ll = << a ; b ; c >> 1 2 3 > ll2 = << A = a; b = b ; x = c >> A b x > ll2.A == ll.[1] 1 1 1 1 1 1 1 1 1 1 1 1
Lists are not indexed in what is perceived as the ``traditional manner''. Instead the list index is converted to a string, and the string value is used to look-up the referenced object15. There are two methods for referencing the elements of a list, the first, a shorthand notation looks like:
list_name . element_name
In this case, the list_name and element_name must follow the same rules as ordinary variable names. The second method for indexing a list is:
list_name .[
numeric_or_string_expression]
The second method allows string and numeric variables to be evaluated before doing the conversion to string type16.
As you have seen in an earlier example lists can be used within functions when it is necessary to return several values. Because of the lists flexible nature a function can return matrices of differing sizes and types, as well as strings, other lists, or user-functions.