For this assignment, you are to define a collection of Scheme functions. For simplicity, place all of 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.
Recall the following formulas from your high school geometry class:
(area-rectangle 4 7)
should evaluate to 28.
area-square
function should call area-rectangle as opposed to duplicating the multiplication. For example, (area-square 6)
should evaluate to 36.
pi
is predefined in Scheme, so you do not need to provide your own approximation. For example, (area-circle 10)
should evaluate to 314.1592653589793.
(area-triangle 4 10)
should evaluate to 20.
(distance '(0 0) '(3 4))
should evaluate to 5.
(slope '((0 0) (2 4)))
should evaluate to 2.
(parallel? '((0 0) (2 4)) '((1 1) (3 5)))
should evaluate to #t.
(perpendicular? '((0 0) (2 4)) '((1 1) (-1 2)))
should evaluate to #t.
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)))))
dice-roll
that takes one input, the number of sides of a die, a returns the sum of two random die rolls. For example, the call (dice-roll 6)
should simulate rolling two 6-sided dice and return an integer from the range 2 through 12 (following the appropriate probability distribution). Likewise, (dice-roll 8)
should simulate rolling two 8-sided dice and return an integer from the range 2 through 16 (following the appropriate probability distribution).
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.
average-rolls
that takes two inputs, the number of die sides and the number of rolls to simulate. The function should simulate rolling a pair of dice the specified number of times and return the average of all those rolls. For example, the call (average-rolls 6 1000)
should simulate 1000 rolls of two 6-sided dice and return the average (as an inexact value). Since the number of rolls could be large, your function should utilize tail-recursion.
count-dice
that has an additional input identifying a particular roll total. The function should simulate the specified number of rolls and return the number of times that total was obtained. For example, the call (count-dice 6 1000 7)
should simulate 1000 dice rolls (of 6-sided dice) and return the number of times 7 was rolled. Since the number of rolls could be large, your function should utilize tail-recursion.