Lab 8 - Spring 2021

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

  1. Open a browser and copy/paste this URL: https://classroom.github.com/a/L1dr6Fb2

  2. Login to Github and you will be prompted to accept this assignment
    1. After you click on the [Accept this assignment] button, you will see a new page display in your browser with the message:
      1. You accepted the assignment, Lab 8. We're configuring your repository now. This may take a few minutes to complete. Refresh this page to see updates.
    2. After you refresh the page you should see a page with the message: “You're ready to go!”
    3. Note: this will create your own copy of Lab 8 for you to work on, but it's not on your computer yet.

  3. Click on the URL where your assignment repository has been created: https://github.com/Vassar-cs101-mlsmith/lab-8-yourGitHubID
    1. you are now on the GitHub page for your Lab 8 repository

  4. Click on the green button to download your [Code]
    1. a “Clone” dialog box will appear, with the HTTPS tab underlined in red (if not, click on the HTTPS tab to select it)
    2. select the last option labeled “Download ZIP” and save it on your computer
    3. move the Lab 8 zip file from its download location to the cs101 folder that you created during lab last week.
    4. extract the contents of the zip file either by double-clicking on it or control-clicking on it and selecting the appropriate option to extract its contents
    5. navigate into your 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-expects.

  1. Launch DrRacket and open your lab8.rkt file
    1. Fill in your name where <YOUR NAME> appears in the comments at the top of the program.

  2. Implement the function count-white, which consumes a list-of-toast and produces the number of toasts in the list with the kind “white”.

    Here is my 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.

  3. Implement the function count-wheat, which consumes a list-of-toast and produces the number of toasts in the list with the kind “wheat”.

    Here is my 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.

  4. Implement the function count-untoasted, which consumes a list-of-toast and produces the number of toasts 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.

  5. Implement the function count-yummy, which consumes a list-of-toast and produces the number of toasts in the list that are yummy. Yummy toast is wheat 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.



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

  • From your browser on the GitHub page for your Lab 8 repository, click on the [Add file] drop-down button and select “Upload files”
  • From your lab8 folder on your desktop, click and drag the lab8.rkt file onto your browser where it says, “Drag files here to add them to your repository”
    • just below the region of the webpage you should see you lab8.rkt file appear; it'll be just above the Commit Changes section of the page.
  • You are now ready to commit changes! Click on the green [Commit changes] button.
  • Your updated lab8.rkt file should now be in your Lab 8 GitHub repository.
    • ask a coach to verify this with you.