Cs1321 introduction to programming l.jpg
Sponsored Links
This presentation is the property of its rightful owner.
1 / 15

CS1321: Introduction to Programming PowerPoint PPT Presentation


  • 220 Views
  • Uploaded on
  • Presentation posted in: Internet / Web

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.

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


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.

How can we make a structure to store a family tree?


The person

What are some properties of a person?

1) name

2) date of birth

3) eye color

4) children


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?


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?


;; 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


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


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


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.


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)…)


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))…]))


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))…]))


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?


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?


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.


  • Login