6.6

Homework 4

home work!

Programming Language BSL

Due Date Friday, October 4 at 6pm

Purpose To practice using union data and self-referential data.

Expectations
  • You should submit a single .rkt file containing your responses to all exercises via the Handin Server. We accept NO email submissions. Failure to submit a .rkt file will result in a 0.

  • 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 will be updated as the semester progresses so please remember to read it before submitting each assignment.

  • You must follow all the steps of the design recipe when completing this assignment.

  • Please be sure to look at the feedback for assignment 2 before submitting, as we will be grading you more harshly on things we have warned you about before.

  • You must submit this assignment with your new partner, assigned in lab on Tuesday, October 1st.

Graded Exercises

Exercise 1 Design the data necessary to represent a line in mathematics, which has no beginning and no end. We will consider two forms: slope-intercept (where we specify two constants, m and b, such that y = mx + b) and a vertical line (where we specify one constant, which is the x location of the line).

Exercise 2 Consider the following data definition, inspired by the game Barrel of Monkeys:
(define-struct monkey [name c others])
; A MonkeyChain is a (make-monkey String String MonkeyChain)
; and represents a collection of monkeys where:
; - name is the name of this monkey
; - c is the color of this monkey
; - others is the other monkeys it is attached to
What is the problem with this data definition? Describe your answer in a comment. (Hint: try defining examples of a MonkeyChain.)

Exercise 3 Consider the following data definition:
(define-struct delay [reason minutes])
; A TrainStatus is one of:
; - Integer
; - String
; - (make-delay String PosInt)
; - false
; and represents number of minutes away, a status message (e.g., "arriving"),
; minutes of delay and reason, or that the train has been cancelled
After following the remaining steps of the data design recipe for TrainStatus, design the function announce which, given a train number, destination, and TrainStatus, outputs an announcement for the train. Some example announcements include:
  • "Train 9 to Portland is 1 minute away."

  • "Train 123 to Newark is arriving."

  • "Train 82 to Philadelphia is boarding."

  • "Train 76 to Chicago is delayed 10 minutes due to signaling error."

  • "Train 9348 to Boston is cancelled."

Hint: if you find that the right-hand side of your cond branches repeat a lot of the same work, you should move some of that work out of the cond (possibly into helper functions) so your code doesn’t repeat itself.

Exercise 4 Design the function reflexes that shows a red screen for 140 ticks and then shows a blue screen for 14 ticks (at the default rate of 28 ticks per second). This should repeat until the user ends the program (by pressing any key). The input to reflexes can be ignored (and given the Any type), but the output should be a Boolean indicating whether or not the user exited the program during a blue screen.

Use the following data definition to help you, being sure to complete the Design Recipe for data:
(define-struct red [ticks-left])
(define-struct blue [ticks-left])
 
; A ReflexGameState (RGS) is one of:
; - (make-red Nat)
; - (make-blue Nat)
; and represents the current state of the game,
; either red or blue, and how many ticks are left
; to show in that state before switching to the other

Hint: one of the reasons to wrap big-bang programs in a main function is so that the output of big-bang can easily be used. Remember that big-bang is an expression whose evaluation can be passed to a function like any other, and that the main function’s body doesn’t have to start with a call to big-bang.

Exercise 5 Consider the following data definitions:
; A Posn is a (make-posn Number Number)
; and represents a 2D position
 
(define-struct circ [radius center])
(define-struct sq [side center])
 
; A Shape is one of...
; - (make-circ PosNumber Posn)
; - (make-sq PosNumber Posn)
; and represents a cirle with a radius and center
; or a square with side length and center
  • Complete the steps of the data design recipe for the given data definitions.

  • Design the function blink which is given a Shape as input and displays it on a screen using a purple outline. When a user clicks inside the shape, the function should flip to a solid visual (and clicking again flips back to outline). If the user clicks outside the shape, the point they clicked should become the new center of the shape (keeping the existing drawing mode). Your program must return the center of the shape when the user exits.

    Note that a point is inside a circle if the distance between the point and the circle’s center is less than the circle’s radius. A point is inside of a square if its x-coordinate lies between the square’s left and right edges and if its y-coordinate lies between the square’s bottom and top edges.

Exercise 6 Consider the following data definition:
(define-struct building [width height color right])
; A Skyline is one of:
; - false
; - (make-building PosInteger PosInteger String Skyline)
; and represents either an empty skyle or a building with
; a width and height in pixels, the color of the building, and the
; rest of the skyline to the right of the building
  • Complete the Design Recipe with respect to a Skyline.

  • Design the function draw-skyline, which draws a Skyline (hint: the beside/align function will be helpful here).

  • Design the function square-skyline?, which determines if every building in a Skyline is a perfectly square building.

  • Design the function doze-red, which removes all "red" buildings from a given Skyline.

  • Design the function grow, which takes a Skyline and a positive number and increases the height of each building by that amount.

  • Design the function skyline-height, which determines the height of the tallest building in a Skyline. If there is no building, the function should return 0.

Exercise 7 A planetary system has some number of planets, each with a name, distance from the host star (in astronomical units), and some number of moons, each of which has a relative ordering in distance from the planet and a mass (in 1016 kg). Design the function bigger-moon?, which determines if any planet in a system has a moon whose mass is greater than a supplied value.