-
Notifications
You must be signed in to change notification settings - Fork 1
/
Exercise 4.1 list-of-values.rkt
166 lines (147 loc) · 3.51 KB
/
Exercise 4.1 list-of-values.rkt
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#lang racket
; Exercise 4.1. Notice that we cannot tell whether the metacircular evaluator evaluates
; operands from left to right or from right to left. Its evaluation order is inherited
; from the underlying Lisp: If the arguments to cons in list-of-values are evaluated
; from left to right, then list-of-values will evaluate operands from left to right; and
; if the arguments to cons are evaluated from right to left, then list-of-values will
; evaluate operands from right to left.
; Write a version of list-of-values that evaluates operands from left to right regardless
; of the order of evaluation in the underlying Lisp. Also write a version of
; list-of-values that evaluates operands from right to left.
; The list-of-values implementation given in the SICP book is reproduced here:
; (define (list-of-values exps env)
; (if (no-operands? exps)
; '()
; (cons
; (eval (first-operand exps) env)
; (list-of-values (rest-operands exps) env)
; )
; )
; )
; S O L U T I O N
(define (evaluate-l-to-r-list-of-values exps env)
(if (no-operands? exps)
'()
(let ((first (eval (first-operand exps) env)))
(cons
first
(evaluate-l-to-r-list-of-values (rest-operands exps) env)
)
)
)
)
(define (evaluate-r-to-l-list-of-values exps env)
(if (no-operands? exps)
'()
(let ((rest (evaluate-r-to-l-list-of-values (rest-operands exps) env)))
(cons
(eval (first-operand exps) env)
rest
)
)
)
)
; Dummy implementation (to allow compilation of the above procedures)
(define (no-operands? exps)
(if (pair? exps)
false
true
)
)
(define (first-operand exps)
(car exps)
)
(define (rest-operands exps)
(cdr exps)
)
(define (eval exp env)
(display "Evaluating ")
(display exp)
(newline)
exp
)
; Test Driver
(define (run-test return-type proc . args)
(define (print-item-list items first-time?)
(cond
((not (pair? items)) (void))
(else
(if (not first-time?)
(display ", ")
(void)
)
(print (car items))
(print-item-list (cdr items) false)
)
)
)
(display "Applying ")
(display proc)
(if (not (null? args))
(begin
(display " on: ")
(print-item-list args true)
)
(void)
)
(newline)
(let ((result (apply proc args)))
(if (not (eq? return-type 'none))
(display "Result: ")
(void)
)
(cond
((procedure? result) ((result 'print)))
; ((eq? return-type 'deque) (print-deque result))
((eq? return-type 'none) (void))
(else
(print result)
(newline)
)
)
)
(newline)
)
(define (execution-time proc . args)
(define start-time (current-milliseconds))
; (display start-time)
; (display " ")
(apply proc args)
(define end-time (current-milliseconds))
; (display end-time)
(display "Execution time of ")
(display proc)
(display ": ")
(- end-time start-time)
)
; Tests
; Test Results
Welcome to DrRacket, version 6.11 [3m].
Language: racket, with debugging; memory limit: 4096 MB.
> (define expressions (list 'a 'b 'c 'd 'e 'f 'g 'h 'i 'j))
> (define env 'env)
> (evaluate-l-to-r-list-of-values expressions env)
Evaluating a
Evaluating b
Evaluating c
Evaluating d
Evaluating e
Evaluating f
Evaluating g
Evaluating h
Evaluating i
Evaluating j
'(a b c d e f g h i j)
> (evaluate-r-to-l-list-of-values expressions env)
Evaluating j
Evaluating i
Evaluating h
Evaluating g
Evaluating f
Evaluating e
Evaluating d
Evaluating c
Evaluating b
Evaluating a
'(a b c d e f g h i j)
>