# CS 61A: Homework 8

Due by 11:59pm on Wednesday, 11/12

Submission: See Lab 1 for submission instructions. We have provided a hw8.scm starter file for the questions below.

Readings: You might find the following references useful:

To complete this homework assignment on your own computer, you will either need to install Scheme or use our online Scheme interpreter.

You can load the starter file in STK using the command

``stk -load hw8.scm``

Scheme does not have a built-in testing framework. To verify behavior, we will use the following `assert-equal` procedure, which tests whether an `expression` evaluates to an expected `value`.

``````(define (assert-equal value expression)
(if (equal? value (eval expression))
(print 'ok)
(print (list 'for expression ':
'expected value
'but 'got (eval expression)))))``````

When you evaluate your solution, a long sequence of `ok` should be displayed. Any test failures will describe the error instead.

### Question 1

Write the function `deep-map`, which takes a function `fn` and a list `s` that may contain numbers or other lists. It returns a list with identical structure to `s`, but replacing each non-list element by the result of applying `fn` on it, even for elements within sub-lists.

``````(define (deep-map fn s)
'YOUR-CODE-HERE  )

(define (square x) (* x x))
(define (double x) (* 2 x))
(define (test-deep-map)
(assert-equal '(4 9) '(deep-map square '(2 3)))
(assert-equal '(4 (6 8)) '(deep-map double '(2 (3 4))))
(assert-equal '(1 4 (9 16 (25 36) ((49)) 64) (81 100))
'(deep-map square
'(1 2 (3 4  (5  6)  ((7))  8)  (9  10)))))

(test-deep-map)``````

Hint: You can use the predicate list? to check if a value is a list.

### Question 2

Write a procedure `substitute` that takes three arguments: a list `s`, an `old` word, and a `new` word. It returns a list with the elements of `s`, but with every occurrence of `old` replaced by `new`, even within sub-lists.

``````(define (substitute-list s olds news)
'YOUR-CODE-HERE  )

(define (test-substitute-list)
(assert-equal '((four calling birds) (three french hens) (two turtle doves))
'(substitute-list
'((4 calling birds) (3 french hens) (2 turtle doves))
'(1 2 3 4)
'(one two three four))))

(test-substitute-list)``````

### Question 3

Write `substitute-list`, that takes a list `s`, a list of old words, and a list of new words; the last two lists must be the same length. It returns a list with the elements of `s`, but with each word that occurs in the second argument replaced by the corresponding word of the third argument.

### Differentiation

The following problems develop a system for symbolic differentiation of algebraic expressions. The `derive` Scheme procedure takes an algebraic expression and a variable and returns the derivative of the expression with respect to the variable. Symbolic differentiation is of special historical significance in Lisp. It was one of the motivating examples behind the development of the language. Differentiating is a recursive process that applies different rules to different kinds of expressions:

``````; Derive returns the derivative of exp with respect to var.
(define (derive expr var)
(cond ((number? expr) 0)
((variable? expr) (if (same-variable? expr var) 1 0))
((sum? expr) (derive-sum expr var))
((product? expr) (derive-product expr var))
((exp? expr) (derive-exp expr var))
(else 'Error)))``````

To implement the system, we will use the following data abstraction. Sums and products are lists, and they are simplified on construction:

``````; Variables are represented as symbols
(define (variable? x) (symbol? x))
(define (same-variable? v1 v2)
(and (variable? v1) (variable? v2) (eq? v1 v2)))

; Numbers are compared with =
(define (=number? expr num)
(and (number? expr) (= expr num)))

(define (make-sum a1 a2)
(cond ((=number? a1 0) a2)
((=number? a2 0) a1)
((and (number? a1) (number? a2)) (+ a1 a2))
(else (list '+ a1 a2))))
(define (sum? x)
(and (pair? x) (eq? (car x) '+)))

(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 (list '* m1 m2))))
(define (product? x)
(and (pair? x) (eq? (car x) '*)))

(define (test-sum)
(assert-equal '(+ a x)       '(make-sum 'a 'x))
(assert-equal '(+ a (+ x 1)) '(make-sum 'a (make-sum 'x 1)))
(assert-equal 'x             '(make-sum 'x 0))
(assert-equal 'x             '(make-sum 0 'x))
(assert-equal 4              '(make-sum 1 3)))

(define (test-product)
(assert-equal '(* a x) '(make-product 'a 'x))
(assert-equal 0        '(make-product 'x 0))
(assert-equal 'x       '(make-product 1 'x))
(assert-equal 6        '(make-product 2 3)))

(test-sum)
(test-product)``````

### Question 4

Implement `derive-sum`, a procedure that differentiates a sum by summing the derivatives of the `addend` and `augend`. Use the abstract data type for a sum:

``````(define (derive-sum expr var)
'YOUR-CODE-HERE  )

(define (test-derive-sum)
(assert-equal 1 '(derive '(+ x 3) 'x)))

(test-derive-sum)``````

### Question 5

Implement `derive-product`, which applies the product rule to differentiate products:

``````(define (derive-product expr var)
'YOUR-CODE-HERE  )

(define (test-derive-product)
(assert-equal 'y '(derive '(* x y) 'x))
(assert-equal '(+ (* x y) (* y (+ x 3)))
'(derive '(* (* x y) (+ x 3)) 'x)))

(test-derive-product)``````

### Question 6

Implement an abstract data type for exponentiation: a `base` raised to the power of an `exponent`. The `base` can be any expression, but assume that the `exponent` is a non-negative integer. You can simplify the cases when `exponent` is `0` or `1`, or when `base` is a number, by returning numbers from the constructor `make-exp`. In other cases, you can represent the exp as a triple `(^ base exponent)`.

Hint: The built-in procedure `expt` takes two number arguments and raises the first to the power of the second.

``````; Exponentiations are represented as lists that start with ^.
(define (make-exp base exponent)
'YOUR-CODE-HERE  )

(define (base exp)
'YOUR-CODE-HERE  )

(define (exponent exp)
'YOUR-CODE-HERE  )

(define (exp? exp)
'YOUR-CODE-HERE  )

(define x^2 (make-exp 'x 2))
(define x^3 (make-exp 'x 3))

(define (test-exp)
(assert-equal 'x '(make-exp 'x 1))
(assert-equal 1  '(make-exp 'x 0))
(assert-equal 16 '(make-exp 2 4))
(assert-equal '(^ x 2) 'x^2)
(assert-equal 'x    '(base x^2))
(assert-equal 2     '(exponent x^2))
(assert-equal true  '(exp? x^2))
(assert-equal false '(exp? 1))
(assert-equal false '(exp? 'x))
)

(test-exp)``````

### Question 7

Implement `derive-exp`, which uses the power rule to derive exps:

``````(define (derive-exp exp var)
'YOUR-CODE-HERE  )

(define (test-derive-exp)
(assert-equal '(* 2 x)                   '(derive x^2 'x))
(assert-equal '(* 3 (^ x 2))             '(derive x^3 'x))
(assert-equal '(+ (* 3 (^ x 2)) (* 2 x)) '(derive (make-sum x^3 x^2) 'x))
)

(test-derive-exp)``````