list-ref - access an element of a list
(import (rnrs)) ;R6RS
(import (rnrs base)) ;R6RS
(import (scheme r5rs)) ;R7RS
(import (scheme base)) ;R7RS
Returns the kth element of list.
- Circular lists
- R7RS explicitly allows list to be circular. R6RS also allows this,
while also limiting the implementation to only checking that list
is a chain of pairs whose length is at least k + 1.
Returns a single value; an object from the list.
(list-ref '(a b c d) 2) => c
(list-ref '(a b c d)
(exact (round 1.8)))
=> c
(define lst (list 'a 'b 'c 'd))
(set-cdr! (cdddr lst) lst) ;lst is now circular
(list-ref lst 4)
=> a
Except for different error behavior, this procedure works the same everywhere.
This procedure can raise exceptions with the following condition types:
- &assertion (R6RS)
- The wrong number of arguments was passed or an argument was outside its
domain. In particular, list must be a list whose length is at least
k + 1, and k must be an exact nonnegative integer.
- R7RS
- The assertions described above are errors. Implementations may signal an
error, extend the procedure's domain of definition to include such
arguments, or fail catastrophically.
R4RS, IEEE Scheme, R5RS, R6RS, R7RS
This procedure first appeared in R2RS.