$19
Expectations
• You and your partner must submit a single .rkt file containing your responses to all exercises via the Handin Server. We accept no email submissions.
• You must use the language specified at the top of this page.
• 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 revisit it before submitting each assignment. (You can resubmit as many times as you like until the due date without penalty.)
• Unless otherwise stated, every function and data definition that you design, including helper functions, must follow all steps of the design recipe.
Failure to comply with these expectations will result in deductions and possibly a 0 score.
Finger Exercises: HtDP/2e: 103, 104, 105
Notes: to find the finger exercises, you’ll need to read the textbook and search for them by number. Also note the “2e” in the name: the exercise numbers changed substantially between the first and second editions. Make sure you’re reading the more recent edition. Finally, some of the finger exercises we recommend above rely on earlier exercises for context: you may need to scroll backwards in the textbook to figure out where the overall exercise starts.
Graded Exercises:
Exercise 1
Consider the following structure definitions:
(define-struct circl [radius color outline])
(define-struct rect [width height color outline])
(define-struct txt [string size color])
a. For each of the above structures, give an appropriate data definition for the corresponding shape: Circle, Rectangle and Text. Make sure you complete all steps of the data design recipe.
b. Give a data definition for Shape which can be any of the above shapes.
c. Design a function change-color, which takes a Shape and a color, and produces an otherwise-identical Shape that has the given color instead.
d. Design data to represent a ShapeScene. There are three kinds of ShapeScenes:
◦ a scene with 2 shapes,
◦ a scene with 3 shapes, or
◦ a scene with 4 shapes.
Note, a ShapeScene is not the same as a DrRacket ’scene’ from 2htdp/image.
e. Design a function change-size, which takes a ShapeScene and a numeric factor, and creates a new ShapeScene where all the shapes in have been changed by the given factor as follows:
◦ for a Circle, multiply its radius,
◦ for a Rectangle, multiply both its width and height, and
◦ for a Text, multiply the size.
f. Design a function rotate-scene which rotates the images in the given ShapeScene in the given Direction. The direction can be either "clockwise" or "counter-clockwise". For example, if a scene contains 3 shapes in the order shape1 shape2 shape3, rotating clockwise will result in shape3 shape1 shape2 and rotating counter-clockwise will result in shape2 shape3 shape1. Design any additional data you might need for this function.
g. Design a function draw-scene which renders a given scene according to the following sketches:
◦ Two shapes:
+-------------------+
| |
| |
| 1 2 |
| |
| |
+-------------------+
◦ Three shapes:
+-------------------+
| |
| 1 |
| |
| 3 2 |
| |
+-------------------+
◦ Four shapes:
+-------------------+
| |
| 1 2 |
| |
| 4 3 |
| |
+-------------------+
h. Design a function which takes a ShapeScene and uses big-bang to render the scene and wait for keyboard input. If the key l is pressed, the shapes rotate counter-clockwise. If the key r is pressed, the shapes rotate clockwise. If the key q is pressed, the world ends. You do not need to write check-expects for this function.
Hint: You may need to design a new data definition to represent the world state for this question.
Exercise 2 The date command, which is part of the GNU Coreutils package and is installed by default on many Linux systems, can display date and time in various formats. One of its features is that is allows relative date specifications. For example, to display what the date and time will be 12 days from now, you can invoke
$ date -d "12 days"
To display the date and time 2 months in the past, we can use
$ date -d "2 months ago"
In this exercise, we will design the data and functions to support a simplified version of this command.
a. Consider the incomplete data definitions below.
(define-struct date [year month day])
; ...
(define-struct time [hour minute second])
; ...
; A DateTime is (make-date-time Date Time)
; ...
First, complete the data definitions for representing a time and date. Use a 24-hour representation to store time. Make sure you follow all steps of the data design recipe.
Next, complete the data definition for representing the current date and time, which includes: the year, month, day, hour, minute and second.
As you complete these data definitions, note that there are various missing pieces, and you must fill in the missing pieces without changing what we have provided above.
b. Design the function format-date-time which takes a DateTime and formats it as a string using the format "MM/DD/YYYY hh:mm:ss am/pm", for example, "09/29/2020 01:20:01pm".
c. Complete the data definitions below for representing a relative time specification, including any structure definitions you might need. (You must spot what is missing. We are not providing any ellipses to hint at missing pieces.) We want to represent specifications of the form:
◦ "now"
◦ "in 2 hours"
◦ "1324 seconds ago"
◦ "4 months ago"
◦ "in 3 years"
; A TimeUnit is one of
; - "second"
; - "minute"
; - "hour"
; A DateUnit is one of
; - "day"
; - "month"
; - "year"
; A RelativeTime is one of
; - "now"
; - (make-time-delta Number TimeUnit)
; - (make-date-delta Number DateUnit)
d. Design a function adjust-time, which takes a DateTime and a RelativeTime and calculates the time adjusted by the given RelativeTime. For simplicity, assume that all months in a year are 30 days long. Positive differences adjust forward, negative backward.
e. Design a function date-time-diff, which, given two DateTimes and a DateUnit, claculates the absolute difference between the two dates in the given unit. Ignore the time of day portion of both DateTimes.
Exercise 3 (Related to HtDP Exercise 109)
A Finite State Machine is an abstract (and general) encoding of a fairly common scenario: it represents the idea of a finite collection of states, and a set of allowable transitions between them. For example, the traffic-light animation used three states (green, yellow, or red), and three transitions (green -> yellow, yellow -> red, red -> green) that occurred once per tick. As another example, a telephone might be in one of three states (idle, ringing, or in-use), and might have several transitions: idle -> ringing when a call comes in, ringing -> idle if you choose to ignore the call, ringing -> in-use if you answer the phone, and in-use -> idle when you hang up. In general, there could be an arbitrary number of states, and arbitrary transitions between the states.
In this problem, you’ll design a world program that implements a Finite State Machine, that recognizes when a user types "good", "goood", "goooood", or similar variants with even more "o"s in them. These letters must appear consecutively: if any other characters appear in the middle (like in "goCS2500od"), your program should not accept the input. However, it doesn’t matter how many letters appear before or after: "CS2500 is gooooooood!" would be accepted.
Concretely:
• Your program will have five states:
◦ START: haven’t seen any part of "good" yet
◦ G: have seen the intial "g"
◦ O1: have seen at least one "o"
◦ O2: have seen at least two "o"s
◦ D: have seen the final "d"
• From state START, if the user types a "g", move to state G. If the user types anything else, stay in START.
• From state G, if the user types an "o", move to state O1. If the user types a "g", stay in state G. If the user types anything else, go back to state START.
• From state O1, if the user types a second "o", go to state O2. If the user types a "g", go back to state G. If the user types anything else, go back to state START.
• From state O2, if the user types another "o", stay in state O2. If the user types a "g", go back to state G. If the user types a "d", go to state D. If the user types anything else, go back to state START.
• From state D, no matter what the user types, stay in state D.
• To render your program: if the user is in state START, draw a "white" rectangle. Draw the next four states as rectangles with colors "pale green", "spring green", "lime green", and "dark green", respectively.
Suggestion: Draw these rules as a diagram in the style of Exercise 109, to help you as you develop your code. You don’t need to hand this in, but having a picture available to you might help organize your thinking.
Reminder: This problem asks you to design the world program. That implies you must first determine what information should be in the world state, and design a data definition for it. Only then can you begin to design the helper functions that the world program needs in order to run.