Go to the first, previous, next, last section, table of contents.


Lists

List is represented by a sequence of elements within parentheses. Each element is separated by space.

(1 2 3 4 5)
(apple banana orange)
(#\H #\e #\l #\l #\o)

Evaluation of a list is conducted in two different ways depending on the first element in the list. When evaluation of the first element results in a function, remaining elements are evaluated and the function is called with those evaluated values as arguments. For example, the identifier + evaluates to a function that returns the sum of its arguments.

(+ 1 2)             ==> 3
(+ 1 2 3 4)         ==> 10
(+ (+ 1 2) (+ 3 4)  ==> 10

In another form of evaluation, the first element represents an invocation of pre-defined special form. How the remaining elements are evaluated depends on the individual special forms. For example, define represents a special form that defines a variable. The first argument indicates the name of the variable, and the evaluation of the second argument specifies the value of the variable.

(define a (+ 10 20))

Evaluation of the above form returns an unspecified value. As a side effect, it defines a variable named a whose value is 30.

In Scheme, lists are constructed from pairs. A pair is a record structure with two fields called the car and cdr fields (for historical reasons). A two-element list is a pair whose car is the first element and whose cdr is a pair whose car is the second element and whose cdr is the empty list. The empty list is a special object of its own and is represented by ().

The most general notation for Scheme pairs is the "dotted" notation (c1 . c2) where c1 is the value of the car field and c2 is the value of the cdr field. For example (4 . 5) is a pair whose car is 4 and whose cdr is 5. For example,

(a b c d e)

and

(a . (b . (c . (d . (e . ())))))

are equivalent notations for a list of symbols.

A chain of pairs not ending in the empty list is called an improper list. The list and dotted notations can be combined to represent improper lists:

(a b c . d)

is equivalent to

(a . (b . (c . d)))


Go to the first, previous, next, last section, table of contents.