![]() |
![]() |
|
home—lectures—recipe—exams—hws—D2L—breeze (snow day)
---- 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).
c. Add the natural recursive call (as appropriate)
d. Inventory: In all cases, think about what type (each piece) is,
and think of some functions that process that type.
---- Per function:
4. write test cases
5. (a-d): header, purpose, contract/type, 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
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.)
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)
| ©2013, Ian Barland, Radford University Last modified 2013.Oct.07 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarland |
![]() |