CSC 533: Organization of Programming Languages
Spring 2018

HW4: 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-hw4.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 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:

    wind-chill = { temperature if windSpeed <= 3
    35.74 + 0.6215*temperature + (0.4275*temperature-35.75)*windSpeed0.16    otherwise
  2. Define functions for converting between square feet and square meters. Note that this is not a straightforward conversion. You must first calculate the square root of the area, convert that square root from one unit to the other, then square the result. Recall that there are 12 inches in a foot and 100 centimeters in a meter. For example, (ft^2->m^2 100) should evaluate to 9.290304, since the square root of 100 is 10, 10 feet converts to 3.048 meters, and 3.0482 is 9.290304. Conversely, (m^2->ft^2 9.290304) should evaluate to 100.0.

  3. The Mosteller Formula is often used by doctors and dieticians in estimating the surface area of a person's body given their height and weight. The formula is as follows, where height is assumed to be in centimeters, weight is in kilograms, and surface area is in square meters:
        area = (height * weight / 3600)1/2
    

    Define a function named surface-area-metric that takes two inputs, a person's height (in centimeters) and weight (in kilograms), and returns their surface area (in square meters). For example, (surface-area-metric 168 59) should evaluate to approximately 1.659.

  4. Define a function named surface-area-american that takes two inputs, a person's height (in inches) and weight (in pounds), and returns their surface area (in square feet). The functions you have previously written should prove useful here. For example, (surface-area-american 66 130) should evaluate to approximately 17.836.

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 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) (begin (display "position: ") (display position) (newline) (cond ((= (abs position) goalDist) #t) ((equal? (coin-flip) 'heads) (random-walk-help (add1 position))) (else (random-walk-help (sub1 position)))))) (random-walk-help 0))

    As is, this function displays each step in the walk and returns #t when the walk ends. Modify the function so that it keeps track of the number of steps in the walk and returns this value instead. Your modification should only utilize tail-recursion.