cs1321 introduction to programming
Download
Skip this Video
Download Presentation
CS1321: Introduction to Programming

Loading in 2 Seconds...

play fullscreen
1 / 15

CS1321: - PowerPoint PPT Presentation


  • 242 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CS1321:' - Pat_Xavi


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

CS1321:Introduction to Programming

Georgia Institute of Technology

College of Computing

Module 14

Mutually Referential Data Definitions

the family tree take two
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
The person

What are some properties of a person?

1) name

2) date of birth

3) eye color

4) children

slide4
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
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?

slide6

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

slide12

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

slide13

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?

slide14

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

ad