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

Program structure


A Scheme program consists of a sequence of expressions and definitions. Expressions are described in section Expressions; definitions are the subject of the rest of the present chapter.

Programs are typically stored in files or entered interactively to a running Scheme system, although other paradigms are possible; questions of user interface lie outside the scope of this report. (Indeed, Scheme would still be useful as a notation for expressing computational methods even in the absence of a mechanical implementation.)

Definitions occurring at the top level of a program can be interpreted declaratively. They cause bindings to be created in the top level environment. Expressions occurring at the top level of a program are interpreted imperatively; they are executed in order when the program is invoked or loaded, and typically perform some kind of initialization.


Definitions are valid in some, but not all, contexts where expressions are allowed. They are valid only at the top level of a <program> and, in some implementations, at the beginning of a <body>.

A definition should have one of the following forms:

Top level definitions

At the top level of a program, a definition

(define <variable> <expression>)
has essentially the same effect as the assignment expression
(set! <variable> <expression>)
if <variable> is bound. If <variable> is not bound, however, then the definition will bind <variable> to a new location before performing the assignment, whereas it would be an error to perform a set! on an unbound variable.

(define add3
  (lambda (x) (+ x 3)))
(add3 3)                    =>  6
(define first car)
(first '(1 2))              =>  1

All Scheme implementations must support top level definitions.

Some implementations of Scheme use an initial environment in which all possible variables are bound to locations, most of which contain undefined values. Top level definitions in such an implementation are truly equivalent to assignments.

Internal definitions

Some implementations of Scheme permit definitions to occur at the beginning of a <body> (that is, the body of a lambda, let, let*, letrec, or define expression). Such definitions are known as internal definitions as opposed to the top level definitions described above. The variable defined by an internal definition is local to the <body>. That is, <variable> is bound rather than assigned, and the region of the binding is the entire <body>. For example,

(let ((x 5))
  (define foo (lambda (y) (bar x y)))
  (define bar (lambda (a b) (+ (* a b) a)))
  (foo (+ x 3)))            =>  45

A <body> containing internal definitions can always be converted into a completely equivalent letrec expression. For example, the let expression in the above example is equivalent to

(let ((x 5))
  (letrec ((foo (lambda (y) (bar x y)))
           (bar (lambda (a b) (+ (* a b) a))))
    (foo (+ x 3))))

Just as for the equivalent letrec expression, it must be possible to evaluate each <expression> of every internal definition in a <body> without assigning or referring to the value of any <variable> being defined.

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