CSC 533: Organization of Programming Languages
Spring 2023

HW5: Scheme Programming I


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

Baseball Stats

Consider the following baseball statistics:

    batting-avg = hits / at-bats
    
    on-base% = (hits + walks + hits-by-pitch) / (walks + hits-by-pitch + sac-flies + at-bats)
    
    total-bases = singles + 2*doubles + 3*triples + 4*home-runs
    
    slugging% = total-bases / at-bats
    
    OPS = on-base% + slugging%
    
  1. Define a function named batting-avg that takes two inputs, the number of hits and at-bats (in that order), and returns the batting average. For example, (batting-avg 72 200) should evaluate to 0.36.

  2. Define a function named on-base% that takes five inputs, the number of hits, walks, hits-by-pitch, sacrifice flies and at-bats (in that order), and returns the on-base percentage. For example, (on-base% 72 10 3 4 200) should evaluate to 0.39170...

  3. Define a function named total-bases that takes four inputs, the number of singles, doubles, triples and home-runs (in that order), and returns the total bases. For example, (total-bases 40 20 8 4) should evaluate to 120.

  4. Define a function named slugging% that takes five inputs, the number of singles, double, triples, home-runs and at-bats (in that order), and returns the slugging percentage. For example, (slugging% 40 20 8 4 200) should evaluate to 0.6.

  5. Define a function named OPS that takes eight inputs, the number of singles, doubles, triples, home-runs, walks, hits-by-pitch, sacrifice-flies and at-bats (in that order), and returns the on-base+slugging percentage (OPS). Note: hits = (singles + doubles + triples + home-runs). For example, (OPS 40 20 8 4 10 3 4 200) should evaluate to 0.99170...

List Processing

Consider the following recursive function, which counts how any times a particular number appears in a list of numbers.

    (define (count-occur num numlist)
      (cond ((null? numlist) 0)
            ((= num (car numlist)) (+ 1 (count-occur num (cdr numlist))))
            (else (count-occur num (cdr numlist)))))
            
  1. Define a similar function named count-between that takes three inputs, the low and high numbers of a range and a list of numbers, and returns a count of numbers from the list that fall within the specified range (inclusive). For example, (count-between 5 10 '(1 4 7 12 5 10 19)) should evaluate to 3.

  2. Define a function named remove-occur that takes two inputs, a number and a list of numbers, and returns a copy of the list with all instances of the number removed. For example, (remove-occur 5 '(5 1 4 7 12 5 5 10 19 5)) should evaluate to (1 4 7 12 10 19).

  3. Define a similar function named remove-between that takes three inputs, the low and high numbers in a range and a list of numbers, and returns a copy of the list with all numbers from the specified range removed. For example, (remove-between 5 10 '(5 1 4 7 12 5 5 10 19 5)) should evaluate to (1 4 12 19).

  4. Define a function named roman->num that takes one input, a list of symbols 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.

  5. Define a function named num->roman that performs the reverse conversion, from a number to a list of symbols 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

The following function simulates rolling a die with a specified number of sides:

    (define (die-roll num-sides)
      (+ 1 (random num-sides)))
      
  1. Define a function named pair-roll that takes one input, the number of sides on a die, and returns the sum of two dice rolls. For example, (pair-roll 6)
  2. should simulate rolling two six-sided dice and return their sum (a number between 2 and 12, with 7 being the most likely).

  3. Define a function named count-roll that utilizes the pair-roll function to simulate rolling two six-sided dice a specified number of times, keeping track of the number of times a particular total was rolled. For example, the call (count-dice 7 10000) should simulate 10,000 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. Define a function named percent-roll that utilizes the count-roll function to determine the percentage of six-sided dice rolls that match a specific total. For example, the call (percent-roll 7 10000) should simulate 10,000 dice rolls and return the percentage of times 7 was rolled (e.g., 16.66).