Week 6 Set b
Due Date Thursday 10/10 at 9:00pm (Week 6)
Purpose To further your implementation of Pacman and begin using higher-order operations.
Finger Exercises
We strongly recommend completing the following finger exercises.
Exercise 1 Revisit the GC data definition from two weeks ago. Design the function update-gc, which given a GC, the name of a food, and a [Cost -> Cost] function, applies that function to the previously associated cost of that food and leaves the rest of the catalogue untouched. If no such food was present, make a reasonable decision about what to do (there are at least two).
Exercise 2 Imagine, for a moment, you are a real-estate website programmer. One crucial data definition would be that of a House:
; A House is a (make-house String Number Number String Number) (define-struct house [address bedrooms bathrooms style value]) Actual real estate data dealing with houses have much larger (and usually poorly designed) data definitions, but that’s not our problem. Now, which of these values is likely to change over time? Only the house’s cost.
Let’s imagine a scenario in which the market dictates all houses with 2.5 or more baths become worth 1.1 times their current value, those with 2 or fewer bedrooms become worth 0.9 times their current value, and those with a "Colonial" style are all increased by 500 dollars. If more than one of these scenarios apply, then only the first one (as listed here) takes effect; if none do, nothing changes.
To implement this,
design a function that given a house and a [Number -> Number] function applies it to the house’s cost (and leaves the rest of the house as it is). Then,
demonstrate the usefulness of the function you just defined by using it in a function which applies the above scenario to a single house. Then,
demonstrate the usefulness of pre-defined list abstractions (listed below) by using one of them and the function you just defined to apply the above scenario to a list of homes.
Graded Exercises
Note that for the following problems, you must use foldr, filter, map, andmap and/or ormap when appropriate.
Pacman Comes Alive
Exercise 3 Incorporate the feedback you received on your last Pacman assignment (Week 5 Set a, see feedback on handin server). Make the suggested improvements and fix any bugs that may still exist.
Exercise 4 Design the function for your stop-when handler, which ends the game once all of the dots and super dots have been eaten.
Exercise 5 Design the function for your on-tick handler, which moves Pacman in the direction he is facing (so long as such a movement is possible) and eats the dots and power pellets he passes through. Note: If the board permits it, Pacman can wrap around the edges to the other side.
Exercise 6 Bring these functions into your main function. We recommend running it at 1/4 seconds per frame.
Exercise 7 Sometimes we want to bin items by a certain property. For example, let’s say we had the following definition for cups:
(define-struct cup [oz color material]) ; A Cup is a (make-cup NonNegNumber String String) ; and represents a cup's capacity in fluid ounces, color, and material (define CUP1 (make-cup 10 "brown" "wood")) (define CUP2 (make-cup 8 "brown" "ceramic")) (define CUP3 (make-cup 10 "red" "plastic")) (define CUP4 (make-cup 6 "clear" "plastic")) (define CUPS (cons CUP1 (cons CUP2 (cons CUP3 (cons CUP4 empty))))) We could bin cups by their capacity in fluid ounces, in which case we’d have:
10: CUP1 and CUP3
8: CUP2
6: CUP4
Alternatively, we could bin cups by their color, in which case we’d have:
"brown": CUP1 and CUP2
"red": CUP3
"clear": CUP4
Note that the only difference is what characteristic of the items we are using to bin - we’ll refer to the distinct values of this characteristic as the "keys" of the binning (e.g., 10, 8, 6 in the first example). Each entry in the result, a bin, has a key as well as a list of the original items having that key (in the order the were in the original list).
Design the function create-binning that takes in 3 arguments - a list of elements, a key extractor, and a key equivalence relation - and produces a binning.
A key extractor is a function that extracts information from an element to determine what bin it should belong to. In the first example given, it would be a function that takes a cup and produces its capacity.
An equivalence relation is a function that takes two elements and produces a Boolean indicating whether or not they are equal. In the second example, the key equivalence relation would be a function that tells us if two colors are equal.