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; |
| > | whattype(X); |
We can create sequences with the command "seq":
| > | Y := seq(y[i],i=1..10); |
To get access to the elements in a sequence, we use the square brackets:
| > | Y[6]; |
| > | op(6,[Y]); |
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); |
The difference between the sequence Y and Z is that we have the more natural indexing.
| > | Z[6]; |
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]; |
| > | op(Y[6])= op(y[6]); |
| > | op(Z[6]); |
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}; |
The reason for this conversion from a sequence to a set is that we have special operations on the set.
| > | T := S union {Z}; |
| > | sZ := T intersect {Z}; |
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); |
| > | whattype(%); whattype(sZ); |
3. List
We create a list from a sequence by placing square brackets around the sequence:
| > | L := [Y]; |
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)}; |
We can zip two lists:
| > | i := [seq(k,k=1..10)]; |
| > | d := [stats[random,uniform[-1,1]](10)]; |
Suppose we want to create points [i,d[i]]:
| > | zip((x,y) -> [x,y],i,d); |
![]()
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); |
![]()
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.