A sequence is like a list, except that parentheses are used instead of braces to create them and to print them. Sequences are implemented in a more efficient way than lists, since a sequence is created every time a function is called with more than one argument. Another difference is that new types of list can be created by the user, but not new types of sequence.
i1 : x = (a,b,c,d,e) |
It is a bit harder to create a sequence of length 1, since no comma would be involved, and parentheses are also used for simple grouping of algebraic expressions.
i2 : (a) |
We provide the function singleton, which can be used to create a sequence of length 1. Its name appears when a sequence of length 1 is displayed.
i3 : singleton a |
Most of the functions that apply to lists also work with sequences. We give just one example.
i4 : append(x,f) |
The operator .. can be used to create sequences of numbers, sequences of subscripted variables, or sequences of those particular symbols that are known to vars, and so on.
i5 : -3 .. 3 |
i6 : y_1 .. y_10 |
i7 : a .. p |
i8 : (1,1) .. (2,3) |
i9 : {a,1} .. {c,2} |
The operator ZZ : Thing can be used to create sequences by replicating something a certain number of times.
i10 : 12:a |
Notice what happens when we try to construct a list using .. or :.
i11 : z = {3 .. 6, 9, 3:12} |
The result above is a list of length 3 some of whose elements are sequences. This may be a problem if the user intended to produce the list {3, 4, 5, 6, 9, 12, 12, 12}. The function splice can be used to flatten out one level of nesting - think of it as removing those pairs of parentheses that are one level in.
i12 : splice z |
The difference between splice and flatten is that flatten removes pairs of braces.
The functions toList and toSequence are provided for converting between lists to sequences.
i13 : toList x |
i14 : toSequence oo |
Other functions for dealing especially with sequences include sequence and deepSplice. The class of all sequences is Sequence.