7.8

Homework 2

home work!

Programming Language BSL

Due Date: Friday September 25, 6pm.

Purpose: To write simple functions; design data; design World Programs

Expectations

Failure to comply with these expectations will result in deductions and possibly a 0 score.

Finger Exercises: HtDP/2e: 13, 18, 19, 20, 21; 30, 31; 48, 49

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

Design the function describe-ordering that takes three real numbers, and returns one of four possibilities:
  • "ascending" if the numbers are increasing,

  • "descending", if they are decreasing,

  • "repeats" if any two numbers are equal,

  • or "scrambled" otherwise.

Hint: Just like + can take more than just two arguments, you might find a comparison function that’s useful in this problem that takes more than two arguments as well.

Exercise 2
  • Look up US Route 66 (the interstate highway from Los Angeles to Chicago), and give a data definition for Route66State that can represent all the US states the highway passes through. Follow all the steps of the data design recipe:
    • Give the definition itself.

    • Give an interpretation of your data definition.

    • Define three examples of a Route66State, as BSL define statements.

    • Define the template for a function that takes in a Route66State.

  • Design the function terminal-state? that takes in a Route66State and determines if Route 66 has an endpoint in that state. Follow all the steps of the function design recipe:
    • What is the signature for this function?

    • What is its purpose statement?

    • Design the function strictly based on its template.

    • Be sure to include sufficient examples.

    Now ask yourself whether, given your data design for Route66State, this function can be simplified? If so, give a simplified definition terminal-state?/v2. If your new implementation is designed correctly, it should have the same signature, purpose, and test cases as terminal-state? (except for adjusting the function name slightly).

  • Design the function next-state-eastbound that takes in a Route66State and returns the next state that someone driving Eastbound on the highway would enter...if there is one. If there is no such state, then return "No more road!". When designing this function, think carefully about its signature. In a comment, explain your choice for that signature.

Exercise 3 The next two problems will deal with clocks. We will measure time down to the minute (no worrying about seconds). We’ll define a Minute as an integer from 0 to 59, inclusive, and we will define an Hour as an integer from 0 to 11, inclusive. (Why do you think we chose this instead of 1 to 12?) Finally, we’ll say that a DayMinute is any non-negative integer between 0 and 719, inclusive. (Where did that number come from?)

  • Design a function dayminute->hour that takes in a DayMinute and produces the Hour of the day it’s in. (Hint: look up the floor function.) For example, day-minute 65 is in hour 1.

  • Design a function dayminute->minute that takes in a DayMinute and produces the Minute within an hour that it’s in. For example, day-minute 65 is minute 5 of hour 1, so your function should produce 5.

  • Given two Hours H and K, we can consider the range of time at-or-after H:00 and before K:00. (Note that we’re ignoring AM and PM, so there is no time between 3:00 and 2:00, since we’re not “wrapping around” at noon or midnight.) Design a function between-hours? that takes in a DayMinute and two Hours, and determines if the given DayMinute is in the range between those hours. For instance, minute 60 corresponds to 1:00, so it is between hour 0 and hour 2, and it’s also between hours 1 and 2, but it is not between hours 0 and 1.

Note: For each of the above three functions, you must define different tests than the examples we have given in this problem.

Exercise 4 We want to draw a clock face that animates, like this:

2:45 = image

We give you the following constants, which can copy and paste into your code (right click on each clock-hand image to copy it, then paste directly into DrRacket; you should write out the expression for the clock face yourself):

If you have a HiDPI monitor, then all the pixel measurements here might need to be doubled.

The clock-face image is of size 81x81 pixels, rather than 80 pixels, so that the circle isn’t “clipped” off on the bottom and right sides. If you write it out yourself in BSL, it will behave as you’d expect and have an image-height and image-width of 80 pixels as intended.

(require 2htdp/image)
(require 2htdp/universe)
 
(define CLOCK-HOUR-HAND image)    ; this hour hand is 20px tall
(define CLOCK-MINUTE-HAND image)  ; this minute hand is 30px tall
(define CLOCK-FACE image) ; this clock face has radius 40

After you’ve completed this problem, in a comment in your code, explain why you think the two clock hands have space beneath them.

  • For the following three functions, which produce images, you should include three tests for each function:
    • A check-expect where the expected output is the actual image you want (you can copy this out of the interactions area, and paste it into the definitions area), so we can visually see what you want to have happen

    • A check-expect where the input is the same as the previous test case, but the output is the BSL expression that generates the desired image

    • One additional, distinct check-expect of your choosing.

    Design a function hour->img that takes in a Hour and produces an image of the hour-hand of the clock, rotated to the correct angle. (Note: you can ignore the fact that “real” watches rotate the hour hand a little bit, based on the current minute of the clock as well.)

    Design a function minute->img that takes in a Minute and produces an image of the minute-hand of the clock, rotated to the correct angle.

    Finally, design a function dayminute->img that takes in a DayMinute and produces a complete image of the clock at that time. (Hint: use functions you’ve already designed, to help here!)

  • To actually animate your clock:

    Design a function next-dayminute that takes a DayMinute and produces the next DayMinute.

    Design a function show-clock that takes a DayMinute to start from, and uses big-bang with next-dayminute and dayminute->img to update and draw your clock. Note: you do not have to provide test cases for this function.