Not all of the following functions are best written using accumulators, but some might be. Except where explicitly required, it is your option whether to use an accumulator or not. As always, when you do use an accumulator, you must provide a description of what it represents.
For each function you define, you must also specify what recipe type it conforms to. The options are:
If you identify a function as structural or structural with loop, you should identify which argument(s) determine the structure.
In addition, for those identified as structural or generative, you should also further specify that it uses an accumulator where appropriate.
Here are some examples illustrating the additional kinds of combinations that might arise now that we have the option of using accumulators:
;; rev-acc : (Listof X) (Listof X) -> (Listof X) ;; reverses a list using an accumulator ;; recipe type: structural, using alox, with accumulator ;; accumulator represents the elements seen so far, in reverse order (define (rev-acc alox accum) (cond [(empty? alox) accum] [else (rev-acc (rest alox) (cons (first alox) accum))])) ;; route-exists-aux? : Node Node Graph (Listof Node) -> Boolean ;; checks whether a route exists from orig to dest in graph ;; recipe type: generative, with accumulator ;; accumulator collects the nodes on the route traversed so far (define (route-exists-aux? orig dest graph nodes-so-far) ...) [defined in class]
#| Keeping Track of Patients A PDB (short for Patient Database) is one of: - empty - (cons Patient PDB) A Patient is (make-patient String MeasurementList) A MeasurementList is one of: - empty - (cons Measurement MeasurementList) A Measurement is a Number The measurements are pulse measurements, in beats per minutes. A healthy pulse measurement for patients is between 50 and 80. |# (define-struct patient (name pulses)) (define LOWER 50) (define UPPER 80)
Problems:
create-patient
,
which consumes a name (a String) and creates a patient for whom
there are no measurements.add-measurement
.
The function consumes
a patient and a Measurement; it produces a patient record that contains
all the original patient information together with the new Measurement.danger?
. The function
consumes a patient and produces true only if the most recent measurement
is outside the range [LOWER,UPPER]
.sudden-change?
.
The function consumes
a patient and determines whether any measurement differs from the
average of the previous three by more than 10 beats. (If there are
fewer than four measurements altogether, it should produce false.)
UPPER
. Create two versions of this
function: one that uses an accumulator (called too-high-acc?
)
and another (called too-high?
), that does not.
(Develop the two functions separately; neither should call the other.)