CSC151.02 2010S Functional Problem Solving : Reference

Lists


The Basics

Believe it or not, but every list operation (more or less) can be defined in terms of these five basic operations.

null
Standard list constant. The empty list.
(cons value lst)
Standard List Procedure. Create a new list by prepending value to the front of lst.
(cdr lst)
Standard List Procedure. Get a list the same as lst but without the first element.
(car lst)
Standard List Procedure. Get the first element of lst.
(null? lst)
Standard list predicate. Checks if lst is the empty list.

Making Lists and Sublists

(list val_0 val_1 ... val_n)
Standard List Procedure. Create a new list of size n+1 of the form (val_0 val_1 ... val_n).
(make-list n val)
Customary List Procedure. Make a new list that consists of n copies of val.
(list-drop lst n)
List Procedure. Build a new list that consists of all but the first n elements of lst.
(list-take lst n)
List Procedure. Build a new list that consists of the first n elements of lst.

List Iteration Procedures

(for-each proc! lst)
Standard Higher-Order List Procedure. Apply proc! to each element of the given list. Called primarily for side effects.
(map func lst)
Standard Higher-Order List Procedure. Create a new list, each of whose elements is computed by applying func to the corresponding element of lst.

Additional List Procedures

Most of these are forthcoming.

(list-ref lst n)
Standard List Procedure. Get the nth element of lst. Note that elements are numbered starting at 0.
(reverse lst)
Standard List Procedure. Build a new list whose elements are the same as those of lst, but in the opposite order.

Hybrids of car and cdr

(caar lst)
Standard List Procedure. If lst's first element is a list, gets the first element of that first element, the the car of the car of lst. If lst is not a list, or its first element is not a list, reports an error.
(cadr lst)
Standard List Procedure. Get the second element of lst, the car of the cdr of lst
(cddr lst)
Standard List Procedure. Get all but the first two elements of lst, the cdr of the cdr of lst
(caddr lst)
Standard List Procedure. Get the third element of lst, the car of the cdr of the cdr of lst.

Creative Commons License

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright (c) 2007-10 Janet Davis, Matthew Kluber, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials copyright by John David Stone and Henry Walker and used by permission.)

This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.

This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/2.5/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.