Node:Indexing, Next:, Up:Sequences



Indexing

To select the n'th element of a sequence s do: s n. Indexing starts at zero, thus the first element is s 0.

Negative subscripts count from the back, where s -2 is the last element, s -3 is the penultimate element, and so on. The index -1 represents the non-existent element at the end of the sequence.

Sometimes it is more helpful to think of the indexes as marking positions between elements. Thus index 0 represents the beginning of the sequence, index 1 is the position just after the first element, and so on. Thus a non-negative index counts the elements from the beginning of the sequence. Then the index -1 represents the end of the sequence (the position just after the last element), and a negative index represents the number of elements from the end of the sequence, counting down from -1.

If an "index" is a sequence, it is used to select a sequence of elements: S [I1 I2 ... In] evaluates to: [(S I1) (S I2) ... (S In)].

If extra arguments are given, they are distributed: S [I1 I2 ... In] X ... Z evaluates to the same value as: [(S I1 X ... Z) (S I2 X ... Z) ... (S In X ... Z)]. (However, the expressions X ... Y are only evaluated once. Also, the result applications are evaluated on demand.)

This feature is useful to get a "slice" of a multi-dimentional array: A [3 4] [1 2] evaluates to: [[(A 3 1) (A 3 2)] [(A 4 1) (A 4 2)]].