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

Sequences

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


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

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

A sequence of sequences simplifies into one sequence, that is, sequences are associative. For example


> s := 1,(4,9,16),25;
                              s := 1, 4, 9, 16, 25

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

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


> max(s);

                                       25

> min(s,0,s);

                                       0

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


> 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

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.


> 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


bondaren@thsun1.jinr.ru