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" or "avacado".
;; 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 "avacado" 3)
(make-toast "wheat" 4)
(make-toast "wheat" 4)
(make-toast "avacado" 6)
(make-toast "wheat" 6)
(make-toast "avacado" 9)
(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.
1. Implement the function count-white
, which consumes a list-of-toast
and produces the number of toast
s in the list with the kind
“white”.
Here is the signature/purpose/header:
; 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.
2. Implement the function count-wheat
, which consumes a list-of-toast
and produces the number of toast
s in the list with the kind
"wheat"
.
Here is the signature/purpose/header:
; 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
.
3. Implement the function count-untoasted
, which consumes a list-of-toast
and produces the number of toast
s in the list at toast level
0.
We've already abstracted over count-white
and count-wheat
to create count-toast
, which counts toast of a given kind, but now you will need to abstract further.
Here's a hint: First write a helper function, 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
.
4. Implement the function count-yummy
or—depending on your taste— count-yucky
, which consumes a list-of-toast
and produces the number of toast
s in the list that are yummy/yucky. Yummy/yucky toast is avacado toast at a level
between 4 and 8 inclusive.
Similar hints apply to this function as the previous one: you should be able to use count-bread
as-is, without abstracting it further. You need only write a new PREDicate function, defined locally, to pass to count-bread
.
Submitting your work
.rkt
file on MoodleLog out
When you are done, close DrRacket by choosing Quit
from the File
menu, and then locate the logout option on the menu bar (lower right corner). Choose “Logout…” and follow any remaining prompts. Always remember to log out when you are done using the system, to ensure that no one else uses your account.