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-older
that 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-older
that 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-father
that 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
swap
that consumes a list of symbolslos
and two symbolss1
ands2
. The function returns a the listlst
with all occurrences ofs1
replaced withs2
and all occurrences ofs2
replaced withs1
, e.g.,(swap 'a 'd (list a b c d))
returns(list d b c a)
- Design the function
product
, that consumessos1
andsos2
each a list of symbols without repetitions, returns a list of 2-lists that represents the Cartesian product ofsos1
andsos2
. 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
flatten
that 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-symbols
consumes anSEXP
and returns the total number of elements inSEXP
that are Symbols. -
sum-numbers
consumes anSEXP
and returns the summation of all elements inSEXP
that are Numbers. -
replace
consumes anSEXP
and twoATOM
sa1
anda2
and replaces all occurrences ofa1
inSEXP
witha2
. 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.