EXERCISE SET #9

1. Define a Scheme procedure that takes a natural number as argument and returns a list containing the specified number of distinct three-letter strings in which the first letter is chosen randomly from the set bdfghjklmnprstvz, the second from the set aeiou, and the third from the set bdfgklmnprstvxz.

2. Suppose we represent a playing card as a pair in which the left field is one of the four symbols spade, heart, diamond, club and the right field is an integer in the range from 1 (= ace) to 13 (= king). Define a Scheme procedure fresh-pack of arity 0 that returns a vector of fifty-two distinct playing cards.

3. Let a vector of fifty-two distinct playing cards be called a pack. Here is a Scheme procedure that shuffles a given pack (destructively). Note that it is invoked for its side effect only:

(define shuffle!
  (lambda (vec)
    (let ((len (vector-length vec)))
      (do ((index (- len 1) (- index 1)))
          ((zero? index))
        (let* ((swap-slot (random (+ index 1)))
               (temp (vector-ref vec swap-slot)))
          (vector-set! vec swap-slot (vector-ref vec index))
          (vector-set! vec index temp))))))
Write a non-destructive version of this procedure. (That is, write a procedure that creates a new, shuffled pack containing the same cards as the given pack, without changing the given pack.)

4. Provide destructive and non-destructive implementations of the operation of cutting a pack of cards. (More formally: Define a Scheme procedure cut! that takes as arguments a vector vec and an integer n in the range from 0 to the length of the vector and rearranges the elements of vec so that the elements previously in positions greater than or equal to n are at the left end of the vector and those previously in positions less than n are at the right end. Within each segment, the elements should keep the same relative order. Then define a Scheme procedure cut that does not change vec, but instead constructs and returns a new vector with the same elements as vec, but arranged in the order described above.)

5. Define a Scheme procedure that takes a string as argument and returns a randomly constructed anagram of that string -- a new string containing the same characters, but randomly permuted.

6. The Twenty-One Bell three-wheeled slot machine, c. 1973, had twenty symbols on each wheel, as follows:

space   first wheel             second wheel            third wheel
------------------------------------------------------------------------
   1    orange                  cherry                  bell
   2    melon                   plum                    orange
   3    plum                    cherry                  plum
   4    cherry                  7 and orange            bell
   5    plum                    cherry                  orange
   6    orange                  bell                    lemon
   7    7                       plum and bar            bell
   8    bell and bar            bell                    melon and orange
   9    orange                  cherry                  bell
  10    cherry                  orange                  plum
  11    bar                     bell                    lemon
  12    plum                    melon and orange        bell
  13    orange                  plum                    plum
  14    plum                    bell                    bell
  15    melon                   cherry                  7 and bar
  16    plum                    bar                     lemon
  17    orange                  orange                  bell
  18    plum                    cherry                  melon and orange
  19    bar                     bell                    bell
  20    plum                    melon and orange        lemon
In some instances, two symbols appeared together in the window; in that case, either symbol could contribute to a paying combination.

Here is the table of payoffs for this machine:

double jackpot:         7       7       7       pays 200 coins
       jackpot:         bar     bar     bar          100
       jackpot:         melon   melon   melon        100
       jackpot:         melon   melon   bar          100
                        bell    bell    bell          18
                        bell    bell    bar           18
                        plum    plum    plum          14
                        plum    plum    bar           14
                        orange  orange  orange        10
                        orange  orange  bar           10
                        cherry  cherry  (any)          5
                        cherry  (any)   (any)          2
On each play, the sucker inserts one coin and throws the lever to start the wheels turning; if the machine is working correctly, each wheel is equally likely to stop in any of its twenty positions, and the three wheels turn independently of one another. (The source for my information is John Scarne, Scarne's new complete guide to gambling, New York: Simon and Schuster, 1974.)

Define a Scheme procedure that simulates the operation of the Twenty One Bell.

7. Compute the percentage of the total handle that the honest proprietor of a Twenty One Bell can expect to keep. Speculate on the rationale of the design of the playoff structure. Redefine the payoffs in your simulation so that they are fair, that is, so that the player's mathematical expectation is 0.

8. Define a Scheme procedure vector-every? that takes two arguments, a predicate pred? of arity 1 and a vector vec, and determines whether all of the elements of vec satisfy pred?.

9. A matrix can be represented in Scheme by a vector of vectors, each of the element vectors constituting one row of the matrix. Define a Scheme procedure identity that takes a positive integer n as its argument and returns an n-by-n identity matrix.

10. Define a Scheme procedure that constructs and returns the transpose of a given matrix.


created June 19, 1996
last revised June 21, 1996

John David Stone (stone@math.grin.edu)