(module mp3 (lib "eopl.ss" "eopl") (require "drscheme-init.scm") (require "mp3-data-structures.scm") (provide ;; omitting functions for Task 1 from public view calculate ; task 2 value-of ; task 3 ;; omitting functions for Task 4 from public view ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; ;;; Task 2 ;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; calculate: arithmetic-exp -> Number ;;; ;;; (calculate exp) returns the numerical value of exp. ;;; The behavior is undefined if exp contains variable ;;; references. (define (calculate exp) (cases arithmetic-exp exp (constant-exp (num) num) (addition-exp (operand1 operand2) (+ (calculate operand1) (calculate operand2))) (subtraction-exp (operand1 operand2) (- (calculate operand1) (calculate operand2))) (multiplication-exp (operand1 operand2) (* (calculate operand1) (calculate operand2))) (var-exp (id) (eopl:error 'calculate "expression contains a variable")))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; ;;; Task 3 ;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; value-of: arithmetic-exp * Env -> Number ;;; ;;; (value-of exp env) returns the numerical value of exp ;;; evaluated in the environment env. The behavior is ;;; undefined if exp contains any variable not bound ;;; to a number in env. (define (value-of exp env) (cases arithmetic-exp exp (constant-exp (num) num) (addition-exp (operand1 operand2) (+ (value-of operand1 env) (value-of operand2 env))) (subtraction-exp (operand1 operand2) (- (value-of operand1 env) (value-of operand2 env))) (multiplication-exp (operand1 operand2) (* (value-of operand1 env) (value-of operand2 env))) (var-exp (id) (apply-env env id)))) )