1 / 15

CS1321: Introduction to Programming

CS1321: Introduction to Programming. Georgia Institute of Technology College of Computing Module 14 Mutually Referential Data Definitions. The Family Tree, take two…. The ancestral tree was not exactly a “family tree” as we normally envision.

Pat_Xavi
Download Presentation

CS1321: Introduction to Programming

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. CS1321:Introduction to Programming Georgia Institute of Technology College of Computing Module 14 Mutually Referential Data Definitions

  2. The Family Tree, take two… The ancestral tree was not exactly a “family tree” as we normally envision. How can we make a structure to store a family tree?

  3. The person What are some properties of a person? 1) name 2) date of birth 3) eye color 4) children

  4. Nope… Is there a limit we can assume on the number of children? No. It varies from no children to some unknown number of children. We cannot predict. Our binary tree model no longer works! So how can we fix our definition?

  5. The new data definition… (define-struct person (name bday eye-color children)) ;; A person is a structure: ;; (make-person name bday eye-color children) ;; Where name & eye-color are symbols, bday is a ;; number, and children is a list-of-children What’s a list-of-children?

  6. ;; a list-of-children is either: ;; 1) the empty list, empty, or ;; 2) (cons p loc) where p is a person and ;; children is a list-of-children

  7. So all together… (define-struct person (name bday eye-color children)) ;; A person is a structure: ;; (make-person name bday eye-color children) ;; Where name & eye-color are symbols, bday is a ;; number, and children is a list-of-children ;; a list-of-children is either: ;; 1) the empty list, empty, or ;; 2) (cons p children) where p is a person ;; and children is a list-of-children

  8. So all together… (define-struct person (name bday eye-color children)) ;; A person is a structure: ;; (make-person name bday eye-color children) ;; Where name & eye-color are symbols, bday is a ;; number, and children is a list-of-children ;; a list-of-children is either: ;; 1) the empty list, empty, or ;; 2) (cons p children) where p is a person, and ;; children is a list-of-children

  9. What we created… Are mutually referential data definitions. One data definition refers to the other data definition. We have two templates. One for each data structure.

  10. Template 1: person ;; Data Definition and Analysis:(define-struct person (name bday eye-color children)) ;; A person is a structure:;; (make-person name bday eye-color children);; Where name & eye-color are symbols, bday is a;; number, and children is a list-of-children ;; Template: ;; (define (process-person somebody) ;; …(person-name somebody)… ;; …(person-bday somebody)… ;; …(person-eye-color somebody)… ;; …(person-children somebody)…)

  11. Template 2: list-of-children ;; Data Definition and Analysis;; a list-of-children is either: ;; 1) the empty list, empty, or ;; 2) (cons p children) where p is a person and ;; children is a list-of-children ;; Template:;; (define (process-children children) ;; (cond [(empty? children) …] ;; [else …(first children)… ;; …(process-children (rest children))…]))

  12. Let’s look at the two templates side by side now: ;; Template: ;; (define (process-person somebody) ;; …(person-name somebody)… ;; …(person-bday somebody)… ;; …(person-eye-color somebody)… ;; …(person-children somebody)…) ;; (define (process-children children) ;; (cond [(empty? children) …] ;; [else …(first children)… ;; …(process-children (rest children))…]))

  13. Let’s look at the two templates side by side now: ;; Template: ;; (define (process-person somebody) ;; …(person-name somebody)… ;; …(person-bday somebody)… ;; …(person-eye-color somebody)… ;; …(person-children somebody)…) ;; (define (process-children children) ;; (cond [(empty? children) …] ;; [else …(first children)… ;; …(process-children (rest children))…])) Why don’t we call each template function from within the other?

  14. Let’s look at the two templates side by side now: ;; Template: ;; (define (process-person somebody) ;; …(person-name somebody)… ;; …(person-bday somebody)… ;; …(person-eye-color somebody)… ;; …(process-children (person-children somebody)…) ;; (define (process-children children) ;; (cond [(empty? children) …] ;; [else …(process-person (first children)… ;; …(process-children (rest children))…])) Why don’t we call each template function from within the other?

  15. Once again… The template reflects the data definition, and it describes the generic case. The template is generic – show the recursive calls! If your function doesn’t really need them, then omit them in your actual function definition. It’s up to you to build your function definition using the template as a guide. The template tries to show all the typically processing that will occur based on the data definitions alone.

More Related