lec21.mws

L-21 MCS 320 Friday 14 October 2005

> restart;

We distinguish between sequences, sets and lists as the first three composite data structures we will consider in this part of the course.

1. Sequence

> X := x$10;

X := x, x, x, x, x, x, x, x, x, x

> whattype(X);

exprseq

We can create sequences with the command "seq":

> Y := seq(y[i],i=1..10);

Y := y[1], y[2], y[3], y[4], y[5], y[6], y[7], y[8], y[9], y[10]

To get access to the elements in a sequence, we use the square brackets:

> Y[6];

y[6]

> op(6,[Y]);

y[6]

In this alternative selection of an element in a sequence, we converted the sequence Y into the list [Y], because the "op" command only works on lists, not on sequences.

> Z := Y||(1..10);

Z := Y1, Y2, Y3, Y4, Y5, Y6, Y7, Y8, Y9, Y10

The difference between the sequence Y and Z is that we have the more natural indexing.

> Z[6];

Y6

The 6-th element of the sequence Z is Y6 which is not like Y[6] = y[6]: the Y6 is a concatenation of two symbols: the letter Y with the number 6.

> Y[6];

y[6]

> op(Y[6])= op(y[6]);

6 = 6

> op(Z[6]);

Y6

The little y is an indexed variable, with the op command we recover its index.

2. Set

A set is created from a sequence by placing curly braces around it:

> S := {Y};

S := {y[1], y[2], y[3], y[4], y[5], y[6], y[7], y[8], y[9], y[10]}

The reason for this conversion from a sequence to a set is that we have special operations on the set.

> T := S union {Z};

T := {Y1, Y2, Y3, Y4, Y5, Y6, Y7, Y8, Y9, Y10, y[1], y[2], y[3], y[4], y[5], y[6], y[7], y[8], y[9], y[10]}

> sZ := T intersect {Z};

sZ := {Y1, Y2, Y3, Y4, Y5, Y6, Y7, Y8, Y9, Y10}

Any idea how to convert a set into a sequence?

A good reason for doing this conversion is to impose an order on the elements, because Maple may order the elements in a set in any way.

> op(sZ);

Y1, Y2, Y3, Y4, Y5, Y6, Y7, Y8, Y9, Y10

> whattype(%); whattype(sZ);

exprseq

set

3. List

We create a list from a sequence by placing square brackets around the sequence:

> L := [Y];

L := [y[1], y[2], y[3], y[4], y[5], y[6], y[7], y[8], y[9], y[10]]

We can convert to a set by removing the square brackets via the op command again, and then placing curly braces round the result of op:

> S := {op(L)};

S := {y[1], y[2], y[3], y[4], y[5], y[6], y[7], y[8], y[9], y[10]}

We can zip two lists:

> i := [seq(k,k=1..10)];

i := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

> d := [stats[random,uniform[-1,1]](10)];

d := [-.2085622790, -.6137203672, -.9551516591, .600374969, -.1448958862, .685245369, -.1754274284, .992834428, -.2271833852, .389214379]

Suppose we want to create points [i,d[i]]:

> zip((x,y) -> [x,y],i,d);

[[1, -.2085622790], [2, -.6137203672], [3, -.9551516591], [4, .600374969], [5, -.1448958862], [6, .685245369], [7, -.1754274284], [8, .992834428], [9, -.2271833852], [10, .389214379]][[1, -.2085622790], [2, -.6137203672], [3, -.9551516591], [4, .600374969], [5, -.1448958862], [6, .685245369], [7, -.1754274284], [8, .992834428], [9, -.2271833852], [10, .389214379]]

We just created another list of x and y coordinates.  But suppose that we want to create a list of powers of the data in d.  The i-th element is d[i]^i:

> zip((x,y) -> y^x,i,d);

[-.2085622790, .3766526891, -.8713988916, .1299242770, -0.6386755300e-4, .1035324891, -0.5113078725e-5, .9440926758, -0.1612089314e-5, 0.7977903212e-4][-.2085622790, .3766526891, -.8713988916, .1299242770, -0.6386755300e-4, .1035324891, -0.5113078725e-5, .9440926758, -0.1612089314e-5, 0.7977903212e-4]

The reason for converting a sequence or a set into a list is because of the anonymous functions which may be applied by map, select, or zip.