The Design Process/Recipe An example over list of circles 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,
Build a database I: Design tables for a new Access database
Templates CS 5010 Program Design Paradigms “Bootcamp” Lesson
Clojure Template Tail Recursion. Hello, Factorial! The factorial function is everybody’s introduction to recursion (defn factorial-1 [n] (if (zero? n)
Multi-way Trees CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.6 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Generics "It is easy to study the rules of overloading and of templates without noticing that together they are one of the keys to elegant and efficient.
Developing Programs for Family Trees c. Kathi Fisler, 2001.
Developing Programs for Family Trees c. Kathi Fisler, 2001.
Loops in Scheme, II c. Kathi Fisler, 2001 (early slides assume map/filter)
Object-Oriented Programming with Java Exceptions, Strings and Things Lecture 4.
Section 4.4: Designing Conditional Functions. REVIEW: Design Recipe – V. 2  Figure out precisely what you need to do. 1. Understand the problem 2. Function.
מבוא מורחב - שיעור 12 1 Lecture 12 Data directed programming Message passing dotted-tail notation & apply Section 2.4, pages ,2.5.2 pages.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7. Outline Symbols Data Directed Programming, Message Passing.
Trees CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.2 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
LIAL HORNSBY SCHNEIDER
Definition: A circle is the set of all points on a plane that is a fixed distance from the center.
Mutually-Recursive Data Definitions CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.4 © Mitchell Wand, This work is licensed under a Creative.
The Area and Componants of Circles. Free powerpoint template: 2 Circles Vocabulary pi or π (pi) Π is a mathematical constant with.
Circles: Area and Circumference. Definitions Circumference: Distance around the outside of a circle Area: How many squares it takes to cover a circle.
TeachScheme, ReachJava Adelphi University Tuesday afternoon June 23, 2009.
10.6 Equations of a Circle Standard Equation of a Circle Definition of a Circle.
CIRCLES Topic 7.3.
  investigate the relationship between the diameter and circumference of a circle.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
Greatest Common Factor and Least Common Multiples GCF and LCM
Arbitrarily Long Data Structures: Lists and Recursion CMSC Introduction to Computer Programming October 4, 2002.
Scheme: Lists Cont’d Chapter 9-10 of HTDP Ms. Knudtzon September 26.
Scheme: Compound Data Chapter 6 of HTDP Ms. Knudtzon September 19.
Examining Two Pieces of Data CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative.
Circumference and Area: Circles
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Recursion: Linear and Tree Recursive Processes and Iteration CMSC Introduction to Computer Programming October 7, 2002.
Circumference and area of circles Pick up your calculator for today’s notes.
1.8 Circles.
CS CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 7 Sept 13th, 2001 Fall Semester.
CSCI-100 Introduction to Computing
More examples of invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative.
TeachScheme, ReachJava Adelphi University Friday morning July 16, 2010.
Lists of Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Circumference Review. Review What is the relationship between a radius and a diameter? What does a circumference measure? What formulas do we use to calculate.
Functional Programming Language 1 Scheme Language: part 2.
Destructor Templates CS 5010 Program Design Paradigms “Bootcamp” Lesson
Lists vs. Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
Lecture on Set! And Local CS 2135 Copyright Kathi Fisler, 2002 This material requires Advanced Language Level.
Writing Equations by Completing the Square Or Using the Distance Formula.
Finding the area of circles
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
EQ: How do you write the equation of a circle? What does each piece in the equation represent? Demonstrated in writing in summary at conclusion of notes.
10-3 Circles Learning Target: I can use equations of circles to model and solve problems Goal 2.09.
2.6-2: Graphing Circles, Circles around Us. Recall… Standard Form: (x-h) 2 + (y-k) 2 = r 2 – Radius = r – Center (“Centre”) = (h, k)
CS CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 6 Sept 6th, 2001 Fall Semester.
The Circle. Examples (1) – (5) Determine the center and radius of the circle having the given equation. Identify four points of the circle.
Lab 11 Lab 1: CSG 711: Programming to Structure Karl Lieberherr.
Using the List Template CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Using the List Template CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.2 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Sometimes Structural Recursion Isn't Enough CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.1 TexPoint fonts used in EMF. Read the TexPoint manual.
Functions vs. Classes CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
 Enter blogger.com in the search barblogger.com  Log-in through a valid Gmail account (or create a Gmail account if you don’t have one)  Then click.
Functional Programming Language 1 Scheme Language: part 3.
Syntax and Semantics. Today's Lecture Compounds: Templates, Templates, … Syntax The acceptable form of what we write Mathematically, a set Mr. Backus.
Lets and Loops Tail Recursion.
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.2
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.2
(early slides assume map/filter)
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.5
Let's Play "What's the Question"
Developing Programs for Family Trees
Get binder Talk about binder grade.
Presentation transcript:

The Design Process/Recipe An example over list of circles c. Kathi Fisler, 2001

Create a data definition for a list of circles ;; A circle is a (make-circle posn number) (define-struct circle (center radius)) ;; A list-of-circles is either ;; - empty, or ;; - (cons circle list-of-circles) [Can you make examples of this data?]

Create a data definition for a list of circles (with examples) ;; A circle is a (make-circle posn number) (define-struct circle (center radius)) ;; A list-of-circles is either ;; - empty, or ;; - (cons circle list-of-circles) - empty - (cons (make-circle (make-posn 0 0) 15) (cons (make-circle (make-posn –5 3) 40) empty))

Create a template for list-of-circles ;; A circle is a (make-circle posn number) (define-struct circle (center radius)) ;; A list-of-circles is either ;; - empty, or ;; - (cons circle list-of-circles) Remember, a template captures the part of a program that we get “for free” from the structure of the data. A template addresses the input, not the output, of a function [Try writing the template]

Creating a template list-of-circles: steps ;; A circle is a (make-circle posn number) (define-struct circle (center radius)) ;; A list-of-circles is either ;; - empty, or ;; - (cons circle list-of-circles) (define (locirc-func alocirc) … ) First, give your template a name based on the name of the data

Creating a template list-of-circles: steps ;; A circle is a (make-circle posn number) (define-struct circle (center radius)) ;; A list-of-circles is either ;; - empty, or ;; - (cons circle list-of-circles) (define (locirc-func alocirc) … ) Next, exploit the cases in the data definition

Creating a template list-of-circles: steps ;; A circle is a (make-circle posn number) (define-struct circle (center radius)) ;; A list-of-circles is either ;; - empty, or ;; - (cons circle list-of-circles) (define (locirc-func alocirc) (cond [(empty? alocirc) …] [(cons? alocirc) …])) Next, exploit the cases in the data definition

Creating a template list-of-circles: steps ;; A circle is a (make-circle posn number) (define-struct circle (center radius)) ;; A list-of-circles is either ;; - empty, or ;; - (cons circle list-of-circles) (define (locirc-func alocirc) (cond [(empty? alocirc) …] [(cons? alocirc) …])) Next, extract the components of the data in each case

Creating a template list-of-circles: steps ;; A circle is a (make-circle posn number) (define-struct circle (center radius)) ;; A list-of-circles is either ;; - empty, or ;; - (cons circle list-of-circles) (define (locirc-func alocirc) (cond [(empty? alocirc) …] [(cons? alocirc) … (first alocirc) … (rest alocirc) … ])) Next, extract the components of the data in each case

Creating a template list-of-circles: steps ;; A circle is a (make-circle posn number) (define-struct circle (center radius)) ;; A list-of-circles is either ;; - empty, or ;; - (cons circle list-of-circles) (define (locirc-func alocirc) (cond [(empty? alocirc) …] [(cons? alocirc) … (first alocirc) … (rest alocirc) … ])) Finally, account for the arrows by calling functions for the target data definitions

Creating a template list-of-circles: steps ;; A circle is a (make-circle posn number) (define-struct circle (center radius)) ;; A list-of-circles is either ;; - empty, or ;; - (cons circle list-of-circles) (define (locirc-func alocirc) (cond [(empty? alocirc) …] [(cons? alocirc) … (circle-func (first alocirc)) … (locirc-func (rest alocirc)) … ])) Finally, account for the arrows by calling functions for the target data definitions

Creating a template list-of-circles: steps ;; A circle is a (make-circle posn number) (define-struct circle (center radius)) ;; A list-of-circles is either ;; - empty, or ;; - (cons circle list-of-circles) (define (locirc-func alocirc) (cond [(empty? alocirc) …] [(cons? alocirc) … (circle-func (first alocirc)) … (locirc-func (rest alocirc)) … ])) But where is circle-func? We need a template for it …

When you create a template, write one template function for each data definition

Creating a template list-of-circles: steps ;; A circle is a (make-circle posn number) (define-struct circle (center radius)) ;; A list-of-circles is either ;; - empty, or ;; - (cons circle list-of-circles) (define (locirc-func alocirc) (cond [(empty? alocirc) …] [(cons? alocirc) … (circle-func (first alocirc)) … (locirc-func (rest alocirc)) … ])) (define (circle-func a-circ) …) Add a template for circles

Creating a template list-of-circles: steps ;; A circle is a (make-circle posn number) (define-struct circle (center radius)) ;; A list-of-circles is either ;; - empty, or ;; - (cons circle list-of-circles) (define (locirc-func alocirc) (cond [(empty? alocirc) …] [(cons? alocirc) … (circle-func (first alocirc)) … (locirc-func (rest alocirc)) … ])) (define (circle-func a-circ) …) What goes into that template?

Creating a template list-of-circles: steps ;; A circle is a (make-circle posn number) (define-struct circle (center radius)) ;; A list-of-circles is either ;; - empty, or ;; - (cons circle list-of-circles) (define (locirc-func alocirc) (cond [(empty? alocirc) …] [(cons? alocirc) … (circle-func (first alocirc)) … (locirc-func (rest alocirc)) … ])) (define (circle-func a-circ) (circle-center a-circ) … (circle-radius a-circ) …) There are no cases, so we move directly to extracting the components of the data

Creating a template list-of-circles: steps ;; A circle is a (make-circle posn number) (define-struct circle (center radius)) ;; A list-of-circles is either ;; - empty, or ;; - (cons circle list-of-circles) (define (locirc-func alocirc) (cond [(empty? alocirc) …] [(cons? alocirc) … (circle-func (first alocirc)) … (locirc-func (rest alocirc)) … ])) (define (circle-func a-circ) (circle-center a-circ) … (circle-radius a-circ) …) There are no arrows from the circle defn to other defns, so the template is done

Summary: Constructing the Template for a Data Definition Name the template function If the data defn has cases, add a cond with one clause per case For each case, use selectors (incl. first, rest) to extract the components of the datum Capture every arrow with a function call (this introduces the recursion) This may require additional template functions if multiple data definitions interact

From Templates to Functions (define (locirc-func alocirc) (cond [(empty? alocirc) …] [(cons? alocirc) … (circle-func (first alocirc)) … (locirc-func (rest alocirc)) … ])) (define (circle-func a-circ) (circle-center a-circ) … (circle-radius a-circ) …) Starting from the template, write a function circle-areas that consumes a list of circles and produces a list of their areas

From Templates to Functions First, write examples of how the new function should work. Use the data examples developed with the data defn. - (circle-areas empty) = empty - (circle-areas (cons (make-circle (make-posn 0 0) 15) (cons (make-circle (make-posn –5 3) 40) empty))) = (cons (cons 5024 empty))

From Templates to Functions ;; circle-areas : list-of-circle  list-of-num ;; produces list of areas of the circles in the list (define (circle-areas alocirc) (cond [(empty? alocirc) …] [(cons? alocirc) … (circle-func (first alocirc)) … (circle-areas (rest alocirc)) … ])) (define (circle-func a-circ) (circle-center a-circ) … (circle-radius a-circ) …) Next, rename the template function and add contract/purpose

From Templates to Functions ;; circle-areas : list-of-circle  list-of-num ;; produces list of areas of the circles in the list (define (circle-areas alocirc) (cond [(empty? alocirc) empty] [(cons? alocirc) … (circle-func (first alocirc)) … (circle-areas (rest alocirc)) … ])) (define (circle-func a-circ) (circle-center a-circ) … (circle-radius a-circ) …) Next, use examples to fill in the …; Start with the base case

From Templates to Functions ;; circle-areas : list-of-circle  list-of-num ;; produces list of areas of the circles in the list (define (circle-areas alocirc) (cond [(empty? alocirc) empty] [(cons? alocirc) … (circle-func (first alocirc)) … (circle-areas (rest alocirc)) … ])) (define (circle-func a-circ) (circle-center a-circ) … (circle-radius a-circ) …) For the recursive case, ask what each piece should give you The list of areas of the circles in the rest of the list need area of first circle

From Templates to Functions ;; circle-areas : list-of-circle  list-of-num ;; produces list of areas of the circles in the list (define (circle-areas alocirc) (cond [(empty? alocirc) empty] [(cons? alocirc) … (area (first alocirc)) … (circle-areas (rest alocirc)) … ])) ;; area : circle  number ;; calculates the area of a circle (define (area a-circ) (circle-center a-circ) … (circle-radius a-circ) …) This tells you what circle-func needs to do – rename it and add contract/purpose (but finish it later) The list of areas of the circles in the rest of the list need area of first circle

From Templates to Functions ;; circle-areas : list-of-circle  list-of-num ;; produces list of areas of the circles in the list (define (circle-areas alocirc) (cond [(empty? alocirc) empty] [(cons? alocirc) (cons (area (first alocirc)) (circle-areas (rest alocirc)))])) ;; area : circle  number ;; calculates the area of a circle (define (area a-circ) (circle-center a-circ) … (circle-radius a-circ) …) Determine how to combine the results on first and rest. Our output must be a list, so we probably want cons The list of areas of the circles in the rest of the list need area of first circle

From Templates to Functions ;; circle-areas : list-of-circle  list-of-num ;; produces list of areas of the circles in the list (define (circle-areas alocirc) (cond [(empty? alocirc) empty] [(cons? alocirc) (cons (area (first alocirc)) (circle-areas (rest alocirc)))])) ;; area : circle  number ;; calculates the area of a circle (define (area a-circ) (* pi (square (circle-radius a-circ)))) Circle-areas is done, so now go finish area

Summary of Design Process Develop data definitions and examples of the data Develop the template(s) for the data defns Once given a function to write, write examples of the expected output of the function Edit the template to complete the function definition Test the function with your examples