2.5 Repetitions
As we saw in Lists, the list form supports repetition binding via ..., and it can use a repetition via ... when creating a new list:
> [x, ..., 100]
[1, 2, 3, 100]
In either bindings or expressions, ... can be nested to create and use repetitions of greater depth. For example, using ... within a term that is already before ... creates a binding of depth 2, which is a repetition of repetitions.
[[1, 2, 3, 100], [5, 6, 100]]
In this example, both ...s are recognized directly by the list binding form, but the list-construction expression follows a more general rule. The outer ... (i.e., the last one) allows any repetition of depth 1 before it, and [z, ..., 100] creates a repetition of depth 1 by using ... after z, which is a repetition of depth 2.
When a repetition is followed by multiple ...s in a row, as opposed to nested ...s, then the repetitions that would be accessed by nesting are flattend into a single repetition. This flattening has the effect of appending sequences.
[1, 2, 3, 5, 6]
Some other expression-like forms serve as repetition forms when they are used in a repetition position. For example, an operator that is defined by operator forms a reptition when it has repetition arguments, so a negation term -x creates a repetition of depth 1 when x is a repetition of depth 1:
[-1, -2, -3]
When a repetition is built from multiple other repetitions, the repetitions are used in parallel. For example, + used on two repetitions maps addition over the repetitions. The two repetitions must have the same length.
[5, 7, 9]
When repetitions of different depths are combined, the shallower repetition is repeated for outer layers of the deeper repetition.
[["a1", "b2"], ["a3", "b4"], ["a5", "b6"]]
A literal value or a variable works as a repetition of depth 0. A repetition of depth 0 is not useful in itself, but it’s useful in combination with a repetition of greater depth. For example, using 1 as a repetition lets us add it to every element of a repetition.
[2, 3, 4]
[6, 7, 8]
> [five, ...]
five: used with wrong repetition depth
expected: 0
actual: 1
Analogous to lists, map and set constructions work as repetition forms.
> [{x}, ...]
[{1}, {2}, {3}]
> [{x: #true}, ...]
[{1: #true}, {2: #true}, {3: #true}]
Function calls, the . operator, array or map access via [], and syntax templates all work as repetition forms, too, given other repetitions to start with.
> class Posn(x, y)
[Posn(x, y), Posn(y, x)]
['z + 2', 'z + 4', 'z + 6']