Developing Programs for Family Trees c. Kathi Fisler, 2001.

Slides:



Advertisements
Similar presentations
CSC 270 Nov. 22, 2005 Last Day of Scheme Dr. Stephen Bloch
Advertisements

Higher-Order Functions and Loops c. Kathi Fisler,
Recursion (define tell-story (lambda () (print ‘’Once upon a time there was a mountain. ‘’) (print ‘’On the mountain, there was a temple. ‘’) (print ‘’In.
Clojure Template Tail Recursion. Hello, Factorial! The factorial function is everybody’s introduction to recursion (defn factorial-1 [n] (if (zero? n)
Recursive Data 2 Mutually Recursive Data Definitions (HTDP sec 15.1)
Microsoft ® Office Word 2007 Training Bullets, Numbers, and Lists ICT Staff Development presents:
Multi-way Trees CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.6 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA 1 ©
Expert System Human expert level performance Limited application area Large component of task specific knowledge Knowledge based system Task specific knowledge.
1 Introduction to Computability Theory Lecture15: Reductions Prof. Amos Israeli.
Developing Programs for Family Trees c. Kathi Fisler, 2001.
Loops in Scheme, II c. Kathi Fisler, 2001 (early slides assume map/filter)
16-Jun-15 Recognizers. 2 Parsers and recognizers Given a grammar (say, in BNF) and a string, A recognizer will tell whether the string belongs to the.
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
The Design Process/Recipe An example over list of circles c. Kathi Fisler, 2001.
This material in not in your text (except as exercises) Sequence Comparisons –Problems in molecular biology involve finding the minimum number of edit.
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
28-Jun-15 Recognizers. 2 Parsers and recognizers Given a grammar (say, in BNF) and a string, A recognizer will tell whether the string belongs to the.
29-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Introduction to a Programming Environment
Case Study: Free Variables CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.5 TexPoint fonts used in EMF. Read the TexPoint manual.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Induction and recursion
From Templates to Folds CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
TeachScheme, ReachJava Adelphi University Tuesday afternoon June 23, 2009.
Foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA © Mitchell.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
Induction and recursion
Scheme: Lists Cont’d Chapter 9-10 of HTDP Ms. Knudtzon September 26.
CS CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 11 Sept 27th, 2001 Fall Semester.
Logic Programming Module 2AIT202 Website Lecturer: Dave Sharp Room: AG15
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 8: Accumulating.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
TeachScheme, ReachJava Adelphi University Thursday morning July 15, 2010.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
More examples of invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative.
GRAMMARS & PARSING Lecture 8 CS2110 – Spring If you are going to form a group for A2, please do it before tomorrow (Friday) noon.
The 8-queens problem CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Cs1321 December 6, 2001 Review. What is computer science? What's an algorithm? Processes and programs Overview of some programming language concepts Functional.
Lecture on Set! And Local CS 2135 Copyright Kathi Fisler, 2002 This material requires Advanced Language Level.
CSE 311 Foundations of Computing I Lecture 28 Computability: Other Undecidable Problems Autumn 2011 CSE 3111.
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
CS CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 13 October 4, 2001 Fall Semester.
Sometimes Structural Recursion Isn't Enough CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.1 TexPoint fonts used in EMF. Read the TexPoint manual.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
The Law of Averages. What does the law of average say? We know that, from the definition of probability, in the long run the frequency of some event will.
Comparative Relational Thinking
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
Functional Programming Language 1 Scheme Language: part 3.
Lets and Loops Tail Recursion.
Generalizing Similar Functions
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.2
Class 11: Two-argument recursion
Halting Functions for Tree-Like Structures
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3
From Templates to Folds
Recursion 12-Nov-18.
The Metacircular Evaluator
Recursion 2-Dec-18.
(early slides assume map/filter)
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.5
Recursion 23-Apr-19.
Developing Programs for Family Trees
Generalizing Similar Functions
Rewriting your function using map and foldr
Bringing it all Together: Family Trees
Presentation transcript:

Developing Programs for Family Trees c. Kathi Fisler, 2001

Susan PatMike Tom AnnJoe Mary Consider the following family tree: Assuming we want to write programs to query who is in the tree or to count how many generations are in the tree, what data model could we use?

Susan PatMike Tom AnnJoe Mary Consider the following family tree: Names Info about people (name, father, mother) Need to represent: (Will ignore other info, like birthday, for now)

Susan PatMike Tom AnnJoe Mary Consider the following family tree: Names Info about people (name, father, mother) Need to represent: (use symbols) (use structures)

Data Model for Family Trees (define-struct person (name father mother)) A family tree (ftree) is (make-person symbol ftree ftree) How about: [Try making a family tree with this definition]

Making Family Trees (define-struct person (name father mother)) A family tree (ftree) is (make-person symbol ftree ftree) (make-person ‘Mary (make-person ‘Joe ) (make-person ‘Ann (make-person ‘Tom …) (make-person ‘Susan …))) What goes here?

Making Family Trees (define-struct person (name father mother)) A family tree (ftree) is (make-person symbol ftree ftree) (make-person ‘Mary (make-person ‘Joe ) (make-person ‘Ann (make-person ‘Tom …) (make-person ‘Susan …))) What goes here? Follow the data definition! Must use a make-person

A Broken Family Tree Data Model Definition requires each parent to be a whole person, with a father and mother (who are also make-persons …) The definition doesn’t allow finite trees! General rule: every recursive data definition needs at least one non-recursive case (ie, a case with no arrows or with a finite chain of arrows)

A family tree (ftree) is either –‘unknown –(make-person symbol ftree ftree) A New Data Model for Family Trees (define-struct person (name father mother)) [Try making a family tree with this definition] [notice this case has no arrows]

Sample Family Trees ‘Hallie (make-person ‘Mary ‘unknown (make-person ‘Ann ‘unknown ‘unknown)) (make-person ‘Bernie (make-person ‘Fred (make-person ‘Bubba ‘unknown ‘unknown) ‘unknown)) (make-person ‘Lisa ‘unknown ‘unknown))

Programs on Family Trees ;; ftree-func : ftree  ??? (define (ftree-func aftree) …) How much of this program can you write based on the data definition? [Try it] Suppose we want to write a program on family trees, but I don’t tell you which one …

Template on Family Trees ;; ftree-func : ftree  ??? (define (ftree-func aftree) …) What kind of data definition does ftree have?

Template on Family Trees ;; ftree-func : ftree  ??? (define (ftree-func aftree) (cond [… …] [… …])) What kind of data definition does ftree have? one based on (two) cases …

Template on Family Trees ;; ftree-func : ftree  ??? (define (ftree-func aftree) (cond [… …] [… …])) What questions differentiate the cases?

Template on Family Trees ;; ftree-func : ftree  ??? (define (ftree-func aftree) (cond [(symbol? aftree) …] [(person? aftree) …])) What questions differentiate the cases?

Template on Family Trees ;; ftree-func : ftree  ??? (define (ftree-func aftree) (cond [(symbol? aftree) …] [(person? aftree) …])) What other information is available in each case?

Template on Family Trees ;; ftree-func : ftree  ??? (define (ftree-func aftree) (cond [(symbol? aftree) …] [(person? aftree) …])) What other information is available in each case? none in the symbol? case

Template on Family Trees ;; ftree-func : ftree  ??? (define (ftree-func aftree) (cond [(symbol? aftree) …] [(person? aftree) (person-name aftree) … (person-father aftree) … (person-mother aftree) …])) What other information is available in each case? selectors in the person? case

Template on Family Trees ;; ftree-func : ftree  ??? (define (ftree-func aftree) (cond [(symbol? aftree) …] [(person? aftree) (person-name aftree) … (person-father aftree) … (person-mother aftree) …])) What about the arrows in the data definition?

Template on Family Trees ;; ftree-func : ftree  ??? (define (ftree-func aftree) (cond [(symbol? aftree) …] [(person? aftree) (person-name aftree) … (ftree-func (person-father aftree))… (ftree-func (person-mother aftree)) …])) What about the arrows in the data definition? add recursive calls

Template on Family Trees ;; ftree-func : ftree  ??? (define (ftree-func aftree) (cond [(symbol? aftree) …] [(person? aftree) (person-name aftree) … (ftree-func (person-father aftree))… (ftree-func (person-mother aftree)) …])) This is the full template for programs over family trees

Practice Problems on Family Trees ;; count-generations : ftree  number ;; return the number of generations in a tree ;; in-family? : ftree name  boolean ;; determines whether name appears in tree [try each in turn]

count-generations : Solution ;; count-gen : ftree  number ;; counts generations in a family tree (define (count-gen aftree) (cond [(symbol? aftree) 0] [(person? aftree) (+ 1 (max (count-gen (person-father aftree)) (count-gen (person-mother aftree))))])) [the blue text is what we added to the template]

in-family? : Solution ;; in-family? : ftree name  boolean ;; determines whether name appears in tree (define (in-family? aftree aname) (cond [(symbol? aftree) false] [(person? aftree) (or (symbol=? (person-name aftree) aname) (in-family? (person-father aftree) aname) (in-family? (person-mother aftree) aname))]))

Augmenting the Model Programmers often augment their initial data models as the problem evolves. We want to augment our family tree model with information on birth-year and eye-color How would you change the model?

Revised Model, Version 1 (define-struct person (name year eye father mother)) A family tree (ftree) is either –‘unknown –(make-person name number symbol ftree ftree)

Revised Model, Version 2 (define-struct person (data father mother)) (define-struct info (name year eye)) An info is a (make-info name number symbol) A family tree (ftree) is either –‘unknown –(make-person info ftree ftree)

Which Model is Better? Model 1 is a little simpler, because it has fewer data definitions (and fewer arrows) Model 2 is more flexible, because we can add new info about a person without changing the data definition or template for people Model 2 is probably a better choice in the long run [develop a template for model 2]

Template on Revised Family Trees ;; ftree-func : ftree  ??? (define (ftree-func aftree) (cond [(symbol? aftree) … ] [(person? aftree) (info-func (person-data aftree)) … (ftree-func (person-father aftree))… (ftree-func (person-mother aftree)) …])) ;; info-func : info  ??? (define (info-func an-info) (info-name an-info) … (info-year an-info) … (info-eye an-info) ) Notice we now have two template functions, because we have two complex data definitions

Template on Revised Family Trees ;; ftree-func : ftree  ??? (define (ftree-func aftree) (cond [(symbol? aftree) … ] [(person? aftree) (info-func (person-data aftree)) … (ftree-func (person-father aftree))… (ftree-func (person-mother aftree)) …])) ;; info-func : info  ??? (define (info-func an-info) (info-name an-info) … (info-year an-info) … (info-eye an-info) ) Notice that the recursive calls match the arrows in the data definition! (3 arrows, 3 calls)

Templates become truly useful (even invaluable) when data definitions get long or refer to each other (ie, have many arrows crossing between them) We expect you to use them.

Practice Problems on Family Trees 2 ;; count-blue-eyed : ftree  number ;; return number of blue-eyed people in tree ;; has-old-and-blue? : ftree number  boolean ;; determines whether tree contains a blue-eyed ;; person born before given year ;; gather-green-eyed : ftree  list[name] ;; construct list of names of green-eyed people [try each in turn]

has-old-and-blue? : Solution ;; has-old-and-blue? : ftree num  boolean (define (has-old-and-blue? aftree yr) (cond [(symbol? aftree) false ] [(person? aftree) (or (old-and-blue? (person-data aftree) yr) (has-old-and-blue? (person-father aftree) yr) (has-old-and-blue? (person-mother aftree) yr)))])) ;; old-and-blue? : info number  boolean ;; true if person has blue eyes and was born before given year (define (old-and-blue? an-info born-before) (and (< (info-year an-info) born-before) (symbol=? ‘blue (info-eye an-info))))

Descendant Family Trees Current model is ancestor-based : each person refers to her parents. Hard to access information about someone’s children Let’s create a new model in which parents refer to their children instead of the other way around

A parent is a structure (make-parent symbol number symbol list-of- children) A list-of-children is either –empty, or –(cons parent list-of-children) Descendant Family Trees: Data Defn (define-struct parent (name year eye children)) [where do we need arrows?]

A parent is a structure (make-parent symbol number symbol list-of- children) A list-of-children is either –empty, or –(cons parent list-of-children) Descendant Family Trees: Data Defn (define-struct parent (name year eye children)) [try writing examples from this data defn]

(define Marypar (make-parent ‘Mary 1975 ‘blue empty)) (make-parent ‘Susan 1925 ‘green (cons (make-parent ‘Ann 1943 ‘blue (cons Marypar empty)) (cons (make-parent ‘Pat 1949 empty) empty))) Descendant Family Trees: Examples

A parent is a structure (make-parent symbol number symbol list-of- children) A list-of-children is either –empty, or –(cons parent list-of-children) Descendant Family Trees: Data Defn (define-struct parent (name year eye children)) [try writing the template for this data defn]

Descendant Family Trees : Template ;; pfunc : parent  ??? (define (p-func a-parent) (parent-name a-parent)... (parent-year a-parent)... (parent-eye-color a-parent)... (loc-func (parent-children a-parent))... ) ;; loc-func : list-of-children  ??? (define (loc-func a-loc) (cond [(empty? a-loc)...] [(cons? a-loc)... (p-func (first a-loc))... (loc-func (rest a-loc))... ])) Again, one call to a template function per arrow in the data defn

Practice on Desc. Family Trees ;; count-blue-eyed : parent  number ;; return number of blue-eyed desc from parent ;; has-old-and-blue? : ftree number  boolean ;; determines whether tree contains a blue-eyed ;; person born before given year [try each in turn]

count-blue-eyed : Solution 1 ;; count-blue-eyed : parent  number (define (count-blue-eyed a-parent) (cond [(symbol=? ‘blue (parent-eye-color a-parent)) (+ 1 (count-blue-kids (parent-children a- parent)))] [else (count-blue-kids (parent-children a- parent))])) ;; count-blue-kids : list-of-children  number (define (count-blue-kids a-loc) (cond [(empty? a-loc) 0] [(cons? a-loc) (+ (count-blue-eyed (first a-loc)) (count-blue-kids (rest a-loc)))]))

count-blue-eyed : Solution 2 ;; count-blue-eyed : parent  number (define (count-blue-eyed a-parent) (cond [(symbol=? ‘blue (parent-eye-color a-parent)) (+ 1 (count-blue-kids (parent-children a- parent)))] [else (count-blue-kids (parent-children a- parent))])) ;; count-blue-kids : list-of-children  number (define (count-blue-kids a-loc) (foldr 0 (lambda (kid result-rest) (+ (count-blue-eyed kid) (count-blue-kids (rest a-loc)))) a-loc))

Why Did We Create All These Variations on Models? This is how real world program development works. –You develop a simple model, –figure out how to solve your problem there, –then augment the model with new features –or redesign the model if necessary. Many programs on the simple models can be reused with just a little modification. And you get to think out your problem in stages, rather than all at once. We will be doing this model-refinement routine on languages later in the course.

Summary: where are we in the course? We are done learning Scheme. We have covered how to write programs using –conditionals –structures –lists –trees These, plus data definitions and templates, give you all of the Scheme programming tools that you need for the course. Next week, we start studying programming languages.