Next: Function Libraries, Previous: Properties, Up: Marsyas Expression Syntax
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.
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}.
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}.
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].