-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex-4.16.scm
60 lines (55 loc) · 2.02 KB
/
ex-4.16.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
(require "./evaluator.scm")
; a
(define (lookup-variable-value var env)
(define (env-loop env)
(define (scan vars vals)
(cond ((null? vars)
(env-loop (enclosing-environment env)))
((eq? var (car vars))
(car vals))
(else (scan (cdr vars) (cdr vals)))))
(if (eq? env the-empty-environment)
(error "Unbound variable" var)
(let ((frame (first-frame env)))
(scan (frame-variables frame)
(frame-values frame)))))
(let ((result (env-loop env)))
(if (eq? '*unassigned* result)
(error "unassigned variable")
result)))
; b
; (lambda ⟨vars⟩
; (define u ⟨e1⟩)
; (define v ⟨e2⟩)
; ⟨e3⟩)
; (lambda ⟨vars⟩
; (let ((u '*unassigned*)
; (v '*unassigned*))
; (set! u ⟨e1⟩)
; (set! v ⟨e2⟩)
; ⟨e3⟩))
(require "./ex-4.8.scm")
(define (scan-out-defines exps)
(let ((define-exps (filter definition? exps))
(non-define-exps (filter (lambda (e) (not (definition? e))) exps)))
(make-let (map (lambda (e) (list (definition-variable e) '*unassigned*))
define-exps)
(append (map (lambda (e) (make-assignment (definition-variable e)
(definition-value e)))
define-exps)
non-define-exps))))
(define (main args)
(print (scan-out-defines '((define u <e1>)
(define v <e2>)
<e3>))))
;; c
;; procedure-body
;; <良い点>
;; ユーザーが入力した定義がそのまま保持できるため、デバッグ時などに便利
;; <悪い点>
;; 効率が悪くなる.procedure-bodyはapplyする度に呼ばれるので呼び出し頻度が高い.
;; make-procedure
;; <良い点>
;; scan-out-definesでコードを変換するのが一度だけになるため、効率が良い。
;; <悪い点>
;; ユーザーが入力した定義が保持が難しくなるため、デバッグが難しくなる.