Lab 8 - Spring 2019

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.




How to get started

  1. Use your CS account to login to one of the Linux workstations in the classroom.

  2. Open a browser and copy/paste this URL: https://classroom.github.com/a/QTWNHrHS

  3. Login to Github and you will be prompted to accept this assignment
    • Note: this will create your own copy of Lab 8 for you to work on, but it's not on your Linux account yet

  4. Open a terminal window.

  5. Create a new directory for this lab in your cs101 directory by entering the following at the Linux prompt:

    cd cs101
    mkdir lab8
    cd lab8

  6. Type the following commands to pull down a copy of this lab from GitHub:
    $ git init
    $ git remote add origin https://github.com/Vassar-cs101-mlsmith-spr2019/lab-8-yourGitHubID
    $ git pull origin master



Now you are ready to write some functions using the Design Recipe!

Instructions

  1. Launch DrRacket and open your lab8.rkt file

  2. Fill in your name where <YOUR NAME> appears in the comments at the top of the program.

  3. Develop the functions for the Exercises as described below.
    • solve each exercise in order
    • follow the steps of the Design Recipe for each function
    • be sure your solution for each exercise includes all appropriate grading artifacts.

Information and instructions

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. 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:
    ; count-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.

  2. 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:
    ; count-toast: list-of-toast string -> number
    ; counts the number of toasts in the list with the given kind
    (define (count-toast lot kind) ...)
    
    ;; count-wheat: 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 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.

  4. 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.

Submitting your work

From a terminal window, type the following commands at the Linux prompt:

$ git commit -m "finished lab8" lab8.rkt
$ git push -u origin master



Log out

When you are done, close DrRacket and log out. Always remember to log out when you are done using the system, to ensure that no one else uses your account.