Vous êtes sur la page 1sur 2

# CS105 Lab 2

The purpose of this lab is for you to get practice in using the design recipe, including testing.
Make sure to use check-expect or check-within, as appropriate. Follow the instructions given
in the Assignment Style Guide.
Language level: Beginning Student.

1. Create the function middle-digit that consumes an integer (called nnn) between 100 and
999, inclusive, and produces the middle digit of nnn. For example, (middle-digit 345)
produces 4, and (middle-digit 803) produces 0. Hint: use quotient and remainder.

2. Create the function set-middle-to-zero that consumes an integer (called nnn) between 100
and 999, inclusive, and produces a number like nnn, except that the middle digit has been set
to 0. For example, (set-middle-to-zero 345) produces 305 and (set-middle-to-
zero104) produces 104. You may wish to use the function middle-digit that you just created

3. Create a function trim-string that consumes a string (called s) and a natural number (called
n), and produces a string with the first and last n characters from s removed. For example,
(trimstring "example" 2) produces "amp". You can assume that s will always contain at
least 2n+1 characters.

4. Create a function shipping-bill that determines the cost for shipping merchandise. It
consumes the handling charge (a fixed cost for a shipment of any size), the charge per
kilogram, the weight of a box in kilograms, and the number of identical boxes.

5. Create a function ring-volume that consumes the inner radius, outer radius, and thickness of
a ring and produces its volume.

6. The airport parking lot has rates by the week and by the day, where you pay by the weekly
rate of \$74.95 for each complete week (any consecutive seven days) and by the daily rate of
\$14.95 for any remaining days. Create a function airport-parking that consumes an
integer number of days and produces the bill. Be sure to use constants where appropriate.
Note: Do not worry if you produce a number that looks like 5 instead of 5.0, or a number that
looks like 5.1 instead of 5.10.

7. Write a function sum-in-string that consumes two numbers, and produced the sum of these
two numbers, as shown below:
(sum-in-string 3 5) "3 + 5 = 8"
(sum-in-string 3.5 1.5) "3.5 + -1.5 = 2"
You may find it convenient to use the function number->string.

8. In this question you should be making use of several helper functions. Create a function film-
choice that produces a string indicating the costs of creating a film based on two pricing
options. The function film-choice consumes the number of clips, the length of the film, and
information for two options. Each option is given as a breakpoint (the number of clips
charged at the full cost of \$100 each) and a discount (the fraction by which the cost of a clip
is reduced, for each clip past the breakpoint). For each option, the total cost is the sum of the
charge for the clips and a charge based on the length of the film (at a cost of \$100 per
minute).
For example, suppose the number of clips is 10, the length is 30 minutes, and for the first
option the breakpoint is 5 and the discount is 0.25. Then the total cost for this option will be
the cost for the length (30*100) plus the cost for 5 clips at \$100 each and the cost for 5 clips
at \$75 each, or a total of \$3875. The total of \$3900 would be calculated in a similar fashion
for the second option, breakpoint 8 and discount 0.5. The string produced by film-choice for
total costs of \$3875 and 3900 will be:
"Cost for option 1 is 3875 and cost for option 2 is 3900"
You may find it convenient to use the function number->string.

9. Write a function called calc-bmi that will calculate a person's body mass index (BMI) based
on their height in feet and inches and their weight in pounds. A person's BMI can be
calculated as follows: mass/height2, where the mass is measure in kilograms and the height is
measured in metres. However, many people in North America only know their height in feet
and inches and their mass in pounds. For example they might say 5 feet, 6 inches tall and 150
pounds. Your function will consume a height in feet, a number of inches given as an integer
between 0 and 11 inclusive, and a mass in pounds. Use the approximate values of 2.2 pounds
for one kilogram, and 2.54 centimetres for one inch. Use the actual values of 12 inches per
foot and 100 centimetres per metre. For example, (calc-bmi 5 6 150) will produce
approximately 24.26126.

## Hint for Questions 7 & 8

(+ 1 5) --> 6
(+ 1 "5") --> Error "5" is string, not number!
(+ 1 (string->number "5")) --> 6

## (string-append "Hello " "There!") --> "Hello There!"

(string-append "Formula " 1) --> Error!
(string-append "Formula " (number->string 1)) --> "Formula 1"