7.0

Week 4 Set b

home work!

Programming Language BSL

Due Date Thursday 9/27 at 9:00pm (Week 4)

Purpose To design functions with lists, posns, maps, and errors.

Finger Exercises

Exercise 1 From HtDP, 143 144 145

Graded Exercises

Love Thy Neighbor

Exercise 2 Design a function which given a Posn outputs its four neighbors, directly to the east, west, north, and south. While you may assume the given posn’s coordinates are always between 0 and 99, you must make sure to return only the neighbors where both coordinates lie above -1 and below 100.

Be sure to define any data definitions you might need for this problem and to follow the design recipe the entire way through.

Double Or Single Bag?

Below is a data definition for a grocery catalogue that could be used by a cash register machine:
; 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.