CSC 533: Organization of Programming Languages
Spring 2020

HW5: Scheme Programming I


For this assignment, you are to define the following Scheme functions. For simplicity, place all of your function definitions in a single file named YOURNAME-hw5.ss, where YOURNAME is your name. Be careful to name your functions exactly as defined in the exercises, and be sure to comment each function as to its behavior.

Numerical Functions

  1. Define a function named dew-point that takes two inputs, a dry temperature (in degrees Fahrenheit) and the relative humidity (between 0 and 100%), and returns the corresponding dew-point, the temperature at which water would condense. The formula for computing the dew-point is:

    dew-point = temp - (100 - humidity)/2.778
  2. Define a function named cloud-base that takes two inputs, a dry temperature (in degrees Fahrenheit) and the relative humidity (between 0 and 100%), and returns the corresponding cloud base, the altitude (in feet) at which clouds form. The formula for computing the cloud-base is:

    cloud-base = 1000 * (temp - dew-point)/4.4
  3. 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 (in degrees Fahrenheit). The formula for computing the wind-chill is:

    wind-chill = { temperature if windSpeed <= 3
    35.74 + 0.6215*temperature + (0.4275*temperature-35.75)*windSpeed0.16    otherwise
  4. Now suppose that you want to convert your weather-related functions to the metric system. That is, you would like to have a dew-point-metric function that takes its temperature in degrees Celsius and returns the dew-point in degrees Celsius. Likewise, cloud-base-metric would take its temperature in degrees Celsius and return the cloud-base in meters. Finally, wind-chill-metric would take its temperature in degrees Celsius and its wind-speed in km/hr and return the wind-chill in degrees Celsius. Define functions for doing the necessary metric conversions (e.g., ft->m, fahr->celsius) and use them along with your existing functions to define the new metric-based weather functions.

List Functions

  1. Define a function named remove-all that takes two inputs, an atom and a list, and returns the list with each occurrence of the atom removed. For example, (remove-all 'x '(z x e x)) should evaluate to (z e).

  2. Define a function named replace-all 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, (replace-all 'a 'x '(z x e x)) should evaluate to (z a e a).

  3. Define a function named roman->num that takes one input, a list of characters representing a roman numeral, and returns the number value represented by that roman numeral. For example, (roman->num '(X V I)) should evaluate to 16. The following is a list of the roman letters and numbers they represent: M = 1000, D = 500, C = 100, L = 50, X = 10, V = 5, I = 1.

    You may assume the ancient roman style of writing letters, where 4 is represented IIII and 90 is represented LXXXX. A harder problem, which you may attempt if you like, is to use the modern roman style where 4 is IV and 90 is XC.

  4. Define a function named num->roman that performs the reverse conversion, from a number to a list of characters representing a roman numeral. For example, (roman->num 66) should evaluate to (L X V I). Again, you may assume the ancient roman style of writing letters.

Simulations

  1. Define a function named dice-roll that has no inputs and simulates the roll of two six-sided dice. That is, the call (dice-roll) should return an integer from the range 2 through 12, following the appropriate probability distribution (e.g., 7 is the most likely roll, 2 and 12 are the least likely). Hint: The built-in random function generates a pseudo-random integer from 0 up to its input (exclusive). For example, the call (random 4) would return either 0, 1, 2, or 3.

  2. Define a function named average-rolls that takes one input, a positive integer, and simulates that many rolls of the dice, returning the average of all of those rolls. For example, the call (average-rolls 1000) should simulate 1000 dice rolls and return the average. Since the number of rolls could be large, your function should utilize tail-recursion.

  3. Define a function named count-dice that takes two inputs, a number of rolls and the desired total. The function should simulate the specified number of rolls and return the number of times the desired total was obtained. For example, the call (count-dice 1000 7) should simulate 1000 dice rolls and return the number of times 7 was rolled. Since the number of rolls could be large, your function should utilize tail-recursion.

  4. The random-walk function below simulates a random 1-dimensional walk. Initially, the walker is assumed to be at position 0. Depending on the flip of a coin, the walker either moves to the right (in the positive direction) or to the left (in the negative direction). The simulation ends when the walker reaches the specified goal distance, in either direction. (define (coin-flip) (if (= (random 2) 0) 'heads 'tails)) (define (random-walk goalDist) (define (random-walk-help position) (cond ((= (abs position) goalDist) position) ((equal? (coin-flip) 'heads) (random-walk-help (add1 position))) (else (random-walk-help (sub1 position))))) (random-walk-help 0))

    As is, this function simulates steps in the walk and returns the final position of the walker. Modify the function so that it keeps a list of all the steps in the walk and returns this list instead (with starting position 0 at the front and the final position at the end). Your modification should only utilize tail-recursion.