Assignment 1, CSC430, Fall 2018
1 Guidelines
1.1 Mutation
1.2 A note on Numbers
2 Assignment
2.1 Some Textbook Problems
2.2 Writing Implements
2.3 Low-degree Polynomials
2.4 Derivative
2.5 Binary Tree
2.6 Mirror
2.7 Substitution
2.8 Occurrences
7.1.0.1

Assignment 1, CSC430, Fall 2018

1 Guidelines

For this and all remaining assignments, every function you develop must come with the following things:

For this assignment, you must develop your solutions using the typed/racket language. Your test cases must use the (check-equal? ...) or (check-= ...) form. Use check-= to check for equality of numbers, especially when they can be inexact (floating-point) numbers.

Your solution should take the form of a single file. Solve each problem separately, ands make sure that each solution appears in a separate part of the file, with comments separating each problem’s solution.

Hand in your solution using the handin server. For help with the handin server, please see the course web page.

1.1 Mutation

There’s no need for mutation in any of the first three assignments in this class. Don’t mutate bindings, and don’t mutate structure fields. Yikes!

1.2 A note on Numbers

For this assignment (as well as all the others), there’s no need for complex numbers. Accordingly, you should use the type Real rather than the type Number. This will save you some hassle in using the check-= form that checks for numeric near-equality.

2 Assignment

2.1 Some Textbook Problems

Solve the following two problems from an older copy of the textbook How To Design Programs, available online at www.htdp.org:

  1. Problem 2.3.3

  2. Problem 3.3.3

Note: These problems come with solutions. I would encourage you to write your own solutions first, including all of the steps of the design recipe. Then, go ahead and take a look at the ones linked to on the web page.

2.2 Writing Implements

Here is a data definition for writing implements:

; represents a writing implement
(define-type Writer (U Pen Pencil))
; ink volume in ml, how-full a number in the range 0.0 to 1.0
(struct Pen ([ink-volume : Real] [how-full : Real]) #:transparent)
; length in cm
(struct Pencil ([length : Real]) #:transparent)

Develop the function how-far-to-write, which accepts a writing implement and returns the distance (in meters) that the implement will write before it’s done. Assume that pens can write 150 meters for each ml of ink left in the tank, and that pencils write 56 meters for each remaining cm of length.

2.3 Low-degree Polynomials

Develop a data definition for a polynomial (with type name Polynomial) that includes variants for linear (Ax + B) and quadratic (Ax^2 + Bx + C) polynomials of one variable. Call the variants Linear and Quadratic; each should accept the coefficients in the order given here (i.e., A comes first). For the purposes of this and the next problem, you should assume that the first coefficient (A) of a quadratic polynomial is never zero. The first coefficient of a linear polynomial, however, may be zero.

Next, define the function interp that accepts a polynomial and a value for the variable and produces the result of plugging in the value for the variable.

2.4 Derivative

Using the data definition you developed in the last problem, develop the function derivative that accepts a polynomial and returns another polynomial that represents its derivative. Ensure that your derivative function does not return a quadratic polynomial whose first coefficient is zero.

2.5 Binary Tree

Develop a data definition for a full binary tree called BTree with symbols at the leaves but no values at the interior nodes. Its variants should be named Leaf and Node. Every node has exactly two children. Include a define-type and at least three examples of the data.

2.6 Mirror

Using the data definition you developed in the last problem, develop the mirror function that accepts a binary tree and produces a new binary tree that is the left-right mirror image of the first one. That is, if the input tree has the symbol 'horse at the far left of the tree, the new one should have the symbol 'horse at the far right of the tree.

2.7 Substitution

Using the same data definition, develop the subst function that accepts a BTree and a symbol and a replacement BTree and returns a new tree where every instance of the symbol is replaced by the replacement tree. (Make sure to use this ordering for the arguments, or my tests will all fail....)

2.8 Occurrences

Using the same data definition, develop the occurrences function that accepts a binary tree and a symbol and returns a number indicating how many leaves in the tree have the given symbol as their value.