Homework 3
Due Date: Wednesday January 29, 9pm.
Purpose: Designing structured data; writing functions that process structured data; designing World programs
Expectations
You should submit three .rkt files, each containing your response to one of the 3 exercises below, via the Handin Server; the three files should be named "HW3-1.rkt", "HW3-2.rkt", and "HW3-3.rkt". We accept NO email submissions. Failure to submit a .rkt file will result in a 0 for that part.
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 may be updated as the semester progresses so please remember to read it before submitting each assignment.
Unless otherwise stated, all data and functions you design must be designed according to the data and function design recipes (DR), resp., following all four steps in each case, with the exception of functions that call big-bang, for which you need no check-expects, as justified in class. All functions used inside big-bang must have check-expects, along with the other three steps of the design recipe.
We will not walk you through the DR steps in each problem, nor request them individually and explicitly. Instead, review the data and function design recipes before starting this homework, and then have these recipes handy.
Graded Exercises:
Exercise 1 Look at the following structure type definitions:
(define-struct book [title author year])
(define-struct date [day month year])
(define-struct holiday [title date is-observed])
You must use these structure definitions to design the data types Book, Date, and Holiday, respectively. For each structure definition, write down:
The names of the functions (constructors, selectors, and predicates) that each definition introduces
A reasonable data definition
An interpretation
Three examples
The function template for that data definition
Exercise 2
The time of day, broken down into hour and minute components, lends itself nicely to being represented as a structure type definition.
First, design the data types Hour and Minute to represent hours with a range of 0 thru 23, and minutes with a range of 0 thru 59, respectively. Ensure you follow all steps of the data design recipe.
Create a define-struct time that has fields named hours and minutes.
Based on this structure definition, as well as your Hour and Minute data types, design a new data type called Time, which represents the time of day in hours and minutes. Again, ensure you follow all steps of the data design recipe.
Design the function tick-tock that consumes a Time, t, and produces a Time that represents the next minute immediately after t.
Design the function time->text, which converts a Time to text. It should represent time as a 12-hour digital clock: two digits each for the hour and minute values, with leading ’0’ if needed, separated by a colon (’:’), followed by either "AM" or "PM". For example, if the hours is 11 and minutes is 20, it should be produce "11:20 AM", while 17 hours and 1 minute should produce "05:01 PM". Additionally, a 12-hour clock never displays 0 for the hours–the displayed hours is strictly between 1 and 12, inclusive–so 0 hours and 15 minutes should produce "12:15 AM". Lastly, the special cases 0 hours and 0 minutes (midnight) should produce "12:00 AM" and 12 hours and 0 minutes (noon) "12:00 PM".
Hint: You might investigate the useful function number->string–watch out for leading 0’s though (or the lack thereof).
Design the function digital-clock that accepts a starting Time t and uses big-bang to draw an animated digital clock that runs starting from the given time. Note: you do not have give check-expects for this function.
Exercise 3
A hotel review website, such as Yelp, lists a lot of information about each hotel, including its name, luxury rating (in stars), and a summary of its price (in dollar signs). Design a new data definition called Hotel, which contains these three pieces of information about a hotel. Assume that hotels are rated with one to five stars, and their price is rated using one to four "$"s.
Hint: You will need auxiliary data definitions to define Hotel.
Finding a hotel that’s not too costly can be tricky. Design a function called in-budget? that takes a Hotel and a price rating, and produces #true if the Hotel is no more expensive than the price rating you’re willing to spend.
The hotel business is tough these days, and less-expensive options are certainly more attractive than pricey ones... Design a function called make-cheaper that consumes a Hotel, called h, and produces a Hotel that has one fewer "$" than h, adds the string "Cheaper " to the beginning of the hotel name, and has the same star rating. However, if the price of h is already in the lowest category (one "$"), the function should produce h itself.
The Yelp website summarizes each hotel by displaying a little panel, such as the following screenshot from the Yelp website:
You can use the 2htdp/image library to draw a similar panel, such as the following image that we produced:
Design a function that draws a panel that is approximately the same as the example above. (It does not have to be pixel-for-pixel identical to this example.)