Documentation |
Lists of Objects
Lists (of domain type DOM_LIST) are ordered collections of an arbitrary number of arbitrary MuPAD^{®} objects, except for sequences and the null object.
In MuPAD, the mathematical construct of an n-tuple is implemented as the data type DOM_LIST. Lists consist of an arbitrary (finite) number of arbitrary objects, with the exception of expression sequences, which are split into their operands when placed into a list.
Unlike sets, lists can contain multiple copies of the same element. The order of elements in a list is preserved.
Lists can be empty.
To create a list for our first example, we use the operator $:
L := [x_.i $ i=1..10]
This list contains 10 elements:
nops(L)
The fifth element of the list is x_{5} and the list of elements from x_{3} through x_{6} can also be accessed very easily:
L[5], L[3..6]
To change an element of the list, we use the indexed form on the left hand side of an assignment:
L[5] := 5
L
Note that this assignment only changes L, not x_{5}:
x_5
Likewise, we can change a sublist by assigning another list to it. This may change the length of the list:
L[3..6] := [1, 2]
nops(L), L
Using a list as a function symbol creates the list obtained by using each list element as a function symbol for the operands used, i.e., [f, g](x, y) results in [f(x, y), g(x, y)].
Assuming that L is a list, the number of elements in L can be determined by calling nops(L).
Individual elements of the list are accessed in the form L[1], L[2] etc. when counting from the beginning or L[-1], L[-2] etc. when counting from the end. Trying to access an element "outside" the list or L[0] raises an error.
Continuous sub-lists can be extracted by using a range in an indexed access: L[2..4] returns the list [L[2], L[3], L[4]]; L[2..-2] returns the list L without its first and last element.
Both forms of indexed access can also be used as the left hand side of an assignment, cf. Example 1.
Lists can be concatenated with the dot operator, as in L1 . L2 or its functional form, _concat.
The function contains finds the first occurrence of a given MuPAD object in a list. select and split can be used to extract those elements from a list fulfilling an arbitrary predicate.
Lists can be sorted with sort or prog::sort.
The function map applies a function to all elements of a list, returning the list of results. To combine two lists element-wise with some function, use zip.
Assigning a list to a list of identifiers is possible and results in a simultaneous assignment, cf. ?_assign.