Advanced List Operations - Cinderella Documentation
Description: The user's guide to the Interactive Geometry Software Cinderella

Skip to content

# 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.

Code | Result
--- | ---
`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>`.

Code | Result
--- | ---
`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.

Code | Result
--- | ---
`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>`.

Code | Result
--- | ---
`triples([1, 2, 3, 4])` | `[[1,2,3], [1,2,4], [1,3,4], [2,3,4]]`

#### Creating the direct product of two lists: `directproduct(<list1>,<list2>)`

Description: This operator produces a list that resembles the direct product of two given lists. The direct products consists of all pairs whose first element is taken from `<list1>` and whose second element is taken from `<list2>`.

Code | Result
--- | ---
`directproduct([1,2,3], ["A", "B"])` | `[[1,"A"], [1,"B"], [2,"A"], [2,"B"], [3,"A"], [3,"B"]]`

#### Flattening a nested list lists: `flatten(<list>)`

Description: This operator takes a list that may itself again consist of lists. It returns a single list of elements that results from appending all the second order lists. Using a modifier flattening can be applied recursively. Also the levels of flattening can be controlled.

This operator produces a list that resembles the direct product of two given lists. The direct products consists of all pairs whose first element is taken from `<list1>` and whose second element is taken from `<list2>`.

Modifiers: The modifier `levels` can be set either to "all", which results in a complete recursive flattening, or it can be set to an integer, that specifies the maximal recursion level of flattening. The statement `flatten(...,levels->1)` is equivalent to `flatten(...)`.

Example: Let us assume that we set

`list=[[1,2],[3,[4,5],[6,[7,8]]],6];`

then we get the following responses to various calls of flattening:

Code | Result
--- | ---
`flatten(list)` | `[1,2,3,[4,5],[6,[7,8]],6]`
`flatten(list,levels->0)` | `[[1,2],[3,[4,5],[6,[7,8]]],6]`
`flatten(list,levels->1)` | `[1,2,3,[4,5],[6,[7,8]],6]`
`flatten(list,levels->2)` | `[1,2,3,4,5,6,[7,8],6]`
`flatten(list,levels->3)` | `[1,2,3,4,5,6,7,8,6]`
`flatten(list,levels->"all")` | `[1,2,3,4,5,6,7,8,6]`

### Order of Elements

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>`.

Code | Result
--- | ---
`reverse([1, 2, 3, 4])` | `[4, 3, 2, 1]`

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

Description: Within CindyScript, all elements are in 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 order. 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 Cinderella uses the order

booleans < numbers < strings < lists

Code | Result
--- | ---
`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.

Code | Result
--- | ---
`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"]`

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

Description: Similar to `sort(<list>, <expr>)` but with `<var>` as the run variable.

#### 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.

Code | Result
--- | ---
`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]`