1 / 13

Bringing it all Together: Family Trees

Bringing it all Together: Family Trees. CMSC 11500 Introduction to Computer Programming October 18, 2002. Roadmap. An integrated example: Family trees Building the data definition Structures of structures Using the data From data definition to template Traversing family trees

arlais
Download Presentation

Bringing it all Together: Family Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Bringing it all Together:Family Trees CMSC 11500 Introduction to Computer Programming October 18, 2002

  2. Roadmap • An integrated example: Family trees • Building the data definition • Structures of structures • Using the data • From data definition to template • Traversing family trees • Heredity: traits • Simplifying with Let

  3. Family Trees • Each child has: mother, father, & some attributes - e.g. birthdate, hair color, eye color Granny: Eyes: Brown Grandpa: Eyes: Blue Mom: Eyes: Green Dad: Eyes: Brown Jane: Eyes: Hazel John: Eyes: Brown

  4. Data Definition • A family-tree is • ‘unknown, or • (make-ft name eye-color mother father) • where name, eye-color are symbols; mother,father are family-tree • (define-struct ft (name eye-color mother father) • (make-ft ‘Granny ‘Blue ‘unknown ‘unknown) • (make-ft ‘John ‘Brown • (make-ft ‘Mom ‘Green (…) (…))…..)

  5. Defining Functions:Data definition to Template Data definition: A family-tree is: - unknown, or - (make-ft name eye-color mother father) Questions: How many kinds of family-tree? # pieces in 1st part? # pieces in 2nd part? Self-references? - Do What?

  6. Defining Functions:Data definition to Template Data definition: A family-tree is: - unknown, or - (make-ft name eye-color mother father) Template: (define (fn-for-f-tree aft) (cond ((eq? ‘unknown aft) …) ((ft? aft) … (ft-name aft)… … (ft-eye-color aft)… … (fn-for-f-tree (ft-mother aft)). … (fn-for-f-tree (ft-father aft))..

  7. Things to do with Family Trees • Find ancestor(s) with certain traits • e.g. eye-color, born before some date • Find all traits for all relatives • Compute relations between individuals • Determine if they are related by blood

  8. Blue-Eyed Ancestor • Contract:b-e-a: family-tree ->symbol • Purpose: Find a blue-eyed ancestor

  9. Blue-eyed-ancestor (define (b-e-a aft) (cond ((eq? ‘unknown aft) #f) ((ft? aft) (if (eq? (ft-eye-color aft) ‘blue) (ft-name aft) (let ((b-e-m (b-e-a (ft-mother aft))) (b-e-f (b-e-a (ft-father aft)))) (cond ((symbol? b-e-m) b-e-m) ((symbol? b-e-f) b-e-f) (else #f))))))

  10. All-blue-eyed-ancestors • Get all ancestors (not just first) • Contract: a-b-e-a: family-tree -> (listof symbol) • Purpose: Find all blue-eyed-ancestors • Helper: “append” • (append ‘(a b c) ‘(a b c)) -> ‘(a b c) • (define (append list1 list2) • (cond ((null? list1) list2) • (else (cons (car list1) (append (cdr list1) list2)))

  11. All-blue-eyed-ancestors (define (a-b-e-a aft) (cond ((eq? ‘unknown aft) ‘()) ((ft? aft) (if (eq? (ft-eye-color aft) ‘blue) (cons (ft-name aft) (append (a-b-e-a (ft-mother aft)) (a-b-e-a (ft-father aft)))) (append (a-b-e-a (ft-mother aft)) (a-b-e-a (ft-father aft))))))))

  12. All-blue-eyed-ancestors (define (a-b-e-a aft) (cond ((eq? ‘unknown aft) ‘()) (else (let ((in-parents (append (a-b-e-a (ft-mother aft)) (a-b-e-a (ft-father aft))))) (if (eq? (ft-eye-color aft) ‘blue) (cons (ft-name aft) in-parents) in-parents))))))

  13. Next Time • Data structures and analysis • Sets

More Related