MIT 6.001 Fall, 1997 Instructor: A. Meyer T.A.: A Chefter Recitation #7, Problems Done in Recitation, Fri., 10/3/97 Today, for simplicity, we'll consider abstract expressions w/o the Product case included in lecture. To work with abstract expressions we need contructors MAKE-CONTANT, MAKE-VARIABLE, and MAKE-SUM, which contruct a corresponding expression given a contant, a variable, or an addend and an augend. We also need selectors VARIABLE-NAME, CONTANT-VALUE, ADDEND, and AUGEND. We require only that the contructors and selectors obey the rules: (contant-value (make-contant c)) => c (variable-name (make-variable v)) => v (addend (make-sum e1 e2)) => e1 (augend (make-sum e1 e2)) => e2 Exercise 1. Implement the constructors and selectors. Now, we are going to define "observers" CONTANT?, VARIABLE?, and SUM? satisfying the following Contract: contract for CONSTANT? (contant? (make-contant c)) => #t (contant? (make-variable v)) => #f (contant? (make-sum e1 e2)) => #f contract for VARIABLE? (variable? (make-variable v)) => #t (variable? (make-constant c)) => #f (varaible? (make-sum e1 e2)) => #f contract for SUM? (sum? (make-sum e1 e2)) => #t (sum? (make-variable v)) => #f (sum? (make-contant c)) => #f Exercise 2. implement the observers. ----------------------------------------------------- SOLUTION 1: A Simple Implementation of Expressions (define (make-contant num) num) (define (make-variable sym) sym) (define (make-sum exp1 exp2) (list 'sum exp1 exp2)) (define (contant-value cont) cont) (define (variable-name var) var) (define addend cadr) (define augend caddr) SOLUTION 2: (define constant? number?) (define variable? symbol?) (define (sum? exp) (and (pair? exp) (eq? (car exp) 'sum)))