Lists and Sets

A list in Maple looks very much like a sequence. A list is a set of Maple expressions separated by commas and enclosed inside square brackets:

> mylist := [1, 5, x^2, z];

[Maple Math]

In lists (just as in sequences) the order of the elements matters. For example, [1, 2, 3] and [2, 1, 3] are different lists, even though they contain the same elements. Order matters.

As with sequences, you can refer to any element of a list by specifying its position, e.g.,

> mylist[3];

[Maple Math]


Sets are similar to lists with two exceptions: In sets

1. Elements are not repeated, i.e., no duplicate elements.
2. The order of the elements of the set is not important, and is not preserved by Maple.

Sets are constructed like lists, but enclosed in curly braces, {}. Here's an example:

> myset := {1, 2, x, z, 2};

[Maple Math]

Maple DOESN'T NECESSARILY PRESERVE THE ORDER OF THE ELEMENTS in sets. It stores the set in no particular order, at least no order we can count on. Also, the duplicate element "2" shows up only once in the set myset . Like lists and sequences, you can refer to the elements of a set, but with sets you can't expect them to be in any particular order.

> myset[2];

[Maple Math]

Finally, you can put sequences, lists, and sets together to form new objects. For example, given two sequences

> seq1 := 1, 5, 8, 2;
seq2 := x, a, b, 2;

[Maple Math]

[Maple Math]

we can form a new sequence by concatenating them (i.e., putting one after the other):

> newseq := seq1, seq2;

[Maple Math]

or make them into a list, or set. Try to figure out what the output of each of the following commands should be, then execute them.

> {seq1, seq2};
[seq1, seq2];

[Maple Math]

[Maple Math]

You can also form lists of lists, lists of sets, sets of lists, etc. An example:

> list1 := [1, 2, 4, 6];
list2 := [2, a, g, h];

[Maple Math]

[Maple Math]

> newlist := [list1, list2];

[Maple Math]

The variable newlist is a list whose elements are themselves lists. You can retrieve the first element of newlist with the usual syntax:

> newlist[1];

[Maple Math]

You can pick out the third element of this list (itself the first element of newlist ) with the command

> newlist[1][3];

[Maple Math]

which says to retrieve the third element of the first element of newlist .

Lists of lists can be used to plot data points: Suppose that the velocity of some object is measured at times t = 0, 1, 2, 3, and 4 seconds, and the velocity at these times is 0, 9.6, 19.4, 29.4, and 37.0 meters per second, respectively. We'd like to plot the velocity versus time. This can be done by putting the time and velocity data into a list of pairs and then plotting the list, as follows:

> data := [ [0, 0], [1, 9.6], [2, 19.4], [3, 29.4], [4, 37.0] ];

[Maple Math]

The first pair refers to the (time,velocity) data point (0, 0), the next pair to data point (1, 9.6), etc. Now invoke the plot command:

> plot(data);

[Maple Plot]

Try redoing the above plot with the " style=point " option.
(Type in the command below, do you remember how the plot options work?)

>

> plot(data, style=point);

[Maple Plot]