Defining Names
The special form define
is used to define variables and functions in Scheme. There are two versions of the define
special form. To define variables, we use the define
form with the following syntax:
(define <name> <expression>)
The rules to evaluate this expression are
- Evaluate the
<expression>
. - Bind its value to the
<name>
in the current frame. - Return
<name>
.
The second version of define
is used to define procedures:
(define (<name> <param1> <param2> ...) <body> )
To evaluate this expression:
- Create a lambda procedure with the given parameters and
<body>
. - Bind the procedure to the
<name>
in the current frame. - Return
<name>
.
The following two expressions are equivalent:
scm> (define foo (lambda (x y) (+ x y)))
foo
scm> (define (foo x y) (+ x y))
foo
define
is a special form because its operands are not evaluated at all! For example, <body>
is not evaluated when a procedure is defined, but rather when it is called. <name>
and the parameter names are all names that should not be evaluated when executing this define
expression.