# Advanced List Operations

There are several operators that take a list as argument and return another list derived from it. This section deals with such operators. These operators form very powerful tools for performing a high-level computation. For examples of how to use and apply these operators in a realistic context, we strongly recommend to read the example section for CindyScript.

## Pairs and Triples

### Building pairs `pairs(<list>)`:

Description: This operator produces a list that contains all two-element sublists of a list. These are all pairs of elements of `<list>`. This operator is particularly useful for creating all segments determined a set of points.
Example:

expression: | evaluates to:
`pairs([1, 2, 3, 4])` | `[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]`

### Creating a chain `consecutive(<list>)`:

Description: This operator produces a list that contains all pairs of elements of consecutive elements of the argument `<list>`.
Example:

expression: | evaluates to:
`consecutive([1, 2, 3, 4, 5])` | `[[1, 2], [2, 3], [3, 4], [4, 5]]`

### Creating a cycle `cycle(<list>)`:

Description: This operator produces a list that contains all pairs of consecutive elements of the argument `<list>`. Furthermore, the pair consisting of the last and the first elements is added.
Example:

expression: | evaluates to:
`cycle([1, 2, 3, 4, 5])` | `[[1, 2], [2, 3], [3, 4], [4, 5], [5, 1]]`

### Building triples `triples(<list>)`:

Description: This operator produces a list that contains all three-element sublists of a list. These are all the triples of elements of `<list>`.
Example:

expression: | evaluates to:
`triples([1, 2, 3, 4])` | `[[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3,4 ]]`

## Orders

The following operators change the order of the elements within a list.

### Reversing a list `reverse(<list>)`:

Description: This operator reverses the order of the elements in `<list>`.
Example:

expression: | evaluates to:
`reverse([1, 2, 3, 4])` | `[4, 3, 2, 1]`

### Sorting a list `sort(<list>)`:

Description: Within CindyScript, all elements possess a natural complete order that makes it possible to compare any two elements. Two elements are equal, or one of them is greater than the other. Within the real numbers, the order is the usual numeric ordering. Within strings, the order is the lexicographic order. Complex numbers are ordered by their real parts first. If two complex numbers have the same real part, then they are compared with respect to their imaginary parts. Two lists are compared by the first entry in which they differ. Furthermore, by convention we have the ordering
booleans < numbers < strings < lists
Examples:

expression: | evaluates to:
`sort([4.5, 1.3, 6.7, 0.2])` | `[0.2, 1.3, 4.5, 6.7]`
`sort(["one","two","three","four","five"])` | `["five","four","one","three","two"]`

### Sorting a list `sort(<list>, <expr>)`:

Description: This operator takes each element of the list and evaluates a function expressed by `<expr>` applied to it. All elements of the list are sorted with respect to the result of these evaluations.
Examples:

expression: | evaluates to:
`sort([-4.5, 1.3, -6.7, 0.2], abs(#))` | `[0.2, 1.3, -4.5, -6.7]`
`sort(["one","two","three","four","five"],length(#))` | `["one","two","four","five","three"]`

### Sets from lists `set(<list>)`:

Description: This operator sorts all elements of a list and removes occurrences of identical elements. Thus a unique representation of the list is computed if the list is considered as a set of objects. Together with the operators `concat`, `remove`, and `common`, this can be used as an implementation of set functionality.
Examples:

expression: | evaluates to:
`set([3, 5, 2, 4, 3, 5, 7])` | `[2, 3, 4, 5, 7]`
`set([3, 5, 2]++[4, 5, 2])` | `[2, 3, 4, 5]`
`set([3, 5, 2]~~[4, 5, 2])` | `[2, 5]`

Page last modified on Wednesday 28 of March, 2007 [08:29:33 UTC].
The original document is available at http://doc.cinderella.de/tiki-index.php?page=Advanced%20List%20Operations