Assignment 6
Due Date : 11/6 @ 11:59pm
Note
For each function that you design you are expected to follow the Design Recipe in the same way as we did in class. Failure to show all your work for each step will cost you points.
Problem 1
Here is a data definition:
;; A family-tree-node is one of: ;; - empty ;; - (make-child father mother name date hair-color) ;; where father and mother are family-tree-nodes, ;; name and hair-color are symbols, and date is a number.Use the above data definition to solve the following problems:
- Develop the function
count-olderthat consumes a family-tree-node and a year and returns the number of people in the tree that were born that year or earlier. - Develop the function
search-tree-olderthat consumes a family-tree-node and a number and produces a list of all of the people in the tree born in that year or earlier. - Develop the function
red-haired-ancestors?that determines whether a family-tree-node contains an ancestor with red hair on the father's side and an ancestor with red hair on the mother's side. - Develop the function
update-fatherthat consumes two family-tree-nodes and produces a family-tree-node that updates the father of the first tree to be the second tree.
Problem 2
- Design the function
swapthat consumes a list of symbolslosand two symbolss1ands2. The function returns a the listlstwith all occurrences ofs1replaced withs2and all occurrences ofs2replaced withs1, e.g.,(swap 'a 'd (list a b c d))returns(list d b c a) - Design the function
product, that consumessos1andsos2each a list of symbols without repetitions, returns a list of 2-lists that represents the Cartesian product ofsos1andsos2. The 2-lists may appear in any order.> (product (list a b c) (list x y)) (list (list a x) (list a y) (list b x) (list b y) (list c x) (list c y)) -
Design the function
flattenthat consumes a list of lists of numbersLof[Lof[Number]]and returns a single list that contains all the numbers (put differently remove the inner lists).> (flatten (list (list 1 2) (list 4 5) (list 10 19))) (list 1 2 4 5 10 19)
Problem 3
Given the following data definition
;; An ATOM is one of: ;; -- Symbol ;; -- String ;; -- Number ;; --------------------------------------------------------- ;; An SEXP (S-expression) is one of: ;; -- empty ;; -- (cons ATOM SEXP) ;; -- (cons SEXP SEXP)Design the following functions
-
count-symbolsconsumes anSEXPand returns the total number of elements inSEXPthat are Symbols. -
sum-numbersconsumes anSEXPand returns the summation of all elements inSEXPthat are Numbers. -
replaceconsumes anSEXPand twoATOMsa1anda2and replaces all occurrences ofa1inSEXPwitha2. For example,> (replace (list (list 'a 'b) (list 1 3 )) 'a 'b) (list (list 'b 'b) (list 1 3)) > (replace (list (list 'a 'b) (list 1 3)) 1 2) (list (list 'a 'b) (list 2 3))
Our are not allowed to use theeq?build in function.