EECS 321 Programming Languages: Homework 1

Due: Friday, January 11th, 2013, noon

Part 0 – Set up DrRacket & Create Handin Account

Be sure you have version 5.3.1 of Racket installed. (This is the latest version from the Racket web page.) It is installed on the tlab machines in /usr/local/bin/drracket, or you can use your own machine. Install it via

Open DrRacket, set the language to “Use the language declared in the source” (via the Language|Choose Language menu item), put the following program into the upper window (replacing whatever is there), and click “Run”

#lang plai
(expt 2 100)
You should see the prompt in the interactions window (a > character) and the number 1267650600228229401496703205376.

Install nwu-eecs-pl.plt by using DrRacket’s “File|Install .plt file...” menu item. In the dialog box, make sure the “Web” option at the top is selected and paste this url into the box:
Wait for the close button to become enabled (and then click it). Restart DrRacket.

After restarting DrRacket, select the Manage EECS 321 PL Handin Account... menu item from DrRacket's File menu. Change to the New User panel, and pick a username and password. (Use your real name and real NetID, so that we can connect your homework submissions with you. DON'T use your real password.)

You will have to install the .plt file in DrRacket for each different filesystem that you want to have Handin button. However, after creating a handin account once from any machine, you can use DrRacket's Handin button on any other machine.

There is a quick reference to things Rackety available from the course webpage. You may wish to have a look if you're not familiar with Racket.

Part 1 – Honor Code

Every homework assignment you hand in must begin with the following strings (taken from the Provost's website see that for a more detailed explanation of these points):

  "Know your rights."
  "Acknowledge your sources."
  "Protect your work."
  "Avoid suspicion."
  "Do your own work."
  "Never falsify a record or permit another person to do so."
  "Never fabricate data, citations, or experimental results."
  "Always tell the truth when discussing your work with your instructor."

Just put those strings immediately following the #lang plai line in your file.

If they are not present, you receive no credit for the assignment.

Part 2 – Trees

The following Tree datatype implements binary trees with numbers in each node and leaf:

  (define-type Tree
    [leaf (val number?)]
    [node (val number?)
          (left Tree?)
          (right Tree?)])

Every problem must come with an appropriate set of test cases. At a minimum the test cases must cover every branch in each function you write.

Implement a sum function that takes a tree and returns the sum of the numbers in the tree.

Example: (sum (node 5 (leaf 6) (leaf 7))) should produce 18.

Part 3 – Negate

Impement the function negate, which takes a tree and returns a tree that has the same shape, but with all the numbers negated.

Example: (negate (node 5 (leaf 6) (leaf 7))) should produce (node -5 (leaf -6) (leaf -7)).

Part 4 – Contains?

Impement the function contains?, which takes a tree and a number and returns #t if the number is in the tree, #f otherwise.

Example: (contains? (node 5 (leaf 6) (leaf 7)) 6) should produce #t.

The second argument to the contains? function is “along for the ride”.

Part 5 – Big Leaves?

Impement the function big-leaves?, which takes a tree and returns #t if every leaf is bigger than (and not equal to) the sum of numbers in the path of nodes from the root that reaches the leaf.

Examples: (big-leaves? (node 5 (leaf 6) (leaf 7))) should produce #t;
(big-leaves? (node 5 (node 2 (leaf 8) (leaf 6)) (leaf 7))) should produce #f (since 6 is smaller than 5 plus 2),
(big-leaves? (node 2 (leaf 2) (leaf 2))) should produce #f,
(big-leaves? (leaf 0)) should produce #f, and
(big-leaves? (leaf 1)) should produce #t, since the sum of no leaves is 0.

Part 6 – Sorted?

Impement the function sorted?, which takes a tree and determines whether it is sorted in the sense that the numbers increase (or stay the same) in a inorder travsersal of the tree.

Your function should run in time proportional to the size of the tree, which rules out making a list of the tree numbers using append on recursive calls. Instead, you must accumulate some information as the function traverses the tree.

Part 7 – is-bst?

Impement the function is-bst?, which takes a tree and returns a boolean that indicates whether it is a binary search tree or not.

Like the previous exercise, your function should run in time proportional to the size of the tree.

Part 8 – Handin

Click the handin button in DrRacket to hand in your submission before the deadline. Late homework will not be accepted.

Last update: Thursday, January 10th, 2013