# Homework 08:

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

## Instructions

Download hw08.zip.

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:

**Grading:** Homework is graded based on effort, not
correctness. However, there is no partial credit; you must show substantial
effort on every problem to receive any points.

# New Editor

## How to launch

In your `hw08`

folder you will find a new editor. To run this editor, run `python3 editor`

. This should pop up a window in your browser; if it does not, please navigate to localhost:31415 and you should see it.

Make sure to run `python3 ok`

in a separate tab or window so that the editor keeps running.

## Features

The `hw08.scm`

file should already be open. You can edit this file and then run `Run`

to run the code and get an interactive terminal or `Test`

to run the `ok`

tests.

`Environments`

will help you diagram your code, and `Debug`

works with environments so you can see where you are in it. We encourage you to try out all these features.

`Reformat`

is incredibly useful for determining whether you have parenthesis based bugs in your code. You should be able to see after formatting if your code looks weird where the issue is.

By default, the interpreter uses Lisp-style formatting, where the parens are all put on the end of the last line

`(define (f x) (if (> x 0) x (- x)))`

However, if you would prefer the close parens to be on their own lines as so

`(define (f x) (if (> x 0) x (- x) ) )`

you can go to Settings and select the second option.

## Warning

**The editor will neither back up nor submit on your behalf.** You still need to run `python ok -u`

to unlock cases from the terminal as well as `python ok --submit`

.

## Reporting bugs

This is new software! While we have tested it extensively, it probably has bugs. Please report them on Piazza post @2712.

# Questions

### Q1: Reverse

Write the procedure `reverse`

, which takes in a list `lst`

and outputs a reversed list. Hint: you may find the built-in function `append`

useful (documentation).

```
(define (reverse lst)
'YOUR-CODE-HERE
)
```

To test your code, if you are in the local Scheme editor, hit

`Test`

. You can click on a case, press`Run`

, and then use the`Debug`

and`Environments`

features to figure out why your code is not functioning correctly.You can also test your code from the terminal by running

`python3 ok -q reverse-simple`

### Q2: Longest increasing subsequence

Write the procedure `longest-increasing-subsequence`

, which takes in a list `lst`

and returns the
longest subsequence in which all the terms are increasing. *Note: the elements do not have to appear
consecutively in the original list*. For example, the longest increasing subsequence of
`(1 2 3 4 9 3 4 1 10 5)`

is `(1 2 3 4 9 10)`

. Assume that the longest increasing subsequence is unique.

Hint: The built-in procedures

`length`

(documentation) and`filter`

(documentation) might be helpful to solving this problem.

```
; helper function
; returns the values of lst that are bigger than x
; e.g., (larger-values 3 '(1 2 3 4 5 1 2 3 4 5)) --> (4 5 4 5)
(define (larger-values x lst)
'YOUR-CODE-HERE)
(define (longest-increasing-subsequence lst)
; the following skeleton is optional, remove if you like
(if (null? lst)
nil
(begin
(define first (car lst))
(define rest (cdr lst))
(define large-values-rest
(larger-values first rest))
(define with-first
'YOUR-CODE-HERE)
(define without-first
'YOUR-CODE-HERE)
(if 'YOUR-CONDITION-HERE
with-first
without-first))))
```

To test your code, if you are in the local Scheme editor, hit

`Test`

. You can click on a case, press`Run`

, and then use the`Debug`

and`Environments`

features to figure out why your code is not functioning correctly.You can also test your code from the terminal by running

`python3 ok -q longest-increasing-subsequence`

### 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))
```

Note that we will not test whether your solutions to this question correctly apply the chain rule. For more info, check out the extensions section.

### Q3: 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
)
```

The tests for this section aren't exhaustive, but tests for later parts will fully test it.

Before you start, check your understanding by running

`python3 ok -q derive-sum -u`

To test your code, if you are in the local Scheme editor, hit

`Test`

. You can click on a case, press`Run`

, and then use the`Debug`

and`Environments`

features to figure out why your code is not functioning correctly.You can also test your code from the terminal by running

`python3 ok -q derive-sum`

### Q4: Derive Product

Implement `derive-product`

, which applies the product
rule to differentiate
products. This means taking the multiplier and multiplicand, and then
summing the result of multiplying one by the derivative of the other.

The

`ok`

tests expect the terms of the result in a particular order. First, multiply the derivative of the multiplier by the multiplicand. Then, multiply the multiplier by the derivative of the multiplicand. Sum these two terms to form the derivative of the original product. In other words,`f' g + f g'`

not some other ordering

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

Before you start, check your understanding by running

`python3 ok -q derive-product -u`

`Test`

. You can click on a case, press`Run`

, and then use the`Debug`

and`Environments`

features to figure out why your code is not functioning correctly.You can also test your code from the terminal by running

`python3 ok -q derive-product`

### Q5: 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)`

.

You may want to use the built-in procedure

`expt`

, which 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))
```

Before you start, check your understanding by running

`python3 ok -q make-exp -u`

`Test`

. You can click on a case, press`Run`

, and then use the`Debug`

and`Environments`

features to figure out why your code is not functioning correctly.You can also test your code from the terminal by running

`python3 ok -q make-exp`

### Q6: Derive Exp

Implement `derive-exp`

, which uses the power
rule to derive exponents. Reduce
the power of the exponent by one, and multiply the entire expression by
the original exponent.

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

Before you start, check your understanding by running

`python3 ok -q derive-exp -u`

`Test`

. You can click on a case, press`Run`

, and then use the`Debug`

and`Environments`

features to figure out why your code is not functioning correctly.You can also test your code from the terminal by running

`python3 ok -q derive-exp`

### Extensions

There are many ways to extend this symbolic differentiation
system. For example, you could simplify nested exponentiation expression such
as `(^ (^ x 3) 2)`

, products of exponents such as `(* (^ x 2) (^ x 3))`

, and
sums of products such as `(+ (* 2 x) (* 3 x))`

. You could apply the chain
rule when deriving exponents, so that
expressions like `(derive '(^ (^ x y) 3) 'x)`

are handled correctly. Enjoy!