Expression Sequences
|
Description
|
|
•
|
Expression sequences, (or simply sequences), are created using the comma operator (, ). For example s := 1, 2, 3 assigns s the sequence 1, 2, 3. In Maple, sequences form the basis of many data types. In particular, they appear in function calls, lists, sets, and subscripts.
|
|
applies the function to the sequence 1, 2, 3
|
|
creates the list containing the elements 1, 2, 3
|
|
creates the set containing the elements 1, 2, 3
|
|
is the 1, 2, 3 subscript
|
•
|
When sequences are concatenated with a comma, the result is a single, unnested sequence. Thus t := s, s assigns t the sequence 1, 2, 3, 1, 2, 3. This also means that an expression sequence cannot be passed into a function as a single argument. f(0,s,4) is equivalent to f(0,1,2,3,4). One can create a list or set from the expression sequence to pass it into a function.
|
•
|
Two key tools for constructing sequences are the seq function and the repetition operator $ . For example, the call seq(f(i), i=1..3) will generate the sequence f(1), f(2), f(3). The call x$3 will generate the sequence x, x, x.
|
•
|
Sequences can also be constructed using the op function. The op function when applied to any Maple expression (except a sequence itself) returns a sequence of the operands of that expression. For example, op([x, y, z]) and op(x+y+z) both return the sequence x, y, z.
|
•
|
The op and nops functions cannot be applied to a sequence because the elements of the sequence are taken to be individual arguments to the function. However, the ith operand of a sequence s may be accessed using the selection operation s[i]. The length of a sequence may be determined by doing nops([s]), that is, by determining the length of the list containing the expression sequence.
|
•
|
There is no expression sequence type in Maple. However, the whattype function returns exprseq for objects that are expression sequences.
|
|
|
Examples
|
|
>
|
p := proc() nargs end proc:
|
>
|
|
| (1) |
>
|
|
| (2) |
>
|
|
| (3) |
>
|
|
| (4) |
Passing expression sequences into functions can lead to unexpected results.
>
|
|
| (5) |
In this case the expression sequence, , is flattened into the expression sequence of arguments to map. Therefore map sees 4 arguments, the function to be mapped, the argument to map over and two extra arguments that are passed to the mapped function.
Making a list from the expression sequence will work as expected.
>
|
|
| (6) |
|
|
See Also
|
|
convert, dollar, list, map, nops, op, selection, seq, set, type, whattype
|
|