6.1.7 Finding a subsequence or a sublist
The bracket notation used to find elements of sequences and lists can
also be used to extract a range of elements.
If S is a sequence or list of size n, then
S[n1..n2] returns the subsequence or sublist
of S consisting of the elements with indices from n1 to
n2, where 0 ≤ n1≤ n2 < s (in Xcas syntax mode) or 0 <
n1≤ n2 ≤ s in other syntax modes.
For lists, the at
command can also be used to get a sublist.
-
at takes two arguments:
-
L, a list.
- n1..n2, a range of integers.
- at(L,n1..n2) returns the sublist
of L consisting of the elements with indices from n1 to
n2.
Again, at cannot be used for sequences.
An alternative to using at for finding a sublist is the
mid command,
which again cannot be used for sequences.
-
mid takes two mandatory and one optional argument:
-
L, a list.
- n, the index of the beginning of the sublist.
- Optionally, l, the length of the sublist.
- mid(L,n ⟨,l⟩) returns the sublist of L with index
beginning at n. With the option l, the length of the sublist
will be l, otherwise it will go to the end of the list L.
Examples