Week 4 Set b
Due Date Thursday 9/26 at 9:00pm (Week 4)
Purpose To design functions with lists, maps, and errors.
Finger Exercises
Graded Exercises
Exercise 2 Design a function which, given a list of integers and an integer i, checks if i occurs in the list an even number of times.
Double Or Single Bag?
; A GC (GroceryCatalogue) is one of: ; - '() ; - (cons CE GC) ; where each catalogue entry has a unique name ; A CE (CatalogueEntry) is a ; (make-ce String Cost) (define-struct ce [name cost]) ; and represents the name of a food and how much it costs ; A Cost is one of: ; - (make-unit Number) ; - (make-lb Number) (define-struct unit [cost]) (define-struct lb [cost]) ; and represents either the cost per unit or per lb of an item ; A Checkout is one of: ; - '() ; - (cons Order Checkout) ; A Order is one of: ; - String ; - (make-weight String Number) (define-struct weight [name lb]) ; and represents either one unit of food or its name and weight in lbs
While a GC is certainly a list, this kind of data is often referred to as a map (for which mapping, hash, and dictionary are all synonyms depending on the language you are in). A map is a type of data that associates pieces of one type of data with another; in this case, foods with their costs. Maps are ubiquitous throughout computer science, and you may even find them come up again in this course!
Exercise 3 Define examples for and the template of each of the above data definitions.
Exercise 4 Design a function get-cost, which given the name of a food and a GC gets its associated Cost. If no Cost is found, return an informative error.
Exercise 5 Design a function set-cost, which given the name of a food, a Cost, and a GC returns the catalogue with that food’s Cost now set to the given one.
Exercise 6 Design the function average-unit-cost, which given a GC, produces the average cost of items that are priced per unit. If there are none, return an informative error. The number of items that are priced per unit should only be computed once!
Exercise 7 Design the function express-lane?, which determines if a Checkout has 10 items or fewer.
Hint: for testing purposes, one can use make-list to make large lists quickly.
Exercise 8 Design the function total-cost, which given a Checkout and a GC produces the total cost. If a food has been weighed that is priced by unit or if a food that is priced by pound has not been weighed, return an informative error.