list-ref - access an element of a list

LIBRARY

(import (rnrs))                     ;R6RS
(import (rnrs base))                ;R6RS
(import (scheme r5rs))              ;R7RS
(import (scheme base))              ;R7RS

SYNOPSIS

(list-ref list k)

DESCRIPTION

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.

RETURN VALUES

Returns a single value; an object from the list.

EXAMPLES

(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

COMPATIBILITY

Except for different error behavior, this procedure works the same everywhere.

ERRORS

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.

SEE ALSO

car(3scm), vector-ref(3scm), assoc(3scm)

STANDARDS

R4RS, IEEE Scheme, R5RS, R6RS, R7RS

HISTORY

This procedure first appeared in R2RS.

AUTHORS

This page is part of the scheme-manpages project. It includes materials from the RnRS documents. More information can be found at https://github.com/schemedoc/manpages/.


Markup created by unroff 1.0sc,    March 04, 2023.