Code Review
Compare your solutions
#| BEGIN (Write your solution here) |#
(define (make-sum a1 a2)
(cond ((=number? a1 0) a2)
((=number? a2 0) a1)
((and (number? a1) (number? a2)) (+ a1 a2))
(else '(a1 + a2))
))
(define (make-product m1 m2)
(cond ((or (=number? m1 0) (=number? m2 0)) 0)
((=number? m1 1) m2)
((=number? m2 1) m1)
((and (number? m1) (number? m2)) (* m1 m2))
(else '(m1 * m2))
)
)
(define (sum? x) (and (pair? x) (eq? (cadr x) '+)))
(define (addend s) (car s))
(define (augend s) (cadr s))
(define (product? x) (and (pair? x) (eq? (cadr x) '*)))
(define (multiplier p) (car p))
(define (multiplicand p) (cadr p))
(define (=number? exp num) (and (number? exp) (= exp num)))
(define (install-product-package)
;; internal procedures
(define (deriv-product exp var)
(make-sum (make-product (multiplier exp)
(deriv (multiplicand exp) var))
(make-product (deriv (multiplier exp) var)
(multiplicand exp)))
)
;; interface to the rest of the system
(put 'deriv '* deriv-product))
(define (install-sum-package)
;; internal procedures
(define (deriv-sum exp var)
(make-sum (deriv (addend exp) var)
(deriv (augend exp) var))
)
;; interface to the rest of the system
(put 'deriv '+ deriv-sum))
#| END |#
#| BEGIN (Write your solution here) |#
(define (make-sum a1 a2)
(cond ((=number? a1 0) a2)
((=number? a2 0) a1)
((and (number? a1) (number? a2)) (+ a1 a2))
(else '(a1 + a2))
))
(define (make-product m1 m2)
(cond ((or (=number? m1 0) (=number? m2 0)) 0)
((=number? m1 1) m2)
((=number? m2 1) m1)
((and (number? m1) (number? m2)) (* m1 m2))
(else '(m1 * m2))
)
)
(define (sum? x) (and (pair? x) (eq? (cadr x) '+)))
(define (addend s) (car s))
(define (augend s) (cadr s))
(define (product? x) (and (pair? x) (eq? (cadr x) '*)))
(define (multiplier p) (car p))
(define (multiplicand p) (cadr p))
(define (=number? exp num) (and (number? exp) (= exp num)))
(define (install-product-package)
;; internal procedures
(define (deriv-product exp var)
(make-sum (make-product (multiplier exp)
(deriv (multiplicand exp) var))
(make-product (deriv (multiplier exp) var)
(multiplicand exp)))
)
;; interface to the rest of the system
(put 'deriv '* deriv-product))
(define (install-sum-package)
;; internal procedures
(define (deriv-sum exp var)
(make-sum (deriv (addend exp) var)
(deriv (augend exp) var))
)
;; interface to the rest of the system
(put 'deriv '+ deriv-sum))
#| END |#
#| BEGIN (Write your solution here) |#
(define (make-sum a1 a2)
(cond ((=number? a1 0) a2)
((=number? a2 0) a1)
((and (number? a1) (number? a2)) (+ a1 a2))
(else '(a1 + a2))
))
(define (make-product m1 m2)
(cond ((or (=number? m1 0) (=number? m2 0)) 0)
((=number? m1 1) m2)
((=number? m2 1) m1)
((and (number? m1) (number? m2)) (* m1 m2))
(else '(m1 * m2))
)
)
(define (sum? x) (and (pair? x) (eq? (cadr x) '+)))
(define (addend s) (car s))
(define (augend s) (cadr s))
(define (product? x) (and (pair? x) (eq? (cadr x) '*)))
(define (multiplier p) (car p))
(define (multiplicand p) (cadr p))
(define (=number? exp num) (and (number? exp) (= exp num)))
(define (install-product-package)
;; internal procedures
(define (deriv-product exp var)
(make-sum (make-product (multiplier exp)
(deriv (multiplicand exp) var))
(make-product (deriv (multiplier exp) var)
(multiplicand exp)))
)
;; interface to the rest of the system
(put 'deriv '* deriv-product))
(define (install-sum-package)
;; internal procedures
(define (deriv-sum exp var)
(make-sum (deriv (addend exp) var)
(deriv (augend exp) var))
)
;; interface to the rest of the system
(put 'deriv '+ deriv-sum))
#| END |#
#| BEGIN (Write your solution here) |#
(define (make-sum a1 a2)
(cond ((=number? a1 0) a2)
((=number? a2 0) a1)
((and (number? a1) (number? a2)) (+ a1 a2))
(else '(a1 + a2))
))
(define (make-product m1 m2)
(cond ((or (=number? m1 0) (=number? m2 0)) 0)
((=number? m1 1) m2)
((=number? m2 1) m1)
((and (number? m1) (number? m2)) (* m1 m2))
(else '(m1 * m2))
)
)
(define (sum? x) (and (pair? x) (eq? (cadr x) '+)))
(define (addend s) (car s))
(define (augend s) (cadr s))
(define (product? x) (and (pair? x) (eq? (cadr x) '*)))
(define (multiplier p) (car p))
(define (multiplicand p) (cadr p))
(define (=number? exp num) (and (number? exp) (= exp num)))
(define (install-product-package)
;; internal procedures
(define (deriv-product exp var)
(make-sum (make-product (multiplier exp)
(deriv (multiplicand exp) var))
(make-product (deriv (multiplier exp) var)
(multiplicand exp)))
)
;; interface to the rest of the system
(put 'deriv '* deriv-product))
(define (install-sum-package)
;; internal procedures
(define (deriv-sum exp var)
(make-sum (deriv (addend exp) var)
(deriv (augend exp) var))
)
;; interface to the rest of the system
(put 'deriv '+ deriv-sum))
#| END |#