If the programmer can change the values of variables, how does Scheme know what those values are? How does it keep track of them?
Whenever it needs to determine the value of a variable, Scheme consults an internal table, called an environment, that contains an entry for each variable that has a value at all. You can think of this environment as a box of three-by-five-inch index cards, one for each variable, with the variable itself at the top for easy reference and the value of the variable written in pencil in the middle of the card.
When Scheme starts running, either in interactive mode or in batch mode, it
builds an initial ``top-level'' environment containing entries for any
variables that the implementers have chosen to define, including names for
built-in procedures (such as quotient and *).
The definitions and commands that make up a Scheme program are evaluated in
this top-level environment.
The entry for a variable in an environment -- the ``index card'' that records the variable and its value -- is called a binding, and a variable that has such an entry is said to be bound in that particular environment.
How can I find out the value of a variable?
In interactive mode, just type in the variable. Scheme treats this as a
command to evaluate the variable and will reply by displaying the resulting
value. For example, if the variable quantity has been given the
value 7, the exchange will look like this:
> quantity 7
And what if the variable quantity hasn't been given any value?
Then the exchange will look more like this:
You can't get a value for a variable that isn't bound.> quantity ERROR: unbound variable: quantity ; in expression: quantity ; in top level environment.
What happens if I give Scheme the name of a procedure?
Try it and see. Your Scheme implementation should make some kind of an effort to display a representation of that procedure; the details vary from one implementation to another. Here's a typical result:
> quotient #<primitive-procedure quotient>
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/bindings.html