# Homework 10

*Due by 11:59pm on Thursday, 11/3*

## Instructions

Download hw10.zip.
The quiz problem can be found in the `quiz`

directory and the homework
problems can be found in the `problems`

directory.

You must run `python3 ok --submit`

two times: once inside the `quiz`

directory and once inside the `problems`

directory.

Our course uses a custom version of Scheme (which you will build for Project 4) included in the starter ZIP archive. To start the interpreter, type

`python3 scheme`

. To run a Scheme program interactively, type`python3 scheme -i <file.scm>`

. To exit the Scheme interpreter, type`(exit)`

.

**Submission:** When you are done, submit with
`python3 ok --submit`

.
You may submit more than once before the deadline; only the final submission
will be scored. Check that you have successfully submitted your code on
okpy.org.
See Lab 0
for more instructions on submitting assignments.

**Using OK:** If you have any questions about using OK, please
refer to this guide.

**Readings:** You might find the following references
useful:

# Homework Questions

Note:Q1 and Q2 (Substitute, Sub All) were extra lab questions from Lab 9. You may check the solutions if you are stuck, but we highly recommend you work through the problem on your own for practice.

### Question 1: Substitute

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.

*Hint*: The built-in `pair?`

predicate returns True if its argument is a `cons`

pair.

```
(define (substitute s old new)
'YOUR-CODE-HERE
)
```

Use OK to unlock and test your code:

```
python3 ok -q substitute -u
python3 ok -q substitute
```

### Question 2: Sub All

Write `sub-all`

, which 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.

```
(define (sub-all s olds news)
'YOUR-CODE-HERE
)
```

Use OK to unlock and test your code:

```
python3 ok -q sub-all -u
python3 ok -q sub-all
```

### 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 EXPR 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)))
; Sums are represented as lists that start with +.
(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 (list? x) (eq? (car x) '+)))
(define (addend s) (cadr s))
(define (augend s) (caddr s))
; Products are represented as lists that start with *.
(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 (list? x) (eq? (car x) '*)))
(define (multiplier p) (cadr p))
(define (multiplicand p) (caddr p))
```

### Question 3: Derive Sum

Implement `derive-sum`

, a procedure that differentiates a sum by
summing the derivatives of the `addend`

and `augend`

. Use data abstraction
for a sum:

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

Use OK to unlock and test your code:

```
python3 ok -q derive-sum -u
python3 ok -q derive-sum
```

### Question 4: Derive Product

Implement `derive-product`

, which applies the product
rule to differentiate
products:

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

Use OK to unlock and test your code:

```
python3 ok -q derive-product -u
python3 ok -q derive-product
```

### Question 5: Make Exp

Implement a data abstraction 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))
```

Use OK to unlock and test your code:

```
python3 ok -q make-exp -u
python3 ok -q make-exp
```

### Question 6: Derive Exp

Implement `derive-exp`

, which uses the power
rule to derive
exps:

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

Use OK to test your code:

`python3 ok -q derive-exp`

# Quiz

### Question 7: How Many Dots?

Implement `how-many-dots`

, which takes in a nested scheme list `s`

and returns
the number of dots that appear when it is displayed by the interpreter (not
counting decimal points). You may assume that `s`

is a nested list that
contains only numbers.

*Hints*: A dot appears when the second element of a pair is not a well formed
list. The procedures `pair?`

, `null?`

, and `number?`

test whether a value is a pair, `nil`

, or a number, respectively.

```
(define (how-many-dots s)
'YOUR-CODE-HERE
)
;;; Tests
(how-many-dots '(1 2 3))
; expect 0
(how-many-dots '(1 2 . 3))
; expect 1
(how-many-dots '((1 . 2) 3 . 4))
; expect 2
(how-many-dots '((((((1 . 2) . 3) . 4) . 5) . 6) . 7))
; expect 6
(how-many-dots '(1 . (2 . (3 . (4 . (5 . (6 . (7)))))))
; expect 0
```

Use OK to unlock and test your code:

```
python3 ok -q how-many-dots -u
python3 ok -q how-many-dots
```