Next: , Previous: , Up: srfi list spec   [Index]


2.2.5.4 Selectors

Function: car pair
Function: cdr pair

R5RS These functions return the contents of the car and cdr field of their argument, respectively. Note that it is an error to apply them to the empty list.

(car '(a b c))     =>  a       | (cdr '(a b c))     =>  (b c)
(car '((a) b c d)) =>  (a)     | (cdr '((a) b c d)) =>  (b c d)
(car '(1 . 2))     =>  1       | (cdr '(1 . 2))     =>  2
(car '())          =>  *error* | (cdr '())          =>  *error*
Function: caar pair
Function: cadr pair
Function: c...r pair
Function: cdddar pair
Function: cddddr pair

R5RS These procedures are compositions of car and cdr, where for example caddr could be defined by:

(define caddr (lambda (x) (car (cdr (cdr x)))))

Arbitrary compositions, up to four deep, are provided. There are twenty-eight of these procedures in all.

Function: list-ref clist i

R5RS Return the i-th element of clist. This is the same as the car of (drop clist i). It is an error if i >= n, where n is the length of clist.

(list-ref '(a b c d) 2) => c
Function: first pair -> object
Function: second pair
Function: third pair
Function: fourth pair
Function: fifth pair
Function: sixth pair
Function: seventh pair
Function: eighth pair
Function: ninth pair
Function: tenth pair

Synonyms for car, cadr, caddr, ...

(third '(a b c d e)) => c
Function: car+cdr pair

The fundamental pair deconstructor:

(lambda (p) (values (car p) (cdr p)))

This can, of course, be implemented more efficiently by a compiler.

Function: take x i
Function: drop x i

take returns the first i elements of list x. drop returns all but the first i elements of list x.

(take '(a b c d e)  2) => (a b)
(drop '(a b c d e)  2) => (c d e)

x may be any value: a proper, circular, or dotted list:

(take '(1 2 3 . d) 2) => (1 2)
(drop '(1 2 3 . d) 2) => (3 . d)
(take '(1 2 3 . d) 3) => (1 2 3)
(drop '(1 2 3 . d) 3) => d

For a legal i, take and drop partition the list in a manner which can be inverted with append:

(append (take x i) (drop x i)) = x

drop is exactly equivalent to performing i cdr operations on x; the returned value shares a common tail with x.

If the argument is a list of non–zero length, take is guaranteed to return a freshly–allocated list, even in the case where the entire list is taken, e.g. (take lis (length lis)).

Function: take-right flist i
Function: drop-right flist i

take-right returns the last i elements of flist. drop-right returns all but the last i elements of flist.

(take-right '(a b c d e) 2) => (d e)
(drop-right '(a b c d e) 2) => (a b c)

The returned list may share a common tail with the argument list.

flist may be any finite list, either proper or dotted:

(take-right '(1 2 3 . d) 2) => (2 3 . d)
(drop-right '(1 2 3 . d) 2) => (1)
(take-right '(1 2 3 . d) 0) => d
(drop-right '(1 2 3 . d) 0) => (1 2 3)

For a legal i, take-right and drop-right partition the list in a manner which can be inverted with append:

(append (take flist i) (drop flist i)) = flist

The return value of take-right is guaranteed to share a common tail with flist.

If the argument is a list of non–zero length, drop-right is guaranteed to return a freshly–allocated list, even in the case where nothing is dropped, e.g. (drop-right lis 0).

Function: take! x i
Function: drop-right! flist i

take! and drop-right! are linear–update variants of take and drop-right: the procedure is allowed, but not required, to alter the argument list to produce the result.

If x is circular, take! may return a shorter–than–expected list:

(take! (circular-list 1 3 5) 8) => (1 3)
(take! (circular-list 1 3 5) 8) => (1 3 5 1 3 5 1 3)
Function: split-at x i
Function: split-at! x i

split-at splits the list x at index i, returning a list of the first i elements, and the remaining tail. It is equivalent to:

(values (take x i) (drop x i))

split-at! is the linear–update variant. It is allowed, but not required, to alter the argument list to produce the result.

(split-at '(a b c d e f g h) 3)
=> (a b c)
   (d e f g h)
Function: last pair
Function: last-pair pair

last returns the last element of the non–empty, finite list pair. last-pair returns the last pair in the non–empty, finite list pair.

(last '(a b c)) => c
(last-pair '(a b c)) => (c)

Next: , Previous: , Up: srfi list spec   [Index]