Documentation Center

  • Trial Software
  • Product Updates

DOM_LIST

Lists of Objects

Description

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.

Examples

Example 1

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 x5 and the list of elements from x3 through x6 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 x5:

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

Function Calls

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)].

Operations

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.

Operands

The operands of a list are its elements.

Element Creation

The most direct way of creating a list is to place a sequence of MuPAD objects (separated by commas) between rectangular brackets, as in [1, 2, 3].

See Also

MuPAD Domains

Was this topic helpful?