Today's lab will give you practice with abstraction, local definitions, passing functions as arguments to other functions, and generally with the satisfaction that comes from avoiding duplicate code.
Note: you may find it helpful to refer to the notes from Wednesday's lecture:
lecture14.pdf
How to get started
cs101
folder that you created during lab last week.lab8
folder and confirm you see the lab8.rkt
file there
Now you are ready to write some functions using the Design Recipe!
A toast
and list-of-toast
are defined as follows:
(define-struct toast (kind level)) ; A toast is a ; (make-toast string number[0 to 10]) ; where a toast’s kind can be any string, including (but not limited to) “white” or “wheat”.
; A list-of-toast (lot) is either ; - '() ; - (cons toast lot)
Here's an example of a list-of-toast
that you should use in your examples and tests for the functions you write:
(define toast-list (list (make-toast "white" 0) (make-toast "wheat" 0) (make-toast "white" 4) (make-toast "wheat" 4) (make-toast "white" 6) (make-toast "wheat" 6) (make-toast "white" 10) (make-toast "wheat" 10)))
For each of the functions you implement below, be sure to follow the Design Recipe.
You should have hand-in artifacts for each function's signature, purpose, and header,
as well as examples in the form of check-expect
s.
lab8.rkt
filecount-white
, which consumes a list-of-toast
and produces the number of toast
s in the list with the kind
“white”. ; list-of-toast -> number ; counts the number of toasts in the list with the kind "white" (define (count-white lot) ...)
Be sure to use a local
expression to define the count-of-rest
of the “white” toast.
count-wheat
, which consumes a list-of-toast
and produces the number of toast
s in the list with the kind
“wheat”. ; list-of-toast string -> number ; counts the number of toasts in the list with the given kind (define (count-toast lot kind) ...) ; list-of-toast -> number ; counts the number of toasts in the list with the kind "wheat" (define (count-wheat lot) (count-toast lot "wheat"))
(Aren't you tempted to cut-and-paste count-white
? Don't do it! Instead, design the more general function, count-toast
, above, with an extra parameter, and redefine count-white
and count-wheat
.) I gave you count-wheat
above, you reimplement count-white
.
count-untoasted
, which consumes a list-of-toast
and produces the number of toast
s in the list at toast level
0. count-white
and count-wheat
to create count-toast
, which counts toast of a given kind, but now you will need to abstract further. count-bread
, that only counts toast that satisfies a given PREDicate. Then have count-untoasted
define a local function that determines whether an individual piece of toast is untoasted, and pass that function to count-bread
. count-yummy
, which consumes a list-of-toast
and produces the number of toast
s in the list that are yummy. Yummy toast is wheat toast at a level
between 4 and 8 inclusive. count-bread
as-is, without abstracting it further. You need only write a new PREDicate function, defined locally, to pass to count-bread
.
Get checked off!
Be sure Professor Smith or one of your coaches checks you off as having completed the lab before you
submit your work electronically, and before you leave. We will look for correct program behavior, as
well as well-documented code, including, for each function, a signature, purpose statement, examples,
and check-expect statements.
Submitting your work
lab8.rkt
file onto your browser where it says, “Drag files here to add them to your repository”lab8.rkt
file appear; it'll be just above the Commit Changes section of the page.lab8.rkt
file should now be in your Lab 8 GitHub repository.