next | previous | forward | backward | up | top | index | toc | home
Macaulay2 > The Macaulay2 language > lists and sequences > ranges and repetitions

ranges and repetitions

In this section we discuss the use of ranges and repetitions.

ranges

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.
i1 : 1 .. 5, y_1 .. y_5, a .. e

o1 = ((1, 2, 3, 4, 5), (y , y , y , y , y ), (a, b, c, d, e))
                         1   2   3   4   5

o1 : Sequence

repetitions

The operator ZZ : Thing is used to create sequences by replicating something a certain number of times.
i2 : 12:a

o2 = (a, a, a, a, a, a, a, a, a, a, a, a)

o2 : Sequence
Replicating something once results in a sequence of length 1, which cannot be entered by simply typing parentheses.
i3 : 1:a

o3 = 1 : (a)

o3 : Sequence
i4 : (a)

o4 = a

o4 : Symbol

ranges and repetitions in lists

Notice what happens when we try to construct a list using .. or :.
i5 : z = {3 .. 6, 9, 3:12}

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

o5 : 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 inward.
i6 : splice z

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

o6 : List
The difference between splice and flatten is, essentially, that flatten removes braces one level inward.
i7 : flatten {a,{b,c}}

o7 = {a, b, c}

o7 : List
i8 : splice {a,(b,c)}

o8 = {a, b, c}

o8 : List
The function toList converts sequences to lists.
i9 : 1..6

o9 = (1, 2, 3, 4, 5, 6)

o9 : Sequence
i10 : toList(1..6)

o10 = {1, 2, 3, 4, 5, 6}

o10 : List
Many operators and functions will splice lists presented to them. For example, when creating a polynomial ring, the array of variables and the list of degrees are spliced for you.
i11 : QQ[a..c,x_1..x_4, Degrees => { 3:1, 4:2 }]

o11 = QQ [a, b, c, x , x , x , x , Degrees => {{1}, {1}, {1}, {2}, {2}, {2},
                    1   2   3   4
      -----------------------------------------------------------------------
      {2}}]

o11 : PolynomialRing
i12 : degrees oo

o12 = {{1}, {1}, {1}, {2}, {2}, {2}, {2}}

o12 : List