Blob


1 ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 ;; about the language level of this file in a form that our tools can easily process.
3 #reader(lib "htdp-advanced-reader.ss" "lang")((modname |32.1|) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp")))))
4 ;A SchemeData (a representation of a Scheme expression) is either
5 ;1. a number,
6 ;2. a symbol,
7 ;3. an operator structure, or
8 ;4. a function structure.
10 (define-struct add (left right))
11 (define-struct sub (left right))
12 (define-struct mul (left right))
13 (define-struct div (left right))
15 ;An operator is a structure
16 ;(make-operator left right)
17 ;where operator is replaced by one of the four operators:
18 ;1. add (addition)
19 ;2. sub (subtraction)
20 ;3. mul (multiplication)
21 ;4. div (division)
22 ;and left and right are SchemeData.
24 ;
26 ;An operator structure
28 ;(make-operator left right)
30 ;represents the expression
32 ;(operator left right)
34 ;in Scheme.
38 (define-struct function (name arg))
42 ;A function is a structure
44 ;(make-function name arg)
46 ;where name is a symbol and arg is a SchemeData.
48 ;
50 ;A function structure
52 ;(make-function name arg)
54 ;represents the expression
56 ;(name arg)
60 (define-struct definition (function parameter body))
64 ;A definition is a structure
66 ;(make-definition function parameter body)
68 ;where function and parameter are symbols and body is a SchemeData.
70 ;numeric? : SchemeData -> boolean
72 ;Given a-data, determine if the expression it
74 ;represents is numeric. That is,
76 ;numeric? evaluates true if the SchemeData lacks symbols,
78 ;which represents an expression which
80 ;lacks variables.
84 (define (numeric? a-data)
86 (cond
88 [(number? a-data) true]
90 [(add? a-data) (and (numeric? (add-left a-data))
92 (numeric?(add-right a-data)))]
94 [(sub? a-data) (and (numeric? (sub-left a-data))
96 (numeric? (sub-right a-data)))]
98 [(mul? a-data) (and (numeric? (mul-left a-data))
100 (numeric? (mul-right a-data)))]
102 [(div? a-data) (and (numeric? (div-left a-data))
104 (numeric? (div-right a-data)))]
106 [else false]))
110 ;evaluate-expression : SchemeData -> number
112 ;Given a-data, evaluates the numeric expression it represents
114 ;and returns the value.
118 (define (evaluate-expression a-data)
120 (cond
122 [(number? a-data) a-data]
124 [(add? a-data) (+ (evaluate-expression (add-left a-data))
126 (evaluate-expression (add-right a-data)))]
128 [(sub? a-data) (- (evaluate-expression (sub-left a-data))
130 (evaluate-expression (sub-right a-data)))]
132 [(mul? a-data) (* (evaluate-expression (mul-left a-data))
134 (evaluate-expression (mul-right a-data)))]
136 [(div? a-data) (/ (evaluate-expression (div-left a-data))
138 (evaluate-expression (div-right a-data)))]))
142 ;subst : symbol number SchemeData -> SchemeData
144 ;Given the representation of a variable V (symbol),
146 ;the number N, and a SchemeData which represents an expression,
148 ;produce a new SchemeData representing an expression
150 ;where all occurrences of the variable V (symbol)
152 ;have been replaced with the number N.
156 (define (subst V N a-data)
158 (cond
160 [(number? a-data) a-data]
162 [(symbol? a-data) (cond
164 [(symbol=? a-data V) N]
166 [else a-data])]
168 [(add? a-data) (make-add (subst V N (add-left a-data))
170 (subst V N (add-right a-data)))]
172 [(sub? a-data) (make-sub (subst V N (sub-left a-data))
174 (subst V N (sub-right a-data)))]
176 [(mul? a-data) (make-mul (subst V N (mul-left a-data))
178 (subst V N (mul-right a-data)))]
180 [(div? a-data) (make-div (subst V N (div-left a-data))
182 (subst V N (div-right a-data)))]
184 [else a-data]))
188 ;evaluate-with-one-def : SchemeData definition -> number
190 ;Given an-exp and P, evaluate the argument an-exp, substitute that for the parameter in P, and then evaluate the body of P.
194 (define (evaluate-with-one-def an-exp P)
196 (cond
198 [(and (definition? P)
200 (numeric? an-exp)) (evaluate-expression (subst (definition-parameter P)
202 (evaluate-expression an-exp)
204 (definition-body P)))]
206 [else (error 'evaluate-with-one-def "unexpected error")]))
210 ;evaluate-with-defs : SchemeData list-of-definitions -> list-of-numbers
212 ;Given an-exp and defs, evaluate an-exp, substitute this value in the body for each definition in the list-of-definitions in place of each one's parameter and evaluate the resulting body.
214 (define (evaluate-with-defs an-exp defs)
216 (cond
218 [(empty? defs) empty]
220 [(cons? defs) (cons (evaluate-with-one-def an-exp (first defs))
222 (evaluate-with-defs an-exp (rest defs)))]))
228 ;test-evaluate SchemeData list-of-definitions list-of-numbers -> boolean
230 ;Given an-exp, defs, and expected-result, evaluate an-exp, substitute it for the parameter for the body of each of the function definitions in defs, evaluate the resulting expressions, and compare that with expected-result. Return true if the two list-of-numbers are identical, false otherwise.
234 (define (test-evaluate an-exp defs expected-result)
236 (equal? (evaluate-with-defs an-exp defs) expected-result))