Next: , Previous: Properties, Up: Marsyas Expression Syntax

##### 9.6.4.9 Sequences (Lists)

Sequence types are those data types that have a sequence of elements. Falling into this category are lists and strings not natural or real numbers.

Lists are denoted using the square brackets as in [ 1, 2, 3 ]. Lists can contain other lists as in [ [1,2], [3,4] ]. However, all elements of a list must be of the same type. This [ 1, '2'] and this [ 1, [ 2, 3 ] ] are not valid lists.

Iterators

There are four different iterators for working with sequence types: map, iter, for, rfor. There are subtle differences.

• map {map x in xs: <expr>}
The map iterator works in a similar fashion to the functional map. Map iterates from left to right across each element and replaces each element with a new one of the same or different type. Map returns a new list without modifying the original. An example: xs<<[1,2,3], b<<{map x in xs: x+1}.
• iter {iter x in xs: <expr>}

The iter iterator works like map except that it modifies the original list in place. Iter returns unit. An example: xs<<[1,2,3], {iter x in xs: x+1}.

• for {for x in xs: <expr>}

The for iterator iterates across the list but does not modify the original list and does not create a new list. for returns unit. An example: xs<<[1,2,3], sum<<0, {for x in xs: sum <<+ x}.

• rfor {rfor x in xs: <expr>}

The rfor iterator works in the same way as for but from right to left. An example: xs<<[1,2,3], b<<{rfor x in xs: Stream.opn << x}.

Element Access

Elements of sequences may be accessed using the standard array notation as in a<<[1,2,3], one<<a[0], three<<a[2]. Note that the first element is position 0. Don't let me catch you using an index that's too big.

Slices

Slices are portions of sequences. A range takes two positions in the list - start position (included) and end position (not included) separated by a colon [start:end]. If the start position is left out then the first element is assumed. If the last element is left out then the end is assumed. The start and end positions may be adjusted to avoid errors, ie negative start becomes the beginning of the list.

We can take the head and tail of a list using slices: a<<[1,2,3], hd<<a[0], tl<<a[1:].

Concatenation, Joining

Sequences may be joined using the concatentation operator otherwise known as +. [1,2,3]+[4] joins the two lists into [1,2,3,4].