nth
retrieves a value by index
(nth seq index default?) (nth! seq index default?)
Returns the value that can be found at the specified index of its sequence. If the index is out of the bounds of the sequence, then either the default value is returned or an error is raised. Keep in mind that indexes are zero-based.
If the sequence is lazily computed, asynchronous, or otherwise incapable of indexed lookup, this function will raise an error. To perform a brute-force lookup, use the nth!
function, keeping in mind that it may never return a result.
An Example
(define l '(1 2 3 4))
(nth l 4 "wrong")
This example returns “wrong” because index 4 (the fifth index) is beyond the end of the specified list.
Indexed Sequence Application
Instead of using the nth
function, indexed sequences such as lists and vectors can also have arguments applied directly to them.
(define l '(1 2 3 4))
(l 4 "wrong")
This will yield the same result as the previous example.