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

`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:

- Add a new primitive
`cons`to the language. You will have to modify the grammar and the`apply-prim`function. - 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`. - 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:

`cons(1,2)`=`'(1 . 2)``cons(cons(1,2),3)`=`'((1 . 2) . 3)``if cons(cons(1,2),3) then 1 else 4`=`1``if cons(-(1,1),0) then 1 else 4`=`4``+(cons(-(4,1),0),cons(5,6))`=`'(8 . 6)``+(cons(1,cons(9,0)),cons(5,cons(3,6)))`=`'(6 . (12 . 6))``sub1(+(cons(-(4,1),0),cons(5,6)))`=`'(7 . 5)``if -(cons(1,1),cons(1,1)) then 1 else 2`=`2``if -(cons(1,2),cons(1,1)) then 1 else 2`=`1`

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