::: (eval exp env) returns the result of evaluating ;;; expression exp in the environment env. (define (eval exp env) (cond ((self-evaluating? exp) exp) ((variable? exp) (lookup-variable-value exp env)) ((quoted? exp) (text-of-quotation exp)) ((assignment? exp) (eval-assignment exp env)) ((definition? exp) (eval-definition exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (make-procedure (lambda-parameters exp) (lambda-body exp) env)) ((begin? exp) (eval-sequence (begin-actions exp) env)) ((cond? exp) (eval (cond->if exp) env)) ((application? exp) (apply (eval (operator exp) env) (list-of-values (operands exp) env))) (else (error "Unknown expression type -- EVAL" exp)))) ;;; (apply procedure arguments) returns the result of ;;; applying procedure to arguments. (define (apply procedure arguments) (cond ((primitive-procedure? procedure) (apply-primitive-procedure procedure arguments)) ((compound-procedure? procedure) (eval-sequence (procedure-body procedure) (extend-environment (procedure-parameters procedure) arguments (procedure-environment procedure)))) (else (error "Unknown procedure type -- APPLY" procedure)))) ;;; Auxiliary functions (define (self-evaluating? exp) (cond ((number? exp) true) ((string? exp) true) (else false))) ;;; (lookup-variable-value var env) searches environment env ;;; for variable var and returns the corresponding value. (define (lookup-variable-value var env) ... ) ;;; (eval-assignment exp env) sets the variable of expression exp ;;; to the value of exp in env. (define (eval-assignment exp env) (set-variable-value! (assignment-variable exp) (eval (assignment-value exp) env) env) 'ok) ;;; (list-of-values exps env) returns a list of the values ;;; of the expressions in exps in env. (define (list-of-values exps env) (if (no-operands? exps) '() (map (lambda (operand) (eval operand env)) exps))) ;;; (extend-environment pars args env) extends env by binding ;;; each argument in arg to the corresponding parameter in pars. (define (extend-environment pars args env) ... ) ...