Fundamentals of CS I (CS151 2001S) : Reference

reverse: reverse a list

Procedurereverse
Parametersl, a list
PurposeProduces a reversed version of l.
ProducesA new list, r, whose elements are the elements of l in reverse order.
PreconditionsOne parameter (verified).
l is a list (verified).
PostconditionsDoes not affect l.
The initial element of r is the last element of l, the next element of r is the penultimate element of l, and so on and so forth.
If any elements of l are lists, does not reverse them.
Examples
> (reverse ())
()
> (reverse (list 1 2 3))
(3 2 1)
> (define lists (list (list 'a 'b 'c) (list 1) (list 4 2 1 0)))
> lists
((a b c) (1) (4 2 1 0))
> (reverse 'a)
reverse: expects argument of type <proper list>; given a

Disclaimer: I usually create these pages on the fly. This means that they are rarely proofread and may contain bad grammar and incorrect details. It also means that I may update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This page was generated by Siteweaver on Thu Feb 22 05:48:18 2001.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2001S/reverse.html.
You may validate this page's HTML.
The source was last modified Sun Jan 28 12:30:51 2001.