PSICS Fall 2004 Quiz #7

Quiz #7 - 10/21/04
Due by Sunday 10/24 at 11PM (to WebCT)

Using local

Submit your solutions to this quiz by dropping in your webct drop box in the box labeled "Quiz7". Let Dave know if you have problems submitting!

Assignment

Function: create-family-tree

Recall the family tree we discussed based on a parent structure. Each parent structure contains a list of it's children (each of which is also a parent structure). We want to develop a function for building these structures given a list of parent-child relationships.

A single parent-child relationship is represented as a list containing two symbols, the first symbol is the parent name, the second symbol is the child name. Here is how we represent that Joe is the parent of Sam: '(Joe Sam).

For this exercise you should use the following definition for the structure parent (no need for eye color here):

(define-struct parent (name children))

You need to create a function named create-tree-for that will create a family tree in which a named parent is at the top of the tree (you create the family tree that includes all the children of the named parent, all the grandchildren, etc.). Here are some details:

;; make-tree-for consumes a symbol and a list and produces a parent structure
;;
;; (make-tree-for pname relist) creates a family tree (based on parent
;;     structures) at which the structure for pname is at the top.
;;  
;; for example, if joe has no children, the result of 
;;    (make-tree-for 'Joe ...) would be (make-parent 'Joe empty)
;;
;;  if Joe has children Sam and Jane, and Sam has a child named Betty, 
;;  the tree produced would look like this:
;;     (make-parent 'Joe
;;       (list 
;;          (make-parent 'Sam (list make-parent 'Betty empty))
;;          (make-parent 'Jane empty)))
;;

Below is some test code you can use to check your function:

;;
;; pclist is a list of parent-child relationships
;; 
(define pclist '( (Joe John)   (Sally John) (Joe Fred)
                  (Sally Fred) (Fred Jane)  (Fred Sam)
                  (Joe George) (Alice Joe)  (Elvis Alice)))
 
(create-tree-for 'Sam pclist)
(create-tree-for 'Sally pclist)
(create-tree-for 'Joe pclist)
(create-tree-for 'Elvis pclist)
                                 

Remember to use local (if and where it is appropriate)!


Extra Credit

Extra Credit: Create a function named create-family-trees that returns a list of family trees such as those produced by create-tree-for. The family trees produced must include a tree for each person that does not have a parent listed, and cannot have a tree for any person that does have a parent listed (so you can't simply use create-tree-for on each person). The trees must also represent all the relationships found in the list of parent-child relationships.

;; create-family-trees consumes a list and produces a list 
;;
;; (create-family-trees relist) creates a list of family trees
;;   (parent structures) that represents all the parent-child
;; relationships found in relist.
;;

Submitting

Submit your code as a single program (file) to the WebCT Drop Box labeled "Quiz 7". We should be able to easily find the required functions and to test them out (please give us some test code!).