#| We will define the following Family Tree functions: make-child, child-name, child-ma, child-pa, child-birthyear, child?, unknown?, and FamTree? |# ;;remember, this structure definition will define the following functions: ;; make-child, child-ma, child-pa, child-birthyear, child? (define-struct child (name ma pa birthyear)) ;;and this defines make-unknown and unknown? (define-struct unknown ()) (define (FamTree? exp) (or (unkown? exp) (child? exp))) (define the-simpsons (make-child 'bart (make-child 'marge (make-child 'jackie (make-unknown) (make-unknown) 1926) (make-unknown) 1958) (make-child 'homer (make-child 'abraham (make-unknown) (make-unknown) 1903) (make-child 'mona (make-unknown) (make-unknown) 1929) 1959) 1983)) ;; size: FamTree --> number ;; Return how many (non-unknown) children ft contains. ;; (define (size ft) (if (unknown? ft) 0 (+ 1 (size (child-ma ft)) (size (child-pa ft))))) ;; related-to-abraham? : FamTree --> boolean ;; Is 'abraham the name of anybody in the FamTree "ft"? ;; (define (related-to-abraham? ft) (if (unknown? ft) false (or (symbol=? (child-name ft) 'abraham) (related-to-abraham? (child-ma ft)) (related-to-abraham? (child-pa ft))))) ;; (height ft) : FamTree --> boolean ;; Return the height of ft. ;; (define (height ft) (if (unknown? ft) 0 (max (add1 (height (child-ma ft))) (add1 (height (child-pa ft)))))) ;; earliest-birthyear: FamTree --> number ;; Return the earliest birthyear of any child in ft. ;; (define (earliest-birthyear ft) (if (unknown? ft) 2003 ;; let's assume no-one was born before next year ;) (min (earliest-birthyear (child-ma ft)) (earliest-birthyear (child-pa ft)) (child-birthyear ft)))) ;; oldest-age : FamTree number --> number ;; Return the age of the oldest person in ft, ;; given the current year is this-year. ;; (We want to know what anniversary of the earliest birth we have. ;; Note that our choice of representing data doesn't let us know ;; whether or not somebody is still alive!) ;; (define (oldest-age ft this-year) (- this-year (earliest-birthyear ft))) ;; ivy : number --> famTree ;; Return a tree with height "ht", ;; but it's a stringy tree, with as few nodes as possible. ;; (define (ivy ht) (if (= 0 ht) (make-unknown) (make-child 'arch-stanton (ivy (sub1 ht)) (make-unknown) 1820))) ;; shrubbery : number --> famTree ;; Return a tree with height "ht", ;; but it's a bushy tree, with as many nodes as possible. ;; (define (shrubbery ht) (if (= 0 ht) (make-unknown) (make-child 'monty-python (shrubbery (sub1 ht)) (shrubbery (sub1 ht)) 1960)))