CSC 533: Programming Languages
Spring 2012

HW5: 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 hw4.scm. Be sure to comment each function as to its behavior.

  1. Define functions for converting between inches and centimeters, and between pounds and kilograms (and vice versa for each). Recall that 1 inch = 2.54 centimeters and 1 pound = 0.45359237 kilograms.

    For example, (in->cm 10) should evaluate to 25.4, while (kg->lb 10) should evaluate to 22.0462262.

  2. Body Mass Index (BMI) is often used by doctors and dieticians in assessing the overall health of a patient. The formula is as follows:
        BMI = (weight in kilograms) / (height in meters)2
    

    Define a function named BMI-Metric that takes two inputs, a person's height (in centimeters) and weight (in kilograms), and returns his/her BMI. For example, (BMI-Metric 170.0 70.0) should evaluate to approximately 24.22.

    Define a function named BMI-American that takes two inputs, a person's height (in inches) and weight (in pounds), and returns his/her BMI. For example, (BMI-American 65.0 170.0) should evaluate to approximately 28.29. Note: you should be able to make use of the BMI-Metric function here.

  3. According to the Center for Disease Control, body mass index correlates to the following weight categories:

    BMI < 18.5underweight
    18.5 ≤ BMI < 25.0normal
    25.0 ≤ BMI < 30.0overweight
    30.0 ≤ BMI obese

    Modify your BMI-Metric and BMI-American functions so that instead of returning a single number, they each return a list containing the BMI and the weight status. For example, (BMI-Metric 170.0 70.0) should evaluate to approximately (24.22 normal), while (BMI-American 65.0 170.0) should evaluate to approximately (28.29 overweight).

  4. Define a function named leap-year? that takes one input, a positive integer representing a year, and returns #t if it is a leap year (otherwise, #f). In general, years that are evenly divisible by 4 are leap years, unless they are divisible by 100 but not 400.

    For example, (leap-year? 2008) and (leap-year? 2000) should evaluate to #t, but (leap-year? 2009) and (leap-year? 2100) should evaluate to #f.

  5. Define a function named days-in-year that takes one input, a positive integer representing a year, and returns the number of days in that year.

    For example, (days-in-year 2008) should evaluate to 366 since it is a leap year, but (days-in-year 2100) should evaluate to 365 since it is not.

  6. Define a function named total-days that takes two inputs, the starting and ending years in a range, and returns the total number of days in that range. You may assume that both inputs are positive integers, and that the first input is less than or equal to the second.

    For example, (total-days 2008 2010) should evaluate to 1096 since the years in the range consist of 366, 365, and 365 days, respectively.

  7. Define functions for converting roman numerals to numbers and back. One function, roman->num will have one input, a list of characters representing a roman numeral. It should return the number value represented by that roman numeral. For example, (roman->num '(X V I)) should evaluate to 16. Conversely, the function num->roman will have an integer as input, and should return the list of characters representing the appropriate roman numeral. 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 old 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.

  8. The following function simulates a random 1-dimensional walk. Initially, the walker is assumed to be at position 0. Depending on a random value, 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 (random-walk goal-dist) (define (random-walk-help position goal) (display position) (newline) (cond ((= (abs position) goal) #t) ((zero? (random 2)) (random-walk-help (add1 position) goal)) (else (random-walk-help (sub1 position) goal)))) (random-walk-help 0 goal-dist))

    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.