-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex520.scm
67 lines (60 loc) · 1.7 KB
/
ex520.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
61
62
63
64
65
66
; Answers for 5-2-0
; Exercise 5.7
(define (assert= x y)
(if (= x y)
true
(error "Assertion error - values should be equal" x y)))
(define expt-machine
(make-machine
'(continue n b val)
(list (list '= =)
(list '* *)
(list '- -))
'(controller
(assign continue (label expt-done))
expt-loop
(test (op =) (reg n) (const 0))
(branch (label base-case))
;; set up to compute expt for n - 1
(save continue)
(assign continue (label after-expt))
(assign n (op -) (reg n) (const 1))
(goto (label expt-loop))
after-expt
;; multiply answer from previous expt
(restore continue)
(assign val
(op *) (reg val) (reg b))
(goto (reg continue))
base-case
(assign val (const 1))
(goto (reg continue))
expt-done)))
(define (test-expt-machine)
(set-register-contents! expt-machine 'b 2)
(set-register-contents! expt-machine 'n 3)
(start expt-machine)
(assert= (get-register-contents expt-machine 'val)
8))
(define expt-iter-machine
(make-machine
'(n b counter product)
(list (list '= =)
(list '* *)
(list '- -))
'(controller
(assign counter (reg n))
(assign product (const 1))
expt-iter
(test (op =) (reg counter) (const 0))
(branch (label expt-done))
(assign counter (op -) (reg counter) (const 1))
(assign product (op *) (reg b) (reg product))
(goto (label expt-iter))
expt-done)))
(define (test-expt-iter-machine)
(set-register-contents! expt-iter-machine 'b 2)
(set-register-contents! expt-iter-machine 'n 3)
(start expt-iter-machine)
(assert= (get-register-contents expt-iter-machine 'product)
8))