Computer Science 3675
Section 001
Fall 2018
Programming Assignment 4

Assigned: Monday, October 15
Due: Monday, October 22, 11:59pm

Purpose of this assignment

This assignment gives you experience using Lisp syntax and writing in a typeless language. You will need to be cautious since the compiler does not perform any type checking.

You will use Scheme, a language in the Lisp family. You can read in the book about Scheme, or see the Scheme Reports for a collection of Scheme reports. You can see the table of contents of the version 6 report at http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-2.html#node_toc_start.

To write a value in Scheme, use (write E). To end a line, use (newline).

To use Scheme, you will find Racket convenient. You can get it from racket-lang.org. When you run it, set the language to R5RS Scheme (under legacy languages).

A Scheme comment begins with a semicolon and ends at the end of the line.

Expressions

We will use expressions that allow numeric constants, symbol x and operators +, -, * and ^. For example, '(- x (* x 3)) is expression x - 3x in Scheme. (^ A B) stands for AB

Derivatives

The derivative of an expression is a concept from calculus. You take the derivative of a function of x (given as an expression involving x), and the answer is another function of x (also given as an expression involving x.

The derivative of x (with respect to x) is 1, and the derivative of expression x2 is 2x. If you are unfamiliar with derivatives, the general rules that are needed here are given below.

Symbolic mathematics packages such as Mathematica, Maple and Derive will take derivatives for you. They do so symbolically, in the same way as you do when you are doing it by hand.

Rules for derivatives

The rules for taking the derivative a′ of an expression a (with respect to x) are as follows. Here, c is a constant and x is the independent variable.

c  =  0
x  =  1
(a+b)′  =  a′ + b
(ab)′  =  a′ − b
(ab)′  =  a(b′) + (a′)b
(ac)′  =  cac-1a

Note that there is no rule here for computing (ab)′ for an arbitrary expression b. The rule given only works when the exponent is a constant.

Simplification

The derivative rules can lead to some rather silly looking expressions. If you apply them exactly as stated, you find that the derivative of 2+3*x is 0 + 3*1 + 0*x. It is nice to do some simplifications. Do at least the following simplifications.

y + 0  =  y
0 + y  =  y
y − 0  =  y
0 * y  =  0
y * 0  =  0
1 * y  =  y
y * 1  =  y
y ^ 1  =  y
1 ^ y  =  1

Also perform constant arithmetic. For example, expression 3*4 should be simplified to 12. Expression 3^4 should be simpified to 81.

The assignment

For this assignment, you will write a Scheme program to compute the derivative of an expression. Write the following functions, as well as any helper functions that are useful in keeping function definitions short. Avoid long function definitions.

  1. (deriv E) returns the derivative of E as obtained directly from the rules, without any simplification. If E is a power expression that deriv cannot handle, it should return (deriv E), as a list whose head is the symbol deriv.

  2. (simplify E) returns a (possibly) simplified version of E. Use at least the simplifications shown above.

    Do not try to write an industrial grade simplifier. The results that you get will not satisfy a calculus teacher, but that is okay.

  3. (derivative E) returns the result of simplifying (deriv E).

Submitting the program

Please call your program deriv.scm. Submit this assignment using command

  ~abrahamsonk/3675/bin/submit 4 deriv.scm
Command
   ~abrahamsonk/3675/bin/submit 4
will tell you what you have submitted.


Asking Questions

To ask a question about your program, first submit it, but use assignment name q1. For example, use command

  ~abrahamsonk/3675/bin/submit q4 deriv.scm
Then send me an email with your question. Do not expect me to read your mind. Tell me what your question(s) are. I will look at the file that you have submitted as q4. If you have another question later, resubmit your new file as assignment q4 and send another email.