You said that the body of a let-expression could also contain definitions. What would that look like?
Here's an example:
> (let ((number 5))
(define successor (+ number 1))
(* number successor))
30
The body of the let-expression consists of the definition (define
successor (+ number 1)) and the command (* number
successor).
When a definition appears inside a let-expression, does it affect the top-level environment, or only the local environment for the body of the let-expression?
It affects only the local environment, not the top-level one. In the
example above, the variable successor would still be unbound
in the top-level environment.
So what's the difference between an internal definition and an internal let-expression? Don't they both just create local bindings?
Yes, they're pretty much equivalent. Some people think that internal definitions are more intelligible to human readers because they eliminate one level of nested parentheses.
In all of the examples so far, the values of the locally bound variables have been numbers. But it is also possible, as we'll see shortly, for the programmer to devise new procedures of her own and to create local bindings in which the values of the bound variables are the newly devised procedures. It turns out that using internal definitions makes constructions of this sort much easier to read.
Next topic
Previous topic
Table of contents
This document is available on the World Wide Web as
http://www.math.grin.edu/~stone/scheme-web/internal-definitions.html