Next: Binding Reflection, Previous: Local Bindings, Up: Binding Constructs
A define
form which appears inside the body of a lambda
,
let
, let*
, letrec
or equivalent expression is
called an internal definition. An internal definition differs
from a top level definition (see Top Level), because the definition
is only visible inside the complete body of the enclosing form. Let us
examine the following example.
(let ((frumble "froz")) (define banana (lambda () (apple 'peach))) (define apple (lambda (x) x)) (banana)) => peach
Here the enclosing form is a let
, so the define
s in the
let
-body are internal definitions. Because the scope of the
internal definitions is the complete body of the
let
-expression, the lambda
-expression which gets bound
to the variable banana
may refer to the variable apple
,
even though it's definition appears lexically after the definition
of banana
. This is because a sequence of internal definition
acts as if it were a letrec
expression.
(let () (define a 1) (define b 2) (+ a b))
is equivalent to
(let () (letrec ((a 1) (b 2)) (+ a b)))
Another noteworthy difference to top level definitions is that within
one group of internal definitions all variable names must be distinct.
That means where on the top level a second define for a given variable
acts like a set!
, an exception is thrown for internal definitions
with duplicate bindings.