Assignment 8
Due Date Monday 02/11 at 9pm
Purpose To design functions over lists and compound data.
You should submit a single .rkt file containing your responses to all exercises via the Handin Server. We accept NO email submissions. Failure to submit a .rkt file will result in a 0.
You are only allowed to use the language specified at the top of this page: failure to do so will result in a 0.
Your code MUST conform to the guidelines outlined in the style guide on the course website. The style guide will be updated as the semester progresses so please remember to read it before submitting each assignment.
You must follow all the steps of the design recipe when completing this assignment.
Please be sure to look at the feedback for assignment 6 before submitting, as we will be grading you more harshly on things we have warned you about before.
You must submit this assignment with your partner. Please make sure you can submit with your partner before 5pm on Monday or we cannot guarantee that you will be able to submit your homework before the deadline. If the course staff are unable to assist you after 5pm and this causes your homework to be late we will not grant an extension.
(define-struct shopping [item others]) ; A ShoppingList is one of: ; - "no items" ; - (make-shopping GroceryItem ShoppingList) ; and represents a list of items to buy at the grocery store (define-struct item [name price quantity]) ; A GroceryItem is a (make-item String PositiveRealNumber Nat) ; - where name is the name of the item ; - price is the item's price at the nearest grocery store ; - and quantity is how many of the item you want to buy
Exercise 1 Provide examples and a template for each data definition above.
Exercise 2 Design a function total-cost which, given a ShoppingList, produces the total cost of purchasing everything on the list. You may assume that the price given for an item is for only 1 of that item.
Exercise 3 Design a function that accepts a ShoppingList and produces an image of the names of the items on the list above each other. Items at the front of the shopping list should appear above the others. An empty shopping list should be drawn with an empty-image. Above all of them (no matter how many items are on the list), the text "Shopping list:" should appear in "blue".
Exercise 4 Design a function that accepts a ShoppingList, a String (representing the name of an item), and a PositiveRealNumber (representing the price of an item). If the item is already on the list the function should add 1 to the quantity for that item (you do not need to check if the price is the same). If the item is NOT on the list, it should be added with a quantity of 1 and the given price. Your function should NOT go through the ShoppingList more than once.
Exercise 5 Design a data definition for an Event. An event has a name (e.g. "Women's march") and a date containing a year, month, and day. Remember to follow all the steps of the data design recipe.
Exercise 6 Design a data definition for a Timeline. A Timeline is either an Event or it has an Event and another Timeline representing events that happened after that first event. Remember to follow all the steps of the data design recipe.
Exercise 7 Consider the following data definitions:
(define-struct letter [from to weight]) ; A Letter is a (make-letter Address Address PositiveRealNumber) ; - where from is the address the letter is from ; - to is the address to send the letter to ; - and weight is the letter's weight (in ounces) (define-struct addr [street num zip]) ; An Address is a (make-addr String Nat ZipCode) ; - where street is the name of the street ; - number is the house number on the street ; - and zip code is the zip code of the address (define-struct zip [a b c d e]) ; A ZipCode is a (make-zip Digit Digit Digit Digit Digit) ; representing a 5-digit zip code ; A Digit is a Nat in the range [0,9] Design a function update-zip-digit which, given a Letter, a Digit, and a natural number in the range [1,5] (representing one of the 5 zip code digits) updates the zipcode in the "to" address by changing the given field to have the given Digit. We have provided one test (which is most definitely not sufficient testing) to help explain this:
(define ZIP-BOSTON (make-zip 0 2 1 1 5)) (define ZIP-ILLINOIS (make-zip 6 0 2 0 8)) (define ZIP-IL-MISTAKE (make-zip 6 0 0 0 8)) (define ADDR1 (make-addr "Huntington Ave" 360 ZIP-BOSTON)) (define ADDR2 (make-addr "Clark St" 633 ZIP-ILLINOIS)) (define ADDR2-MISTAKE (make-addr "Clark St" 633 ZIP-IL-MISTAKE)) (define LETTER-BAD (make-letter ADDR1 ADDR2-MISTAKE 5)) (define LETTER-GOOD (make-letter ADDR1 ADDR2 5)) (check-expect (update-zip-digit LETTER-BAD 2 3) LETTER-GOOD) You do not have to provide the steps of the data design recipe for the given definitions. However, doing so will definitely help you to design the function.