Next: Lists and Sets Up: Data Structures Previous: Data Structures

Sequences

A sequence is a sequence of expressions separated by commas. For example

=-1.00.5plus##1`##1=12=^^M=12 > s := 1,4,9,16,25; s := 1, 4, 9, 16, 25

> t := sin,cos,tan; t := sin, cos, tan

plusplus -100 plus A sequence of sequences simplifies into one sequence, that is, sequences are associative. For example

=-1.00.5plus##1`##1=12=^^M=12 > s := 1,(4,9,16),25; s := 1, 4, 9, 16, 25

> s,s; 1, 4, 9, 16, 25, 1, 4, 9, 16, 25

plusplus -100 plus The special symbol NULL is used for the empty sequence. Sequences are used for many purposes. The next section shows how lists and sets are constructed from sequences. Here we note that function calls are really constructed from sequences. For example, the min and max functions in Maple take an arbitrary number of values as arguments, i.e. a sequence of arguments

=-1.00.5plus##1`##1=12=^^M=12 > max(s);

25

> min(s,0,s);

0

plusplus -100 plus

The seq function is extremely useful for creating sequences. It comes in two flavours, corresponding to the two kinds of for loops. The first one is

seq( , ).

The way seq works is just as if you had programmed the following loop.


s := NULL;
for i from m by 1 to n do s := s, f(i) od;

For example

=-1.00.5plus##1`##1=12=^^M=12 > seq( i^2, i=1..5 ); 1, 4, 9, 16, 25

> s := NULL; for i from 1 to 5 do s := s, i^2 od;

s :=

s := 1

s := 1, 4

s := 1, 4, 9

s := 1, 4, 9, 16

s := 1, 4, 9, 16, 25

plusplus -100 plus Notice that seq is more efficient than the for loop because it does not create the intermediate sequences. The other flavour of seq is

seq( , ).

This is equivalent to


seq( f(op(i,a)), i=1..nops(a) )

Here are a couple of interesting examples. The coeff function computes the coefficient of the term of degree of a polynomial in . The D function in Maple is the derivative operator.

=-1.00.5plus##1`##1=12=^^M=12 > a := 3*x^3+y*x-11; 3 a := 3 x + y x - 11

> seq( coeff(a,x,i), i=0..degree(a,x) );

-11, y, 0, 3

> seq( D(f), f=[sin,cos,tan,exp,ln] );

2 cos, - sin, 1 + tan , exp, a -> 1/a

plusplus -100 plus



Next: Lists and Sets Up: Data Structures Previous: Data Structures


Klaus Steinberger
Mi Apr 13 12:51:51 MDT 1994