SICP/ex-4_01-xx.scm
2021-01-16 05:19:25 -05:00

79 lines
2.2 KiB
Scheme

(load "util.scm")
(load "misc/evaluator.scm")
(display "\nex-4.1 - list-of-values\n")
(define (list-of-values-left-to-right exps env)
(if (no-operands? exps)
'()
(let ((first (eval (first-operand exps) env)))
(let ((rest ((list-of-values (rest-operands exps) env))))
(cons first rest)))))
(define (list-of-values-right-to-left exps env)
(if (no-operands? exps)
'()
(let ((rest ((list-of-values (rest-operands exps) env))))
(let ((first (eval (first-operand exps) env)))
(cons first rest)))))
(display "[done]\n")
(display "\nex-4.2 - explicite-call\n")
; Louis's plan does not work because the implementation would treat assignments
; and definitions as applications. That means define and set! would no longer be
; special forms.
(define (application?-call exp) (tagged-list exp 'call))
(define (operator-call exp) (cadr exp))
(define (operands-call exp) (cddr exp))
(display "[done]\n")
(display "\nex-4.3 - data-directed-eval\n")
(define *eval-table* (make-hash-table))
(define (put op proc)
(hash-table/put! *eval-table* (list op) proc))
(define (get op)
(hash-table/get *eval-table* (list op) #f))
(define (install-eval-package)
(put 'quote (lambda (exp env) (text-of-quotation exp)))
(put 'set! eval-assignment)
(put 'define eval-definition)
(put 'if eval-if)
(put 'lambda
(lambda (exp env)
(make-procedure (lambda-parameters exp)
(lambda-body exp)
env)))
(put 'begin (lambda (exp env) (eval-sequence (begin-actions exp) env)))
(put 'cond (lambda (exp env) (eval (cond->if exp) env)))
(put 'and (lambda (exp env) 'TODO))
(put 'or (lambda (exp env) 'TODO))
(display "[install-eval-package]\n")
'done)
(install-eval-package)
(define (eval exp env)
(cond
((self-evaluating? exp) exp)
((variable? exp) (lookup-variable-value exp env))
((and (not (null? exp))
(get (car exp)))
((get (car exp)) exp env))
((application? exp)
(apply (eval (operator exp) env)
(list-of-values (operands exp) env)))
(else (error "Unknown expression type -- EVAL" exp))))
(display "[done]\n")
(display "\nex-4.5 - and/or\n")
(display "\nex-4.6\n")