lists - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


Sets and Lists

 

Calling Sequence

Parameters

Description

Thread Safety

Examples

Calling Sequence

{es}

[es]

Parameters

es

-

expression sequence

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

• 

In output, the empty set is typeset as .

• 

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 sub-sequence (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 in-place. 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. For more information on mutable and immutable data structures, refer to the Basic Data Structures chapter of the Maple Programming Guide.

• 

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} (Sx).

• 

Replacing the i-th 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 i-th 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} (Sx).

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 (xL).

• 

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

• 

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

• 

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

• 

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

• 

For more information on these set operations, see Set Operators.

• 

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 data-structures 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 data-structure. 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 object-address 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 1-5 listed above but alter whether each sub-ordering 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.

• 

For more information on thread safety, see index/threadsafe.

Examples

Sets

Repeated elements in a set are ignored.

{x,y,y};

x,y

(1)

{y,x,y};

x,y

(2)

 

Sets of single letter variables are always sorted in alphabetical order.

{B,b,c,a,A};

A,B,a,b,c

(3)

 

Sets of small integers are always in numerical order.

S:={5,4,3,2,1};

S1,2,3,4,5

(4)

Sunion3,6

1,2,3,4,5,6

(5)

 

Set operators do not change the set.  They create a new set.

S

1,2,3,4,5

(6)

 

  

Set operators do not work on lists.

1,2,3intersect2,3,4

2,3

(7)

1,2,3intersect2,3,4

Error, invalid input: intersect received [1, 2, 3], which is not valid for its 1st argument

 

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

evalb1in1,2,3

true

(8)

evalb4in1,2,3

false

(9)

S1,2

S1,2

(10)

T1,2,3

T1,2,3

(11)

Determine if S is a subset of T:

SsubsetT

true

(12)

Difference between S and T is given by:

TminusS

3

(13)

Lists

Elements can be repeated inside a list.

x,y,y

x,y,y

(14)

y,x,y

y,x,y

(15)

Create a list using the seq command.

Lseqxi,i=1..4

Lx1,x2,x3,x4

(16)

Return the number of elements as follows:

numelemsL

4

(17)

Extract specific elements from a list:

L2

x2

(18)

L1..2

x1,x2

(19)

L2..1

x2,x3,x4

(20)

Extract the contents of a list as a sequence:

opL

x1,x2,x3,x4

(21)

L

x1,x2,x3,x4

(22)

opL2..3

x2,x3

(23)

op2..3,L

x2,x3

(24)

Append a new element to a list:

LopL,x5

Lx1,x2,x3,x4,x5

(25)

Delete an element from a list:

subsop2=NULL,L

x1,x3,x4,x5

(26)

Test for list membership

memberx1,L

true

(27)

Nested lists:

L1,2,3,4,5,6,7,8,9

L1,2,3,4,5,6,7,8,9

(28)

Elements from a nested list can be extracted using one of the following methods:

L3,2,1

5

(29)

L321

5

(30)

See Also

Array

convert

in

intersect

list Maple Debugger command

ListTools

maple

member

minus

MultiSet

nops

op

selection

seq

subset

type/list

type/set

union