CSC 533: Organization of Programming Languages
HW6: Scheme Programming
For this assignment, you are to define the following Scheme functions. For simplicity, place all of your function definitions in a single file named hw6.scm. Be sure to comment each function as to its behavior.
- Define each of the following functions for converting English and Metric units of length. For example, (in->cm 2) should evaluate to 5.08 since 1 inch is equivalent to 2.54 centimeters. Note that there are 12 inches in a foot, 5,280 feet in a mile, 100 centimeters in a meter, and 1,000 meters in a kilometer.
in->cm ft->m mi->km
cm->in m->ft km->mi
Note: the return value for all of these functions should be
- Define a function named wind-chill that takes two inputs, a temperature (in degrees Fahrenheit) and a wind-speed (in miles per hour), and returns the corresponding wind-chill factor. The formula for computing the wind chill is as follows, where a = 91.4, b = 10.45, c = 6.69, d = 0.447, and e = 22.0.
For example, (wind-chill 38 2) should evaluate to 38 since a
wind-speed of only 2 miles per hour has no effect on the temperature of
38 degrees Fahrenheit. Note: the return value for this
function should be inexact.
| wind-chill =
|| if wind <= 4
| a - (b + c*sqrt(wind) - d*wind)*(a - temp)/e
|| if wind > 4 and temp <= 45
| 1.6*temp - 55
|| if wind > 4 and temp > 45
- Define a function named num-occur that takes two inputs, an atom and a list, and returns a count of the number of times the atom appears in the list. For example, (num-occur 'x '(z x e x)) should evaluate to 2.
- Define a function named remove that takes two inputs, an atom and a list, and returns the list with each occurrence of the atom removed. For example, (remove 'x '(z x e x)) should evaluate to (z e).
- Define a function named subst that takes three inputs, two atoms and a list, and returns the list with each occurrence of the second atom replaced by the first. For example, (subst 'a 'x '(z x e x)) should evaluate to (z a e a).
- Define a function named prime? that takes one input, a positive integer, and returns #t if that number is prime, else #f. For example, (prime? 6) should evaluate to #f, while (prime? 7) should evaluate to #t. Your function should only utilize tail-recursion (perhaps a help function will be required).
- Define a function named change that takes one input, a positive integer representing some number of cents, and returns the number of ways that amount can be obtained using some number of quarters, dimes, nickels, and pennies. For example, (change 10) should return 4 since 10 cents can be obtained using 1 dime, 2 nickels, 1 nickel and 5 pennies, or 10 pennies.
Hint: think recursively when solving this problem. Given an amount X that is greater than 25 cents, there are two possible ways to obtain X. One uses a quarter, and so it remains to count the number of ways of obtaining X-25 using any combination of coins. If a quarter is not to be used, then it remains to count the number of ways of obtaining X using only coins smaller than a quarter. If you then add the counts for these two possibilities, you have the total number of possibilities for X.