$29
• All Finger Exercises on this homework should be done individually (without a partner) and you should have them done by Monday night. Finger exercises will not be graded and do not have to be turned in.
• All Graded Exercises on this homework should be done with the partner you're assigned in lab on Tuesday Sept 15.
• For this and all future assignments you 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.
• On this assignment, you are encouraged to write signatures and purpose statements for every function you write, in the format we studied in class; we will give you feedback on how clear or accurate they are. On future homeworks, they will be required and graded as part of the assignment. As for check-expects: follow the individual instructions for each problem.
Failure to comply with these expectations will result in deductions and possibly a 0 score.
Finger Exercises You are not required to submit your finger exercises but they will be helpful so we recommend doing them anyway.
Exercise 1 Write a function that subtracts 2 from a number.
Exercise 2 The following table describes how far a person has gone in a race in a certain amount of seconds:
t =
1
2
3
4
5
6
7
8
9
10
d =
3
4.5
6.0
7.5
9.0
10.5
12.0
13.5
?
?
Write a function that predicts, based on this data, how far they will have run at time t. Write three check-expects: two that test to see the data in the table matches your function’s output, and then one more that tests the output when t is at least 9.
Exercise 3 Take a look at figure 1, which is a graph of f, a function of x.
Turn the graph into a table for x = 0, 1, 2 and 3 and formulate a function definition.
Figure 1: A function graph for f
Exercise 4 Translate the following mathematical function into a BSL function:
f(x) = x2 + 12
Use it to create a table for x = 0, 2, 5, and 9.
Exercise 5 Enter the following function definition in BSL into DrRacket:
(define (math-is-boring x)
(+ (* -4 (expt x 3)) (* 8 (expt x 1)) (* 9 (expt x 0))))
Look up the documentation for expt.
Apply the function to 0, 1, and 3 in the interactions area.
Apply the function to 1 in the definitions area and use the stepper to see how DrRacket evaluates this program.
Exercise 6 Enter the following function definition in BSL into DrRacket:
(define (hello x)
(cond
[(string=? "Ben" x) (string-append "Dear " x ", Esquire:")]
[(string=? "Leena" x) (string-append "Dear " x ", Esquirette:")]
[else (string-append "Greetings, " x ", ")]))
What kind of argument does hello consume? Apply the function to your favorite argument and step through the evaluation.
Exercise 7 Design the function render-string, which consumes a number t and produces a text image of the first t letters from the string "qwerty".
Place the text on a white 200 x 100 rectangle. Use black text of font size 22.
Graded Exercises
Exercise 8 Write a function, nineply, that multiplies a number, supplied as an argument, by 9. Write three check-expects: one for a negative value, another for zero, and a third for a positive value.
Exercise 9 Complete the following table:
t =
0
1
2
3
4
5
6
7
d =
FUNDIES
FUNDIE
FUNDI
FUND
FUN
FU
?
?
Turn this table into a function definition. In a comment before the function, give as precise a signature for this function as you can (hint: it’s not quite straightforward – you may need to describe it rather than use the notation from class).
Exercise 10 Implement the function, cycle-spelling, so that when (animate cycle-spelling) is called, it animates spelling the following long word, letter-by-letter (in all caps). When the end of the word is reached, it cycles back and starts from the beginning.
(define LONG-WORD "diScomBoBUlaTeDiScomBoBulaTeDboBUlaTeD")
The word should be displayed in blue in font size 40 on a white background.
Hints:
• Your code ought to work no matter what string is used when defining LONG-WORD, without needing to change anything else.
• One way to count 0 up to some number and then loop back is to think about dividing two numbers and taking the remainder; so the remainder of 1 / 3 is 1, the remainder of 2 / 3 is 2; and the remainder of 3 / 3 is back to 0. Look for the remainder function in BSL.
• You may want to look at the documentation at https://docs.racket-lang.org/htdp-langs/beginner.html for some of the functions available to you in BSL.
Exercise 11 Design a function, draw-kite, that produces a kite with 4 colors when given the desired width and height of the kite. We have provided two check-expects to help you test your function in the starter file for this assignment.
• The kite should have the following four colors, starting with the top left quadrant going clockwise: "blue", "yellow", "green", "red".
• The widest point of the kite is at one-third of the distance from the top and two-thirds from the bottom.
Exercise 12 Write a function, convert-to-inches, that takes as arguments a number of yards, feet, and inches (in that order) and converts to a total number of inches. For example, (convert-to-inches 4 1 6) would produce 162 inches. Write two check-expects: one expressing the example above, and a different one of your choosing.
Exercise 13 Design the function speed-check. It consumes two natural numbers: one that represents the speed of a car and the other one the speed limit of the road. The result is one of these strings: (1) "okay" for a car that goes below the speed limit; (2) "bit fast" for a car that is going at most 9 mph faster than the speed limit; or (3) "speeding: ___ mph over limit" for a car that exceeds the speed limit, where the underlines are replaced by how much the car is over the speed limit. Write five check-expects that test the function’s behavior in different scenarios.