130 likes | 284 Views
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
E N D
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 • Heredity: traits • Simplifying with Let
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
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 (…) (…))…..)
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?
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))..
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
Blue-Eyed Ancestor • Contract:b-e-a: family-tree ->symbol • Purpose: Find a blue-eyed ancestor
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))))))
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)))
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))))))))
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))))))
Next Time • Data structures and analysis • Sets