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-intermediate-reader.ss" "lang")((modname 17.7.1) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "dir.ss" "teachpack" "htdp") (lib "hangman.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "dir.ss" "teachpack" "htdp") (lib "hangman.ss" "teachpack" "htdp")))))
4 ;A operator structure is a sturcture
5 ;(operator left right) where operator represents
6 ;one of the primitive operators mul (multiplication),
7 ;add (addition), sub (subtraction), and div
8 ;(division). Left and right are numbers or symbols
9 ;(symbols represent variables, which ultimately
10 ;represent numbers).
11 (define-struct add (left right))
12 (define-struct sub (left right))
13 (define-struct mul (left right))
14 (define-struct div (left right))
15 ;
16 ;(+ 10 -10)
17 ;(make-add 10 -10)
18 ;
19 ;(+ (* 20 3) 33)
20 ;(make-add (make-mul 20 3) 33)
21 ;
22 ;(* 3.14 (* r r))
23 ;(make-mul 3.14 (make-mul 'r 'r))
24 ;
25 ;(+ (* 9/5 c) 32)
26 ;(make-add (make-mul 9/5 'c) 32)
27 ;
28 ;(+ (* 3.14 (* o o))
29 ; (* 3.14 (* i i)))
30 ;(make-add (make-mul 3.14 (make-mul 'o 'o))
31 ; (make-mul 3.14 (make-mul 'i 'i)))
33 (define struct1 (make-add (make-mul 3.14 (make-mul 'o 'o))
34 (make-mul 3.14 (make-mul 'i 'i))))
35 (define struct2 (make-add (make-mul 3.14 (make-mul 4 5))
36 (make-mul 3.14 (make-mul 9 2))))
38 ;The SchemeData that we consider are
39 ;1. numbers
40 ;2. symbols, and
41 ;3. the structures add, mul, div, sub.
43 ;Definition
44 ;A numeric expression is one that does not
45 ;require the use of variables for evaluation.
46 ;(an expression that has no variables)
47 ;
48 ;A numeric expression is either
49 ;1. a number or
50 ;2. a structure (make-oper ne2 ne2) where make-oper
51 ;corresponds to the make for the four different structures
52 ;add, sub, mul, div (make-add, make-sub etc.) and ne1, ne2
53 ;are numeric expressions.
55 ;Template
56 ;(define (fun-for-numeric a-data)
57 ; (cond
58 ; [(number? a-data) ...]
59 ; [(add? a-data) ... (fun-for-numeric (add-left a-data)) ...
60 ; ... (fun-for-numeric (add-right a-data)) ...]
61 ; [(sub? a-data) ... (fun-for-numeric (sub-left a-data)) ...
62 ; ... (fun-for-numeric (sub-right a-data)) ...]
63 ; [(mul? a-data) ... (fun-for-numeric (mul-left a-data)) ...
64 ; ... (fun-for-numeric (mul-right a-data)) ...]
65 ; [(div? a-data) ... (fun-for-numeric (div-left a-data)) ...
66 ; ... (fun-for-numeric (div-right a-data)) ...]))
69 ;
70 ;
71 ;numeric? : SchemeData -> boolean
72 ;Given a-data, determine if the expression it
73 ;represents is numeric. That is,
74 ;numeric? evaluates true if the SchemeData lacks symbols,
75 ;which represents an expression which
76 ;lacks variables.
78 (define (numeric? a-data)
79 (cond
80 [(number? a-data) true]
81 [(add? a-data) (and (numeric? (add-left a-data))
82 (numeric?(add-right a-data)))]
83 [(sub? a-data) (and (numeric? (sub-left a-data))
84 (numeric? (sub-right a-data)))]
85 [(mul? a-data) (and (numeric? (mul-left a-data))
86 (numeric? (mul-right a-data)))]
87 [(div? a-data) (and (numeric? (div-left a-data))
88 (numeric? (div-right a-data)))]
89 [else false]))
91 ;
92 ;
93 ;evaluate-expression : SchemeData -> number
94 ;Given a-data, evaluates the numeric expression it represents
95 ;and returns the value.
97 (define (evaluate-expression a-data)
98 (cond
99 [(number? a-data) a-data]
100 [(add? a-data) (+ (evaluate-expression (add-left a-data))
101 (evaluate-expression (add-right a-data)))]
102 [(sub? a-data) (- (evaluate-expression (sub-left a-data))
103 (evaluate-expression (sub-right a-data)))]
104 [(mul? a-data) (* (evaluate-expression (mul-left a-data))
105 (evaluate-expression (mul-right a-data)))]
106 [(div? a-data) (/ (evaluate-expression (div-left a-data))
107 (evaluate-expression (div-right a-data)))]))
109 ;Examples
110 ;(evaluate-expression 5)
111 ;5
112 ;(evaluate-expression (make-add 3 4))
113 ;7
114 ;(evaluate-expression (make-add (make-add 4 9) (make-add -2 4)))
115 ;15
116 ;(evaluate-expression (make-mul (make-add 4 5) (make-div 2 3)))
117 ;(* 9 2/3)
119 ;checked-evaluate-expression : SchemeData -> number
120 ;Given a-data, check to see if the expression is numeric.
121 ;If so, evaluate the expression. Return error if a-data
122 ;is not numeric.
124 (define (checked-evaluate-expression a-data)
125 (cond
126 [(numeric? a-data) (evaluate-expression a-data)]
127 [else (error 'checked-evaluate-expression "expected arg: numeric expression")]))
129 ;Test
130 ;(checked-evaluate-expression (make-mul (make-add 9 4)
131 ; (make-div (make-add 4 5) 2)))
132 ;(* (+ 9 4)
133 ; (/ (+ 4 5) 2))
134 ;(checked-evaluate-expression (make-mul 4 'c))
136 ;subst : symbol number SchemeData -> SchemeData
137 ;Given the representation of a variable V (symbol),
138 ;the number N, and a SchemeData which represents an expression,
139 ;produce a new SchemeData representing an expression
140 ;where all occurrences of the variable V (symbol)
141 ;have been replaced with the number N.
143 (define (subst V N a-data)
144 (cond
145 [(number? a-data) a-data]
146 [(symbol? a-data) (cond
147 [(symbol=? a-data V) N]
148 [else a-data])]
149 [(add? a-data) (make-add (subst V N (add-left a-data))
150 (subst V N (add-right a-data)))]
151 [(sub? a-data) (make-sub (subst V N (sub-left a-data))
152 (subst V N (sub-right a-data)))]
153 [(mul? a-data) (make-mul (subst V N (mul-left a-data))
154 (subst V N (mul-right a-data)))]
155 [(div? a-data) (make-div (subst V N (div-left a-data))
156 (subst V N (div-right a-data)))]
157 [else a-data]))
158 ;Test
159 ;(evaluate-expression (subst 'number 5 (make-add (make-mul (make-div 'number 4)
160 ; (make-div 'number 2))
161 ; (make-mul (make-sub 'number 2)
162 ; (make-add 'number 3)))))
163 ;(+ (* (/ 5 4)
164 ; (/ 5 2))
165 ; (* (- 5 2)
166 ; (+ 5 3)))