How permanent are definitions?
A definition creates a binding that lasts as long as Scheme is running -- until the end of an interactive session, or throughout the execution of a batch-mode program. But bindings are not carried over from one run to another; all environments are discarded at the end of the run, and a fresh one is constructed each Scheme is started up. If you want to use the same definitions in several interactive sessions, you should type them into a text editor and save them in a file. Your Scheme implementation will provide some way of loading in such a file at the beginning of a session.
Is there any way to undo a top-level definition -- to remove the index card from the box?
No -- once a binding has been created, Scheme provides no way to uncreate it. You can redefine the same variable, so that a different value appears on the index card, but you can't remove the card from the box.
This accumulation of bindings could be rather inconvenient if you were working on a large program, perhaps in collaboration with half a dozen other programmers -- you'd all have to remember which variables had already been used, and never duplicate or recycle variables. Fortunately, Scheme also provides ways to create local bindings -- index cards that are consulted only when a variable is evaluated in a specific context, usually a very small context created by a single member of a programming team. The same variable can be used in many such contexts, and uses in different contexts will not conflict with one another because they will involve different bindings.
So local bindings are temporary, while the bindings created by top-level definitions last to the end of the program?
Not quite. It's not a matter of how long they last -- local bindings may remain on file just as long as the global bindings created by top-level definitions do. The difference is that a variable that has a global binding can be used anywhere in a program, so long as the definition of the variable is processed before the value of the variable is needed. A local binding can be used only within a short and clearly delimited stretch of program text.
It may be helpful to think in terms of environments. Top-level definitions add entries to the top-level, global environment. Adding a local binding, on the other hand, creates a new, local environment, which will be consulted only for variables occurring in a short section of the program.
I'm confused. Can you give me an example?
For an example in Scheme, proceed to the next topic. But here's an analogy: Imagine an algebra class in which the teacher is going over a sequence of problems from the textbook. In discussing problem 1, he says, ``Let a equal 7,'' and goes on to talk about the problem for a while, on the assumption that the variable a stands for 7. Then, later on in the hour, while discussing problem 12, the teacher says ``Let a equal 0,'' and a student complains, ``Earlier you said that a was 7!'' The student is assuming that a is bound globally, whereas in fact the teacher is trying to create a separate local binding for a in each problem.
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/global-and-local.html