;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-reader.ss" "lang")((modname 14.4.1) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp"))))) ;A operator structure is a sturcture ;(operator left right) where operator represents ;one of the primitive operators mul (multiplication), ;add (addition), sub (subtraction), and div ;(division). Left and right are numbers or symbols ;(symbols represent variables, which ultimately ;represent numbers). (define-struct add (left right)) (define-struct sub (left right)) (define-struct mul (left right)) (define-struct div (left right)) ; ;(+ 10 -10) ;(make-add 10 -10) ; ;(+ (* 20 3) 33) ;(make-add (make-mul 20 3) 33) ; ;(* 3.14 (* r r)) ;(make-mul 3.14 (make-mul 'r 'r)) ; ;(+ (* 9/5 c) 32) ;(make-add (make-mul 9/5 'c) 32) ; ;(+ (* 3.14 (* o o)) ; (* 3.14 (* i i))) ;(make-add (make-mul 3.14 (make-mul 'o 'o)) ; (make-mul 3.14 (make-mul 'i 'i))) (define struct1 (make-add (make-mul 3.14 (make-mul 'o 'o)) (make-mul 3.14 (make-mul 'i 'i)))) (define struct2 (make-add (make-mul 3.14 (make-mul 4 5)) (make-mul 3.14 (make-mul 9 2)))) ;The SchemeData that we consider are ;1. numbers ;2. symbols, and ;3. the structures add, mul, div, sub. ;Definition ;A numeric expression is one that does not ;require the use of variables for evaluation. ;(an expression that has no variables) ; ;A numeric expression is either ;1. a number or ;2. a structure (make-oper ne2 ne2) where make-oper ;corresponds to the make for the four different structures ;add, sub, mul, div (make-add, make-sub etc.) and ne1, ne2 ;are numeric expressions. ;Template ;(define (fun-for-numeric a-data) ; (cond ; [(number? a-data) ...] ; [(add? a-data) ... (fun-for-numeric (add-left a-data)) ... ; ... (fun-for-numeric (add-right a-data)) ...] ; [(sub? a-data) ... (fun-for-numeric (sub-left a-data)) ... ; ... (fun-for-numeric (sub-right a-data)) ...] ; [(mul? a-data) ... (fun-for-numeric (mul-left a-data)) ... ; ... (fun-for-numeric (mul-right a-data)) ...] ; [(div? a-data) ... (fun-for-numeric (div-left a-data)) ... ; ... (fun-for-numeric (div-right a-data)) ...])) ; ; ;numeric? : SchemeData -> boolean ;Given a-data, determine if the expression it ;represents is numeric. That is, ;numeric? evaluates true if the SchemeData lacks symbols, ;which represents an expression which ;lacks variables. (define (numeric? a-data) (cond [(number? a-data) true] [(add? a-data) (and (numeric? (add-left a-data)) (numeric?(add-right a-data)))] [(sub? a-data) (and (numeric? (sub-left a-data)) (numeric? (sub-right a-data)))] [(mul? a-data) (and (numeric? (mul-left a-data)) (numeric? (mul-right a-data)))] [(div? a-data) (and (numeric? (div-left a-data)) (numeric? (div-right a-data)))] [else false])) ; ; ;evaluate-expression : SchemeData -> number ;Given a-data, evaluates the numeric expression it represents ;and returns the value. (define (evaluate-expression a-data) (cond [(number? a-data) a-data] [(add? a-data) (+ (evaluate-expression (add-left a-data)) (evaluate-expression (add-right a-data)))] [(sub? a-data) (- (evaluate-expression (sub-left a-data)) (evaluate-expression (sub-right a-data)))] [(mul? a-data) (* (evaluate-expression (mul-left a-data)) (evaluate-expression (mul-right a-data)))] [(div? a-data) (/ (evaluate-expression (div-left a-data)) (evaluate-expression (div-right a-data)))])) ;Examples ;(evaluate-expression 5) ;5 ;(evaluate-expression (make-add 3 4)) ;7 ;(evaluate-expression (make-add (make-add 4 9) (make-add -2 4))) ;15 ;(evaluate-expression (make-mul (make-add 4 5) (make-div 2 3))) ;(* 9 2/3) ; ;checked-evaluate-expression : SchemeData -> number ;Given a-data, check to see if the expression is numeric. ;If so, evaluate the expression. Return error if a-data ;is not numeric. (define (checked-evaluate-expression a-data) (cond [(numeric? a-data) (evaluate-expression a-data)] [else (error 'checked-evaluate-expression "expected arg: numeric expression")])) ;Test ;(checked-evaluate-expression (make-mul (make-add 9 4) ; (make-div (make-add 4 5) 2))) ;(* (+ 9 4) ; (/ (+ 4 5) 2)) ;(checked-evaluate-expression (make-mul 4 'c)) ;subst : symbol number SchemeData -> SchemeData ;Given the representation of a variable V (symbol), ;the number N, and a SchemeData which represents an expression, ;produce a new SchemeData representing an expression ;where all occurrences of the variable V (symbol) ;have been replaced with the number N. (define (subst V N a-data) (cond [(number? a-data) a-data] [(symbol? a-data) (cond [(symbol=? a-data V) N] [else a-data])] [(add? a-data) (make-add (subst V N (add-left a-data)) (subst V N (add-right a-data)))] [(sub? a-data) (make-sub (subst V N (sub-left a-data)) (subst V N (sub-right a-data)))] [(mul? a-data) (make-mul (subst V N (mul-left a-data)) (subst V N (mul-right a-data)))] [(div? a-data) (make-div (subst V N (div-left a-data)) (subst V N (div-right a-data)))] [else a-data])) ;Test (evaluate-expression (subst 'number 5 (make-add (make-mul (make-div 'number 4) (make-div 'number 2)) (make-mul (make-sub 'number 2) (make-add 'number 3))))) (+ (* (/ 5 4) (/ 5 2)) (* (- 5 2) (+ 5 3)))