This document contains a brief summary about the environment model, which can be used for drawing environment diagrams.
For more information read Structure and Interpretation of Computer Programs pages 184-199.
Application of a procedure
(procedure arg1 ... argn)
- Construct a new environment where the formal parameters of the procedure definition are bound to the actual parameters of the procedure application.
- The parent scope of the environment is the environment were the environment was defined (lexical scoping). NOT the environment where the procedure call is being executed (dynamic scoping).
- Evaluate the body of the procedure in the context of the new environment.
Evaluating a native procedure does not result in the creation of a new environment, since this is implemented in the Scheme interpreter (i.e., implemented in the host language). However, a higher-order native procedure (e.g.,
apply) can still call non-native procedures and these are consequently evaluated according to the same application rules.
Evaluation of a lambda expression
(lambda (par1 ... parn) body)
The resulting procedure object contains…
- The formal parameter (variable names) of the anonymous procedure.
- The body of the anonymous procedure.
- A pointer to the environment in which the procedure was defined.
(define name expression)
Definition of a variable will create a binding in the current environment by associating the symbol with the value obtained by evaluating the expression.
Evaluating a set!
(set! variable value)
The evaluation of a
set! operation in an environment will result in the first binding that can be found with the given variable name being modified. If no binding exists in a given environment, its lexical environment will be considered. If no binding of that variable name exists, the result is undefined (some Scheme interpreters will raise an error, other will return the new variable).
(define (name par1 ... parn) body)
This case is identical to the evaluation of
(define name (lambda (par1 ... parn) body)).