________ _______ __ __ _______ ___
/ ____/ |/ / __ \/ / / / < / __ < / Spring 2019 Section 54
/ / / /|_/ / /_/ / / / /_____/ / / / / /
/ /___/ / / / ____/ /_/ /_____/ / /_/ / / Assignment 09
\____/_/ /_/_/ \____/ /_/\____/_/ Thursday, 18th April, 2019
ASSIGNMENT IS DUE AT THE BEGINNING OF NEXT THURSDAY'S CLASS.
=====================================================================================
Before you begin, press CTRL+ALT+T. This will bring up a terminal window, which is
a quick way to access your network directory. Once it's open, type:
mkdir asmts
cd asmts
mkdir asmt9
cd asmt9
When you save your work today (which you should do often), you should make sure that
you save it in the asmts/asmt9 directory. THIS IS IMPORTANT!
-------------------------------------------------------------------------------------
Please provide AT LEAST THREE examples of your function running.
Don't forget to use ASMT-HELPER.TXT.
Don't forget to use the PROBLEM function before each of your solutions.
------------------------------------------------------------------------------------
Problems may be found in the Problems document on the course website.
Remember to provide contracts for your functions even when they are in the
Problems Book.
Please use the PRINT-HW instruction to print out your homework. Full details
are on the Assignments page of the course website.
==========================================================================================
PROBLEMS HIGHLIGHTED WITH ASTERISKS ARE **COACH-FREE PROBLEMS**.
1. Define a function called COUNT-N-IN-K which satisfies the following contract:
;; ===================================================================
;; COUNT-X-IN-N-K
;; ===================================================================
;; Counts the occurrences of rolling X in N rolls of a K-sided die.
;; -------------------------------------------------------------------
;; INPUTS: X, a positive integer.
;; N, a positive integer.
;; K, a positive integer.
;; OUTPUT: The number of times X occurs in N rolls of a K-sided
;; die.
;; ===================================================================
You must use WHILE or DOTIMES to get credit for this answer.
2. Recall your solution to Problem 4 of Assignment 8, a version of Problem 15.6 which
uses LETREC to handle recursion.
Rewrite your solution to this problem to use iteration.
HINT: Which is better: WHILE or DOTIMES? Remember that one is open-ended and the
other has a built-in limit, so which is better here?
3. Consider Problem 16.18, CONSEQ-SUMS.
Write a function, CONSEQ-SUMS-ITER, which uses iteration to solve this problem.
HINT: Refer back to your previous solution to inform your work.
Which is better here, WHILE or DOTIMES?
4. A certain popular game uses rolls of combinations of dice to generate random outcomes.
Varying numbers of dice with different sides can be rolled all at once, and the sum
of those rolls is used to determine outcomes.
In this problem, you will be building up a small suite of functions whose purpose
is to roll different kinds of dice and then sum the total outcome.
*
(a) Define a function called ROLL-N-D-K which satisfies the following contract:
*
;; ===================================================================
;; ROLL-N-D-K
;; ===================================================================
;; Outputs a list containing N rolls of K-sided dice.
;; -------------------------------------------------------------------
;; INPUTS: N, a positive integer.
;; K, a positive integer.
;; OUTPUT: A list of N K-sided die rolls.
;; ===================================================================
You must use iteration to get full credit for this problem.
*
(b) Define a function called ROLL-EM which satisfies the following contract:
*
;; ===================================================================
;; ROLL-EM
;; ===================================================================
;; Outputs a list containing lists of N rolls of K-sided dice.
;; -------------------------------------------------------------------
;; INPUTS: LISTY, a list of lists, each sublist having 2 integers
;; greater than 1.
;; OUTPUT: A list of lists, each vector containing the die rolls
;; specified in the input lists.
;; ===================================================================
Here are some examples:
> (roll-em ((4 6) (5 20)))
((3 6 3 1) (19 8 10 3 20))
> (roll-em ((11 8) (4 12)))
((3 5 8 1 6 3 6 1 4 2 7) (12 3 2 1))
> (roll-em ((20 6)))
((1 3 6 6 6 4 1 1 2 3 4 4 5 2 2 6 3 5 4 5))
>
HINT: You can (and should) use a call to ROLL-N-D-K in your solution.
You will need to make a list to hold the lists of die rolls before you
start rolling dice. (Think 'accumulator'.)
Which would be better here, WHILE or DOTIMES?
(c) Define a function called ROLL-AND-SUM-DICE, which satisfies the following contract:
;; ===================================================================
;; ROLL-AND-SUM-DICE
;; ===================================================================
;; Outputs the sum of rolling a LOT of bunches of K-sided dice.
;; -------------------------------------------------------------------
;; INPUTS: LISTY, a LIST of lists, each sublist having two
;; integers greater than 1.
;; OUTPUT: The sum of all the dice rolled.
;; SIDE-EFFECT: We print the dice rolled, the outcomes, and the sum
;; for each set of dice as we go.
;; ===================================================================
Here are some examples:
> (roll-and-sum-dice ((4 6)))
(4 6) --> (4 2 4 3) sum = 13
13
> (roll-and-sum-dice #(4 6) (22 8)))
(4 6) --> (4 2 2 4) sum = 12
(22 8) --> (1 7 6 8 2 6 8 6 5 5 3 8 1 1 7 4 6 3 8 2 ...) sum = 105
117
> (roll-and-sum-dice ((12 4) (8 6) (5 8) (4 12)))
(12 4) --> (2 2 2 2 4 2 1 4 4 1 1 2) sum = 27
(8 6) --> (5 5 4 5 1 5 4 2) sum = 31
(5 8) --> (1 8 2 4 8) sum = 23
(4 12) --> (10 8 7 12) sum = 37
118
> (roll-and-sum-dice ((14 4) (12 6) (10 8) (8 10) (6 12) (4 20)))
(14 4) --> (1 3 1 1 4 4 3 1 4 3 3 4 3 3) sum = 38
(12 6) --> (2 5 4 3 6 4 2 6 3 3 1 4) sum = 43
(10 8) --> (6 3 3 8 1 2 8 3 3 4) sum = 41
(8 10) --> (4 3 1 3 1 3 9 1) sum = 25
(6 12) --> (12 3 3 7 3 5) sum = 33
(4 20) --> (19 11 8 16) sum = 54
234
> (roll-and-sum-dice ())
0
>
You must use iteration in your solution in order to get full credit.
------------------------------------------------------------------------------------------
Don't forget to submit your work using the SUBMIT101 command!
submit101 e-asmt9 asmt9
(If the name of your directory is different from 'asmt9', change 'asmt9' to whatever
the name of your directory is.)
==========================================================================================
==========================================================================================