reverse(3scm) Scheme Programmer's Manual reverse(3scm)

reverse - reverse a list

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

(reverse list)

Returns a newly allocated list consisting of the elements of list in reverse order.
R6RS
Implementations must verify that list is a proper list.

Chibi Scheme
Circular lists are not detected. Improper lists are handled as if they were terminated by the last non-pair in the chain.

Returns a single value; a list.

(reverse '(a b c))
        => (c b a)
(reverse '(a (b c) d (e (f))))
        => ((e (f)) d (b c) a)

In R6RS implementations the following code is guaranteed to raise an exception. Non-R6RS implementations will commonly detect the problem or loop until the process runs out of memory.

(import (rnrs) (rnrs mutable-pairs))
(let ((xs (list 1 2)))
  (set-cdr! (cdr xs) xs)
  (reverse xs))

This procedure can raise exceptions with the following condition types:
&assertion (R6RS)
The wrong number of arguments was passed or the argument was not a proper list.
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.

list(3scm), append(3scm), list-copy(3scm)

R4RS, IEEE Scheme, R5RS, R6RS, R7RS

This procedure was already present in the first versions of Scheme, even those running on MacLisp. It was also present in LISP 1.5.

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

2022-04-21