the design process/recipe
DESCRIPTION
The Design Process/Recipe. An example over list of circles. c. Kathi Fisler, 2001. ;; 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). - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/1.jpg)
The Design Process/Recipe
An example over list of circles
c. Kathi Fisler, 2001
![Page 2: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/2.jpg)
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?]
![Page 3: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/3.jpg)
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))
![Page 4: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/4.jpg)
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]
![Page 5: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/5.jpg)
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
![Page 6: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/6.jpg)
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
![Page 7: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/7.jpg)
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
![Page 8: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/8.jpg)
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
![Page 9: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/9.jpg)
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
![Page 10: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/10.jpg)
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
![Page 11: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/11.jpg)
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
![Page 12: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/12.jpg)
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 …
![Page 13: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/13.jpg)
When you create a template, write one template function for
each data definition
![Page 14: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/14.jpg)
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
![Page 15: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/15.jpg)
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?
![Page 16: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/16.jpg)
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
![Page 17: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/17.jpg)
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
![Page 18: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/18.jpg)
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
![Page 19: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/19.jpg)
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
![Page 20: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/20.jpg)
From Templates to FunctionsFirst, 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 706.5 (cons 5024 empty))
![Page 21: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/21.jpg)
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
![Page 22: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/22.jpg)
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
![Page 23: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/23.jpg)
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 offirst circle
![Page 24: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/24.jpg)
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 offirst circle
![Page 25: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/25.jpg)
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 offirst circle
![Page 26: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/26.jpg)
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
![Page 27: The Design Process/Recipe](https://reader033.vdocument.in/reader033/viewer/2022051018/568144cf550346895db1994a/html5/thumbnails/27.jpg)
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