CS1321: Introduction to Programming - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

CS1321: Introduction to Programming

Description:

(define-struct person (name bday eye-color children)) ;; A person is a structure: ... (person-bday somebody)... ;; ...(person-eye-color somebody) ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 16
Provided by: Mon685
Category:

less

Transcript and Presenter's Notes

Title: CS1321: Introduction to Programming


1
CS1321Introduction 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-p
erson name bday eye-color children) Where name
eye-color are symbols, bday is a number, and
children is a list-of-children
Whats 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-pe
rson 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-pe
rson 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
Lets 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
Lets 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 dont we call each template function from
within the other?
14
Lets 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 dont 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 doesnt really need them, then omit them
in your actual function definition. Its 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.
Write a Comment
User Comments (0)
About PowerShow.com