![]() |
![]() |
|
home—lectures—recipe—exams—hws—D2L—breeze (snow day; distance)
---- Per data type:
1. Choose data definition
2. Give some examples of the data
3. Template:
a. If handling a union type, include a cond w/ one branch per option.
b. If handling a product-type, pull out each piece (field).
(Do this for an overall product-type, or within cond-branches from (a).)
c. Inventory: In all cases, think about what type (each piece) is,
and think of some functions that process that type.
d. Add the natural recursive call (as appropriate).
---- Per function:
4. write test cases
5. (a-d): contract/type, header, purpose, copy the template, and stub it.
6. Inventory with values: Consider a particular test case,
and think about what specific value of each sub-expression.
7. complete the body-expression
8. run tests
A list-of-numbers is: - empty, or - (cons [number] [list-of-numbers])
empty (cons 4 empty) (cons 2 (cons 4 empty)) (cons 93 (cons 2 (cons 4 empty))) |
; func-for-nums : list-of-numbers -> ???
;
(define (func-for-nums a-lon)
(cond [(empty? a-lon) ...]
[(cons? a-lon) (...(first a-lon) ... (func-for-nums (rest a-lon)))]))
|
(check-expect (contains? empty 4) false)
(check-expect (contains? (cons 4 empty) 4) true)
(check-expect (contains? (cons 4 empty) 99) false)
(check-expect (contains? (cons 2 (cons 4 empty)) 99) false)
(check-expect (contains? (cons 2 (cons 4 empty)) 2) true)
(check-expect (contains? (cons 2 (cons 4 empty)) 4) true)
(check-expect (contains? (cons 93 (cons 2 (cons 4 empty))) 93)
true)
(check-expect (contains? (cons 93 (cons 2 (cons 4 empty))) 4)
true)
(check-expect (contains? (cons 93 (cons 2 (cons 4 empty))) 77)
false)
(check-expect (contains? (cons 93 (cons 2 (cons 93 empty))) 93)
true)
|
; contains? : list-of-number, number -> boolean
;
(define (contains? a-lon target)
(cond [(empty? a-lon) ...]
[(cons? a-lon) (...(first a-lon) ... (contains? (rest a-lon) target))]))
|
In this example:
You are given
What is the rule we want, so that we achieve our test cases:
(If you have code that is missing a recursive call, it means you skipped part of step 5 above.)
First, make sure that you do each step, in order. Each step plays helps nudge you to get correct answers to later steps!
Example: When you're pulling out fields of a struct and asking what type they are, you are relying on the data-definition, which included exactly that info.
When you're looking at the result of the recursive call and how to use it, you use the return-type listed in your function-header
In general, every step is used in at least one following step.
Help with the inventory-with-values:
For example, if working with a list template,
the “inventory with values” means writing down the following:
Let's do an inventory-with-values: pick a particular value for `a-lon`, and see what the two pieces from the template refer to, in that case. So, reply back w/ filling out this table: (a) My sample particular value to use for `a-lon`: ________________________________________ (b) what is `(first a-lon)` in this case: __________ (c) what is `(map-sqr (rest a-lon))` in this case: ________ (Note: I'm *not* asking for `(rest a-lon)`, but the result of giving `(rest a-lon)` to `map-sqr`.) (d) What is the value that I would want the overall function to return, in this case? _________________________________ (e) How did I combine (b) and (c) to get (d)? (f) How, in the *general* case, will I combine `(first a-lon)` with `(map-sqr (rest a-lon))` to get my desired answer?
More examples of following the design recipe are in lect09-structs1.rkt (unions and structs) in lect11-structs2.rkt (structs containing structs, structs containing unions, ...) and in lect14-list-intro.rkt (incl. recursively-defined lists).
1We know this by the test case.
You can also think of this as using the technique of wishful-thinking:
“I wish I could just call some other
slightly-smaller-helper-function-I'll-finish-writing-later
that would compute whether
home—lectures—recipe—exams—hws—D2L—breeze (snow day; distance)
| ©2015, Ian Barland, Radford University Last modified 2015.Nov.17 (Tue) |
Please mail any suggestions (incl. typos, broken links) to ibarland |
![]() |