Sets and Lists

Calling Sequence


{es}
[es]


Description


•

A set is an unordered sequence of distinct expressions enclosed in braces {}, representing a set in the mathematical sense.

•

A list is an ordered sequence of expressions enclosed in square brackets []. The ordering of the expressions is the ordering of es.

•

Note that es may be empty so that the empty set is represented by {} and the empty list is represented by [].

•

The number of elements of a set or list S is given by numelems(S).


Accessing the elements


•

The elements of a set or list can be extracted via the selection operation. Thus if S is a set or list then S[i] selects the ith element. Alternatively you may use op(i,S). The elements are numbered from 1 so S[1] extracts the first element. Negative subscripts can also be used. S[1] selects the last element, S[2] the second last, and so on.

•

Multiple elements of a list or set S can be extracted. The selection op(i..j,S) selects the subsequence (S[i],S[i+1],...,S[j]). If S is a set then the selection S[i..j] selects the subset {op(i..j,S)}. If S is a list then the selection S[i..j] selects the sublist [op(i..j,S)]. Negative subscripts can also be used. Thus S[2..2] selects all elements except the first and last.

•

Lists and sets can be nested, in which case selection can be done in one of two ways: S[i][j]...[n] or S[i,j,...,n].

•

To extract the contents of a list or set L as a sequence, use the op(L) function or the empty selection operator L[].



Modifying the elements


Note: The following modifications to a list or set cause a new list or set to be created rather than modifying the original inplace. To save space and time, when the number of data or the number of modifications to them are large, use of an Array may be preferable.
•

Appending an element x to a list L is done by [op(L), x]. Inserting an element x to a set S is done using the union operator S union {x} ().

•

Replacing the ith element of a list L by x can be done by subsop(i=x, L).

•

The operation L[i] := x also works for reasons of convenience, but only for short lists. It is not recommended to use this operation unless you know that the list is small.

•

Deleting the ith element of a list L is subsop(i=NULL, L). Deleting an element x from a set S is done using the minus operator S minus {x} ().



Set and list operations


•

To test if an element x is in a list or set L, use either member(x,L) or x in L ().

•

To test if a set S is a subset of a set T, use the subset operator S subset T ().

•

To find the intersection of two sets S and T, use S intersect T ().

•

To find the union of two sets S and T, use S union T ().

•

To find the difference of two sets S and T, use S minus T ().

•

Further list operations can be found in the package ListTools.



Set ordering


Sets have a deterministic ordering that, for most objects, is not based on runtime properties. This means that when {b,c,a} is input, the order will be fixed to {a,b,c} no matter when you created that set. A notable exception to this rule is when a set contains multiple mutable objects of the same type. For example, two vectors inside a set could appear in either order in different sessions.
In general, object ordering is first determined by quickly identifiable features like its class or size. The default comparison order checks properties in the following order:
1.

object id: this causes the same kind of datastructures to be grouped together. For example integers will come before exact rationals, floats, and complex numbers.

2.

object length: this causes objects of the same size to be grouped together. The "length" is a property of the underlying datastructure. As a result, {2^200, 2^100} is not in increasing numerical order and {bb, aaaaaaaa} is not in alphabetical order; in both cases the shorter data structure occurs first.

3.

lexicographical or numerical order: this orders numbers in increasing numerical order and strings in lexicographic order.

4.

recurse on components: this causes compound objects to be ordered according to the above properties of their child components. For example {f(z),g(a)} will pass the above properties as both f(z) and g(a) are function objects of the same length. So the next level of ordering will compare the names of each function, f and g, which will fall into rule 3. Note that the ordering of expressions like {x+1,1+y} may still be session dependent if the unique representation of x+1 is in the order of 1+x, or if the second expression is y+1 instead of 1+y.

5.

address: in some cases the object will be identical in every way, such as the local name x and the global name x. When this happens the machine address of the given object will be used. This at least ensures the set order will be consistent within the running session.

Note: This order could be changed in future versions of Maple, and/or objects could be represented in a different way in future versions. You should not assume that sets will be maintained in any particular order.
The objectaddress based set ordering used in Maple 11 and earlier versions can be obtained by using the commandline option setsort=0. Similarly, the new set order can be enabled with setsort=1. Variations of setsort=0..8 are available if you want to purposefully introduce ordering differences into your code to test its robustness. Settings 2 through 8 apply the same rules 15 listed above but alter whether each subordering is increasing or decreasing. For more information on commandline options, see the maple help page.



Thread Safety


•

The [] and {} constructors are thread safe as of Maple 15.



Examples


>


 (1) 
>


 (2) 
>


 (3) 
>


 (4) 
>


 (5) 
>


 (6) 
Extract list elements
>


 (7) 
>


 (8) 
>


 (9) 
Extract the contents of a list as a sequence
>


 (10) 
>


 (11) 
>


 (12) 
>


 (13) 
Append a new element to a list
>


 (14) 
Delete an element from a list
>


 (15) 
Test for list membership
>


 (16) 
Nested lists
>


 (17) 
>


 (18) 
>


 (19) 
Sets of single letter variables are always sorted in alphabetical order.
>


 (20) 
Sets of small integers are always in numerical order.
>


 (21) 
>


 (22) 
Set operators do not change the set. They create a new set.
>


 (23) 
Set operators do not work on lists.
>


 (24) 
>


The in operator () tests for set or list membership.
>


 (25) 
>


 (26) 
>


 (27) 
>


 (28) 
>


 (29) 
>


 (30) 


See Also


Array, convert, in, intersect, list Maple Debugger command, ListTools, maple, member, minus, nops, op, selection, seq, subset, type/list, type/set, union

