Assignment 3: Automated Algebra

This assignment involves arithmetic expressions, which may or may not be numbered. You will want to use the following help functions:
(define atom?
  (lambda (x)
    (not (or (pair? x) (null? x)))))

(define operator car)

(define 1st-sub-exp cadr)

(define 2nd-sub-exp caddr)

(define build-aexp
  (lambda (op sub1 sub2)
    (cons op (cons sub1 (cons sub2 '())))))

(define numbered?
  (lambda (aexp)
    (cond
      ((atom? aexp)
       (number? aexp))
      (else
       (and (numbered? (1st-sub-exp aexp))
            (numbered? (2nd-sub-exp aexp)))))))

(define atom-to-function
  (lambda (x)
    (cond
      ((eq? x '+) +)
      ((eq? x '*) *)
      (else expt))))

  1. Write a function simplify which evaluates as much of an arithmetic expression as possible. For example,
    (simplify '(* 2 (+ (* a (+ 3 5)) (+ 1 4))))
    
    should return:
    (* 2 (+ (* a 8) 5))
    
  2. Modify the function atom-to-function to include subtraction and division.
  3. Write a function remove-noops which elimintates any additions of 0 or multiplications by 1. For example, both
    (remove-noops '(* 3 (* 1 (+ a 0))))
    
    and
    (remove-noops (simplify '(* 3 (* 1 (+ a (- 1 1))))))
    
    should return (* 3 a).
Hint: to compare two atoms, use eqv?. This is a built-in function which works like the function eqan? defined on p. 78.

Email a .scm file containing all of your definitions (including the help functions given above) to me.


Last modified: Thu Feb 12 11:31:53 PST 2004