Next: , Previous: , Up: scheme basic   [Index]


3.5.2 Variables, keywords, and regions

Within the body of a library or top–level program, an identifier may name a kind of syntax, or it may name a location where a value can be stored. An identifier that names a kind of syntax is called a keyword, and is said to be bound to that kind of syntax (or, in the case of a syntactic abstraction, a transformer that translates the syntax into more primitive forms). An identifier that names a location is called a variable and is said to be bound to that location. At each point within a top–level program or a library, a specific, fixed set of identifiers is bound; the set of these identifiers, the set of visible bindings, is known as the environment in effect at that point.

Certain forms are used to create syntactic abstractions and to bind keywords to transformers for those new syntactic abstractions, while other forms create new locations and bind variables to those locations; collectively, these forms are called binding constructs. Some binding constructs take the form of definitions, while others are expressions. With the exception of exported library bindings, a binding created by a definition is visible only within the body in which the definition appears, e.g. the body of a library, top–level program, or lambda expression. Exported library bindings are also visible within the bodies of the libraries and top–level programs that import them.

Expressions that bind variables include the lambda, let, let*, letrec, letrec*, let-values, and let*-values forms from the base library. Of these, lambda is the most fundamental. Variable definitions appearing within the body of such an expression, or within the bodies of a library or top–level program, are treated as a set of letrec* bindings. In addition, for library bodies, the variables exported from the library can be referenced by importing libraries and top–level programs.

Expressions that bind keywords include the let-syntax and letrec-syntax forms. A define form is a definition that creates a variable binding, and a define-syntax form is a definition that creates a keyword binding.

Scheme is a statically scoped language with block structure. To each place in a top–level program or library body where an identifier is bound there corresponds a region of code within which the binding is visible. The region is determined by the particular binding construct that establishes the binding; if the binding is established by a lambda expression, for example, then its region is the entire lambda expression. Every mention of an identifier refers to the binding of the identifier that establishes the innermost of the regions containing the use. If a use of an identifier appears in a place where none of the surrounding expressions contains a binding for the identifier, the use may refer to a binding established by a definition or import at the top of the enclosing library or top–level program. If there is no binding for the identifier, it is said to be unbound.


Next: , Previous: , Up: scheme basic   [Index]