8.3

Homework 2

home work!

Programming Language BSL

Due Date Tuesday 9/21 at 9:00pm (Week 2)

Purpose To write simple functions relating to numbers, strings, images, and conditionals; practice function design, including with unions and structs; and prepare for the exam.

Finger Exercises

Exercise 1 From HtDP, 48 49 50 55.

Exercise 2 Do these after Wednesday lecture: From HtDP, 64 65 84

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

  • You are only allowed to use the language specified at the top of this page: failure to do so will result in a 0.

Graded Exercises

Note: For Exercises 3 to 6, we simply ask you to "write" a function (which means you do not have to follow the design recipe, but you may). From Exercise 7 onwards, we ask you to "design" a function or data (which means you must follow the design recipe).

Exercise 3 Now that Brexit has happened, it is far more common for Britons traveling to Europe to need to exchange Pounds Sterling for Euros. A currency-exchange service provides the following chart...

Notice that there is a flat fee charged for service as well as a conversion rate for each Pound -> Euro. For example, anything less than 5 Pounds gets eaten by the service fee, but 10 Pounds gets you 4 Euros.

Write a function, gbp->eur, to convert from Pounds to Euros. Write four check-expects: two match the examples described above, one that is shown in the graph, but not described, and one that is a value greater than those shown in the graph. Note: one of these must result in a non-integer for credit.

Hint: it can be useful to write out a table of input/output pairs to help understand these types of mathematical relationships (and these can then provide great tests!)

Exercise 4

This problem is inspired by common abbreviations in web-development, where "Accessibility" is abbreviated "A11y", and "Localization" is abbreviated "L10n".

For any word of at least two characters, let’s say that its “encoding” is the uppercase version of its first letter, followed by the number of characters in the middle of the word, followed by the uppercase version of the last letter of the word. For example, the encoding of "potter" is "P4R", and the encoding of "Northeastern" is "N10N".

Write a function, encoding, that takes a string as an argument and returns its encoding. You may assume that the argument is a valid word as described above. Write three check-expects: at most two of the examples above, as well as one of your choosing: be creative, but respectful :)

Hint: The String function documentation is a useful place to look for functions that operate on Strings.

Exercise 5 Write the function leap-year? which determines if a natural number represents a leap year in the Gregorian calendar.

Write four check-expects and make sure that once these tests are successful, no parts of your function are highlighted in halloween colors.

Exercise 6 You’ve been tasked to produce an animation of a red ball of radius 25 that moves from top to bottom on a 50 by 250 background, hits the bottom edge of the background, then goes back up, bounces off the top edge and so on, down and up. The background should be yellow whenever the ball is moving down and it should be orange whenever the ball is moving up.

Write a function down-and-up that consumes the amount of time that has elapsed since the start of the animation, in clock ticks, and produces an image of the ball on the appropriate position and backround. Then use (animate down-and-up) to run the animation.

The animation should start with:

with the ball moving down:

until it hits the bottom edge:

and starts moving up:

eventually hitting the top edge:

and starting to move down again:

and so on.

Be sure to make use of constants to simplify your code and to have a single point of control over constant values mentioned in the problem statement.

Let’s Shuffle

; A DSG (DiceShuffleGame) is a (make-dsg MaybeDice MaybeDice MaybeDice)
(define-struct dsg [left middle right])
; and represents the three cups in a dice shuffle game, and what is under them
 
; A MaybeDice is one of:
; - #false
; - Number
; and represents either no dice or the sum of die values
 
; A Guess is one of:
; - "left"
; - "middle"
; - "right

Exercise 7 Provide examples and templates for the above data definitions. Recall that when data definitions refer to other complex data types, the template must reflect this by calling the appropriate template.

Exercise 8 Design a function, shuffle-right, which moves all cup values in a DSG to the right. The right cup’s contents should loop back to the left cup.

Exercise 9 Design a function, which given a DSG and a Guess, outputs the value of the dice of the guessed cup. Not having a die is worth 0.

Exercise 10 Design the function extra-roll, which given a DSG and a number, adds that dice value to all of the dice in the cup, and leaves empty cups as is.

Cowabunga!

In many parts of the world, UFOs have been abducting cows. This should not surprise you, as you are a UFO pilot who has been sent on just such a mission. As you have probably been warned, however, the base of your UFO is made of a special alloy that cannot be allowed to come in contact with the ground. Your task, therefore, is to land your UFO atop a cow without crashing on Earth... which, as you may have heard, can turn you into a participant in certain "experiments".

Here’s how the game works:

In this assignment, we will begin working on some of the data definitions and logic of the game. We will finish it and integrate it into big-bang next assignment.

Exercise 11 Design a data definition for UFOs and Cows. A UFO has an X and a Y coordinate. A Cow has an X coordinate and is either going left or right.

Exercise 12 Design a function that moves a UFO down by some fixed amount.

Exercise 13 Design a function that moves a UFO either left or right by some fixed amount.

Exercise 14 Design a function that moves a Cow in the direction it is going by some fixed amount.

Exercise 15 Design a function that determines if a Cow is at the edge of the screen.

Exercise 16 Design a function that flips the direction of a Cow.

Exercise 17 Design a function that moves a Cow in the direction it is going, unless it at the edge, in which case, the Cow should flip directions, and then move.

Exercise 18 Design a function that determines if a UFO has captured a Cow.

Exercise 19 Design a function that determines if a UFO has crashed on the ground.

Exercise 20 Design a function that determines if the game is over.