![]() |
![]() |
|
home—info—lectures—exams—archive
The steps of design recipe (taken from How to Design Programs).
1. Data Def'n
2. Examples of Data
3. Template for functions handling the data
4a Write function header
b Write function comments
c Write test cases
d Write function body
i. a cond line for each variant of union data
ii. pull out fields for each line handling a struct
iii. add natural-recursion call, if present.
5. run tests
Compare to the data defn for natnums:
; Data def'n:
; A natnum is:
; - 0, or
; - (add1 [natnum])
What would a template look like for natnums?
Did our code for ! follow the template? Yes!
The robo-rally game: [Images at wikipedia]
A robot-move is a sequence of many steps, where each step is
either moving forward, or turning.
E.g. "move forward 3, then turn right, then turn right, then move forward 1."
How to represent this type of data, in our program?
A robo-move is
- empty, or
- (cons number robo-move)
- (cons symbol robo-move)
Examples of the data:
(cons 3 (cons 'right (cons 'right (cons 1 empty))))
(cons 'right (cons 'right (cons 'right empty)))
empty ; Of course!
Template. How many branches in the cond? [See data def'n.]
; total-steps : (-> robo-move number)
; How many forward-moves are in this robo-move?
; NB Moving forward, turning 180, and moving back counts as 2 moves.
; (This might be helpful for determining battery usage.)
;;;;;;;;;;;;; Trees
Trees.
abe mona jackie
homer marge
bart
(define abe (make-person "Abe" 1920 'blue 'unknown 'unknown))
(define mona (make-person "Mona" 1929 'brown 'unknown 'unkown))
(define jackie (make-person "Jackie" 1926 'brown 'unknown 'unknown))
(define marge (make-person "Marge" 1956 'blue jackie 'unknown))
(define homer (make-person "Homer" 1955 'brown mona abe))
(define bart (make-person "Bart" 1979 'brown homer marge))
contains-name?
count-oldies
find-blue-eyes
find-blue-eyed-orphans
find-oldies
.... generalized find!
find-long-names
find-named-for-ancestor
find-time-travelers: somebody whose has an ancestor whose birth-year
is later than their own!
Quote.
Sexpr.
(see GlobalPreferences.plist.orig
build-list
write filter (passing in a list)
map, filter, fold(reduce);
writing loop;
Accumulator versions:
; sum-accum
; reverse : (-> (list-of any) (list-of any))
This follows from the template, although we need a helper:
; add-at-end : (-> (list-of animal) animal (list-of animal))
; Given a list of animals and another animal,
; Return a list containing all the listed
;
(define (add-at-end zoo newguy)
...)
What is running time? Memory usage?
Accumulators; tail-recursion.
home—info—lectures—exams—archive
| ©2008, Ian Barland, Radford University Last modified 2008.Sep.19 (Fri) |
Please mail any suggestions (incl. typos, broken links) to ibarland |
![]() |