[previous] [up] [next]     [index]
Next: Alternative Data Definitions for Up: Natural Numbers Previous: Processing Natural Numbers of

Extended Exercise: Creating Lists, Testing Functions

We often encounter situations where we would like to create lists of data that involve namers. For example, we may wish to create large lists of numbers to test a function like extract1 in section [cross-reference] on large lists instead of hand-coded small ones. Sometimes we would like to visualize randomly picked data. We can create such functions using recursion on natural numbers and a random number generator.


Exercises

Exercise 11.3.1

Scheme provides the operation random. It consumes a natural number n greater than 1, and produces a random integer between 0 and n - 1:

;; random : N -> N
;; to compute a natural number between 0 and n-1 
(define (random n) ...) 
Two successive uses of (random n) may produce two distinct results.

Now consider the following definition:

;; random-n-m : integer integer -> integer
;; ... 
;; Assume: n < m 
(define (random-n-m n m) 
  (+ (random (- m n)) n)) 
Formulate a succinct and precise purpose statement for random-n-m. Use a number line with an interval to explain the result of (random n). Use a symbolic evaluation to support your explanation. Solution

Exercise 11.3.2

Develop the function tie-dyed. It consumes a natural number and produces a list of numbers. Each of these should be between 20 and 120. Use tie-dyed to test draw-circles from exercise [cross-reference]Solution

Exercise 11.3.3

Develop the function create-temps. It consumes a natural number n and two integers, called low and high. It produces a list of n integers that are between low and high.

Use create-temps to test check-range from exercise [cross-reference].

Finally, discuss the following questions. Can we simply feed the result of create-temps into check-range or do we need to know the list that create-temps produced? Are there values for low and high such that we don't need to know the result of create-temps and yet we can predict the result of the test? Which function tests which? What does this tell us about testing with automatically generated test data? Solution

Exercise 11.3.4

Develop the function create-prices, which consumes a natural number and produces a list with a corresponding of prices between $.10 and $10.00 with increments of a dime. Use create-prices to test dollar-store? from exercise [cross-reference].

Hint: How many dimes are there between $.10 and $10.00? Solution

Exercise 11.3.5

Develop a program that visualizes a student riot. In preparation of a student riot, a small group of students meets to make paint-filled balloons. The typical riot uses RED only. Then, on the evening of the riot, the students enter a university's progressive theater with the balloons and throw them all over the seats.

The program's only input should be a natural number, which represents the number of balloons thrown. The visualization should use a canvas that contains a black grid and the positions of the balls:

Assume a random distribution of the balls over the theater's seats. Each box in the grid represents a seat. Configure the program so the change of one variable definition changes the number of columns in the grid and a change to another changes the number of rows.

Hint: Develop auxiliary functions that draw some given number of lines in the vertical and the horizontal direction. Solution



[previous] [up] [next]     [index]
Next: Alternative Data Definitions for Up: Natural Numbers Previous: Processing Natural Numbers of

PLT