[next][previous][up][top][index]
search for:

sequences -- an overview

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)

o1 = (a, b, c, d, e)

o1 : Sequence

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)

o2 = a

o2 : Symbol

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

o3 = singleton a

o3 : Sequence

Most of the functions that apply to lists also work with sequences. We give just one example.

i4 : append(x,f)

o4 = (a, b, c, d, e, f)

o4 : Sequence

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

o5 = (-3, -2, -1, 0, 1, 2, 3)

o5 : Sequence
i6 : y_1 .. y_10

o6 = (y , y , y , y , y , y , y , y , y , y  )
       1   2   3   4   5   6   7   8   9   10

o6 : Sequence
i7 : a .. p

o7 = (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)

o7 : Sequence
i8 : (1,1) .. (2,3)

o8 = ((1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3))

o8 : Sequence
i9 : {a,1} .. {c,2}

o9 = ({a, 1}, {a, 2}, {b, 1}, {b, 2}, {c, 1}, {c, 2})

o9 : Sequence

The operator ZZ : Thing can be used to create sequences by replicating something a certain number of times.

i10 : 12:a

o10 = (a, a, a, a, a, a, a, a, a, a, a, a)

o10 : Sequence

Notice what happens when we try to construct a list using .. or :.

i11 : z = {3 .. 6, 9, 3:12}

o11 = {(3, 4, 5, 6), 9, (12, 12, 12)}

o11 : List

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

o12 = {3, 4, 5, 6, 9, 12, 12, 12}

o12 : List

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

o13 = {a, b, c, d, e}

o13 : List
i14 : toSequence oo

o14 = (a, b, c, d, e)

o14 : Sequence

Other functions for dealing especially with sequences include sequence and deepSplice. The class of all sequences is Sequence.


[next][previous][up][top][index]
search for: