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.
<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).
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)
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)
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.
sum-tree : <num-tree> -> <num>, adds all of the numbers in a tree
(sum-tree '((1 . 7) . 3)) = 11
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
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)
Put these functions at the end of hw4.scm, too.
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:
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, 2002mflatt@cs.utah.edu |