CS 3520 Homework 4   - Due September 19

This homework is due September 19, 11:59 PM. Place all of the requested functions in a single file, hw4.scm, and hand it in with submit on the CS filesystem.

Important: Start with the code in hw4.scm.

Exercise 4.1, Abstraction 1

Here is a data definition for a tree of numbers:
<num-tree> ::= <num>
           ::= (cons <num-tree> <num-tree>)
Implement the following three functions by implementing one recursive helper function. Then implement the three functions using the helper function. The three functions must not be recursive (i.e., all needed recursion is in the helper).
  1. heavier-tree : <num-tree> -> <num-tree>, produces a tree like the given one, except that every number is 1 bigger

    (heavier-tree '((1 . 7) . 3)) = '((2 . 8) . 4)

  2. zero-tree : <num-tree> -> <num-tree>, produces a tree like the given one, except that every number is changed to 0

    (zero-tree '((1 . 7) . 3)) = '((0 . 0) . 0)

  3. grow-tree : <num-tree> -> <num-tree>, produces a tree like the given one, except that every number is replaced by a new node with two copies of the number

    (grow-tree '((1 . 7) . 3)) = '(((1 . 1) . (7 . 7)) . (3 . 3)) which is the same as '(((1 . 1) 7 . 7) 3 . 3)

Hint: a good name for your helper function would be map-tree.

Put your functions at the end of hw4.scm.

Exercise 4.2, Abstraction 2

Like the previous problem, but you will need a different helper function.
  1. sum-tree : <num-tree> -> <num>, adds all of the numbers in a tree

    (sum-tree '((1 . 7) . 3)) = 11

  2. max-tree : <num-tree> -> <num>, finds the maximum number in the tree, assuming that all numbers are non-negative

    (max-tree '((1 . 7) . 3)) = 7

  3. content-tree : <num-tree> -> <list-of-num>, extracts all of the numbers in a tree and puts them into a list; duplicates in the list are ok

    (content-tree '((1 . 7) . 3)) = '(1 7 3)

A good name for your helper function in this case would be fold-tree.

Put these functions at the end of hw4.scm, too.

Exercise 4.3, A Tree-Processing Language

The starter hw4.scm implements the language from lecture 5. In that language, the only values are numbers.

In this exercise, you will modify the language to that its only values are number trees (which includes plain numbers). You will generalize all of the operations on numbers so that they work on trees. For example, the add1 primitive will take a tree and add one to every number in the tree. Similarly, the + primitive will take two trees and add the numbers pairwise to produce a new tree.

Make these adjustments through the following steps:

  1. Add a new primitive cons to the language. You will have to modify the grammar and the apply-prim function.
  2. Modify the implementation of if so that it treats any tree containing all zeros as false, and treats all others trees as true. This modification is to eval-expression.
  3. Modify the implementation of all primitives except cons so that they operate on trees. This modification is again in apply-prim.

In the same way that the original interpreter assumes that + is used on two arguments, your revised interpreter can assume that + is used on two trees that have the same shape, etc.

Here are some example programs to try with your interpreter:


Last update: Wednesday, September 18th, 2002
mflatt@cs.utah.edu