exercise-6.scm (1473B)
1 (define-library (sicp solutions chapter-4 exercise-6) 2 (import (scheme base) 3 (ice-9 match)) 4 5 (begin 6 (define (eval exp env) 7 (cond ((self-evaluating? exp) exp) 8 ((variable? exp) (lookup-variable-value exp env)) 9 ((quoted? exp) (text-of-quotation exp)) 10 ((assignment? exp) (eval-assignment exp env)) 11 ((definition? exp) (eval-definition exp env)) 12 ((if? exp) (eval-if exp env)) 13 ((lambda? exp) 14 (make-procedure (lambda-parameters exp) 15 (lambda-body exp) 16 env)) 17 ((begin? exp) 18 (eval-sequence (begin-actions exp) env)) 19 ((cond? exp) (eval (cond->if exp) env)) 20 ((let? exp) (eval (let->combination exp) env)) 21 ((application? exp) 22 (apply (eval (operator exp) env) 23 (list-of-values (operands exp) env))) 24 (else 25 (error "Unknown expression type -- EVAL" exp)))) 26 27 (define (let? exp) (eqv? (car exp) 'let)) 28 29 (define (let->combination exp) 30 (match exp 31 (`(let ,variables ,first-code-expression . ,rest-of-code-expressions) 32 (let ((variable-names (map car variables)) 33 (variable-expressions (map cadr variables))) 34 `((lambda ,variable-names 35 ,first-code-expression 36 ,@rest-of-code-expressions) 37 ,@variable-expressions)))))))