;1. ; contract, purpose ;;size-all: FamTree --> num ;;purpose: to count all nodes (children and unknowns) in a family tree ; data definition ;;a FamTree is ;;- unknown, or ;;-(make-child symbol number FamTree FamTree), ;; where : ;; the symbol represents a name, ;; the number is the birthyear, ;; the first FamTree is the mother's family tree, ;; the second FamTree is the father's side. ;; Unknown is an empty FamTree (define-struct unknown (name)) ; example (define UNKNOWN (make-unknown '?)) ; Template #| (define (FamTree-function tree) (cond [(unknown? tree) ...] [(child? tree) (...(child-name tree)... ...(FamTree-function (child-ma tree))... ...(FamilyTree-function (child-pa tree))... ...(child-birthyear tree)...)])) |# (define-struct child (name birthyear ma pa)) (define (size-all tree) (cond [(unknown? tree) 1] [(child? tree) (+ 1 (size-all (child-ma tree)) (size-all (child-pa tree)))] )) ; examples (define nick (make-child 1892 'bob UNKNOWN UNKNOWN)) (define keesha (make-child 1899 'keesha UNKNOWN UNKNOWN)) (define steve (make-child 'steve 1922 keesha nick)) (define ian (make-child 'ian 1944 UNKNOWN steve)) ; test cases (= (size-all UNKNOWN) 1) (= (size-all nick) 3) (= (size-all ian) 9) ;2a. ;; data definition ;; A list of classes is either ;; - empty ;; - cons(f, r) ;; where: ;; f is a class ;; r is a list of classes ;; data definition ;; a class is either ;; - empty ;; - cons(f, r) ;; where: ;; f is a symbol ;; r is a list of symbols ; examples (define school1 (list (list 'jack 'paul 'jason) (list 'rob 'jamil))) (define school2 (list (list 'cheryl 'tom 'todd 'matt 'mike ) (list 'matt 'mark ))) (define school3 (list (list 'mike 'mike 'marla 'omar 'jamila) (list 'ferron 'mike 'john 'mike) (list 'mike))) ; Template - list of classes #| (define (f-loc loc) (cond [(empty? loc) ...] [(cons? loc) .... ... (f-a-class ( first loc)) ... (f-loc (rest loc))])) (define (f-a-class los) (cond [(empty? los)... ] [(cons? los).... ... (first los) ... (f-a-class (rest los))])) |# ; 2b. ;; inSchool?: symbol, list of classes -> boolean ;; returns true if input name appears in the school atleast once and ;; false otherwise (define (inSchool? name loc) (cond [(empty? loc) false] [(cons? loc) (cond [(inClass? name (first loc)) true] [else (inSchool? name (rest loc))])])) ;; inClass?: symbol, los -> boolean ;; return true if a given name appeared in the list (define (inClass? aName aClass) (cond [(empty? aClass) false ] [(cons? aClass) (cond [(symbol=? aName (first aClass)) true] [else (inClass? aName (rest aClass))])])) ; test cases (boolean=? (inSchool? 'rob empty) false) (boolean=? (inSchool? 'jamil school1) true) (boolean=? (inSchool? 'claire school1) false) (boolean=? (inSchool? 'matt school2) true) (boolean=? (inSchool? 'matthew school2) false) ; 2c. ;; sumSchool: symbol, list of classes -> num ;; returns the total number of times the input name appear ;; in the school (define (sumSchool aName loc) (cond [(empty? loc) 0] [(cons? loc) (+ (sumClass aName (first loc)) (sumSchool aName (rest loc)))])) ;; sumClass: symbol, los -> num ;; return how many names in the class were some given name (define (sumClass aName aClass) (cond [(empty? aClass) 0] [(cons? aClass) (+ (cond [(symbol=? aName (first aClass)) 1] [else 0]) (sumClass aName (rest aClass)))])) ; test cases (= (sumSchool 'kobe empty) 0) (= (sumSchool 'matt school2) 2) (= (sumSchool 'cheryl school1) 0) (= (sumSchool 'mike school3) 5) (= (sumSchool 'jamil school3) 0)