-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06.rkt
189 lines (151 loc) · 4.38 KB
/
06.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#lang racket
(require rackunit)
(require "00-preface.rkt")
; AE is an ArithmeticExpression and is one of:
; - Atom
; - AE + AE
; - AE * AE
; - AE ^ AE
; S-expression -> Boolean
; check if the expression constans only numbers and +, * and ^
; (define (numbered? x) #f)
(define numbered?
(lambda (axpr)
(cond
((atom? axpr) (number? axpr))
(else
(and (numbered? (car axpr))
(numbered? (car (cdr (cdr axpr)))))))))
(check-eq? (numbered? 1) #t)
(check-eq? (numbered? '(3 + (4 ^ 5))) #t)
(check-eq? (numbered? '(2 * sausage)) #f)
; S-expression -> Number
; evaluate a numeric expression
; (define (value nexp) 0) ;stub
(define value
(lambda (nexp)
(cond
((atom? nexp) nexp)
((eq? '+ (car (cdr nexp)))
(+ (value (car nexp))
(value (car (cdr (cdr nexp))))))
((eq? '* (car (cdr nexp)))
(* (value (car nexp))
(value (car (cdr (cdr nexp))))))
((eq? '^ (car (cdr nexp)))
(expt (value (car nexp))
(value (car (cdr (cdr nexp)))))))))
(check-eq? (value 13) 13)
(check-eq? (value '(1 + 3)) 4)
(check-eq? (value '(1 + (10 + 2))) 13)
(check-eq? (value '(1 + (3 * 4))) 13)
(check-eq? (value '(1 + (3 ^ 4))) 82)
; S-expression -> Number
; (define (value.v2 nexp) 0) ;stub
(define value.v2
(lambda (nexp)
(cond
((number? nexp) nexp)
((eq? '+ (car nexp))
(+ (value.v2 (car (cdr nexp)))
(value.v2 (car (cdr (cdr nexp))))))
((eq? '* (car nexp))
(* (value.v2 (car (cdr nexp)))
(value.v2 (car (cdr (cdr nexp))))))
((eq? '^ (car nexp))
(expt (value.v2 (car (cdr nexp)))
(value.v2 (car (cdr (cdr nexp)))))))))
(check-eq? (value.v2 4) 4)
(check-eq? (value.v2 '(+ 4 4)) 8)
(check-eq? (value.v2 '(* (+ 4 4) 4)) 32)
(check-eq? (value.v2 '(^ (* (+ 1 1) 2)
2))
16)
(provide 1st-sub-expr)
; S-expression -> S-expression
; (define (1st-sub-expr axpr) '())
(define 1st-sub-expr
(lambda (axpr)
(car (cdr axpr))))
(check-eq? (1st-sub-expr '(+ 1 2)) 1)
(check-equal? (1st-sub-expr '(+ (+ 3 6) 2)) '(+ 3 6))
(provide 2nd-sub-expr)
; S-expression -> S-expression
; (define (1st-sub-expr axpr) '())
(define 2nd-sub-expr
(lambda (axpr)
(car (cdr (cdr axpr)))))
(check-eq? (2nd-sub-expr '(+ 1 2)) 2)
(check-equal? (2nd-sub-expr '(+ (+ 3 6) (^ 3 3))) '(^ 3 3))
(provide operator)
; S-expression -> Atom
; retrun the operator of an expression, in this case
; it is the first Atom
; (define (operator anexpr) 'a) ;stub
(define operator
(lambda (axpr)
(car axpr)))
(check-eq? (operator '(+ 1 2)) '+)
(check-eq? (operator '(* (+ 3 6) (^ 3 3))) '*)
(define value.v3
(lambda (nexpr)
(cond
((atom? nexpr) nexpr)
((eq? '+ (operator nexpr))
(+ (value.v3 (1st-sub-expr nexpr))
(value.v3 (2nd-sub-expr nexpr))))
((eq? '* (operator nexpr))
(* (value.v3 (1st-sub-expr nexpr))
(value.v3 (2nd-sub-expr nexpr))))
((eq? '^ (operator nexpr))
(expt (value.v3 (1st-sub-expr nexpr))
(value.v3 (2nd-sub-expr nexpr)))))))
(check-eq? (value.v3 4) 4)
(check-eq? (value.v3 '(+ 4 4)) 8)
(check-eq? (value.v3 '(* (+ 4 4) 4)) 32)
(check-eq? (value.v3 '(^ (* (+ 1 1) 2)
2))
16)
; representing numbers as lists
; this is zero: '()
; this is one: '(())
; this is two: '(() ())
; ListNumber is one of:
; - '()
; - (cons '() ListNumber)
; ListNumber -> Boolean
; check if list number represents 0
; (define (lnzero? n) #f)
(define lnzero?
(lambda (n) (null? n)))
(check-eq? (lnzero? '()) #t)
(check-eq? (lnzero? '(())) #f)
; ListNumber -> ListNumber
; add representation of 1 to ListNumber
; (define (lnadd1 n) '(())) ;stub
(define lnadd1
(lambda (n)
(cons '() n)))
(check-equal? (lnadd1 '()) '(()))
(check-equal? (lnadd1 '(())) '(() ()))
; ListNumber -> ListNumber
; remove representation of 1 to ListNumber
; requirement: ListNumber can't be zero
; (define (lnsub1 n) '()) ;stub
(define lnsub1
(lambda (n)
(cdr n)))
(check-equal? (lnsub1 '(())) '())
(check-equal? (lnsub1 '(() ())) '(()))
; ListNumber ListNumber -> ListNumber
; add two LNs to each other
; (define (ln+ n m) '()) ; stub
(define ln+
(lambda (n m)
(cond
((lnzero? m) n)
(else
(cons '() (ln+ n (lnsub1 m)))))))
(check-equal? (ln+ '() '(())) '(()))
(check-equal? (ln+ '(()) '(())) '(() ()))
(check-equal? (ln+ '(() ()) '(())) '(() () ()))