EXERCISE SET #5

1. Define a Scheme procedure that computes and returns the standard deviation of any non-empty list of real numbers.

2. Define a Scheme procedure that takes two lists of symbols, equal in length, and determines the number of positions at which they contain different values. For instance, given the lists (a b c e f g) and (a b d e g h), the procedure should return 3. Use a named let-expression as the body of the procedure and ensure that it computes the result by an iterative process.

3. Let's dress up the preceding exercise a little by making it a component of an experiment in short-term memory. Suppose that the symbols in the first list are random letters or nonsense syllables that the subject is supposed to memorize during a short exposure, and those in the second list are the subject's report of what she remembers from that exposure. The Scheme procedure is supposed to evaluate how well the response was remembered.

For this purpose, the Scheme procedure defined in exercise 2 does a poor job, since it imposes just as great a penalty for mispositioning a symbol as for forgetting it entirely and substituting a completely different symbol. The exercise is to propose a better scoring method and to implement it as a Scheme procedure.

4. Define a Scheme procedure shuffle that takes two lists as arguments and returns a single list, the elements of which are drawn alternately from the two given lists as long as they both hold out, after which the rest of the elements are the same as those of the longer of the given lists. For instance, the value of the call (shuffle '(a b c d e) '(x y z)) is (a x b y c z d e).

5. Define a Scheme procedure merge that takes two lists of real numbers, both in ascending order, and returns a single list, containing all the elements from both lists, also in ascending order.

6. 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 predicate that determines whether a given list of five cards, considered as a poker hand, is a flush (that is, whether they are all of the same suit). Extend the exercise, if you like, to include predicates that test for other holdings.

7. Using the random procedure in SLIB or in your preferred Scheme implementation, define a Scheme procedure of arity zero that constructs and returns a randomly dealt five-card poker hand. (It's a little harder than it may look, since you can't construct each card independently. If your procedure deals out a hand with two aces of spades in it, it will be shot.)

8. The setting this time involves a word game in which each letter has a particular numerical value:

                  a, e, i, l, n, o, r, s, t, u:  1 point
                                          d, g:  2 points
                                    b, c, m, p:  3 points
                                 f, h, v, w, y:  4 points
                                             k:  5 points
                                          j, x:  8 points
                                          q, z: 10 points
Define a Scheme procedure that takes any word and determines the total of the values of its letters. (This is pretty easy if you represent a word as a list of letters, so for a little more of a challenge you might want to take this opportunity to become familiar with Scheme's string and character types by supposing that the argument will be presented as a string.)

9. Define a Scheme procedure that takes a list of pairs, each pair consisting of a symbol (either male or female) and a real number (representing a subject's reaction time on a particular task), and returns a list of two pairs, one consisting of the symbol male and the mean reaction time of all the male subjects, the other consisting of the symbol female and the mean reaction time of all the female subjects. If the given list of pairs contains no data for a sex, the corresponding entry in the result should have the symbol no-data in its right field instead of a number.

10. Suppose we represent a point in the Cartesian plane by a pair of real numbers, the car representing the point's x-coordinate and the cdr its y coordinate. Suppose further that we represent a line segment by a pair of which the two fields are the two endpoints of the segment.

Define a Scheme predicate that takes two line segments, one horizontal (i.e., both of its endpoints have the same y-coordinate) and the other vertical, and determines whether they intersect (that is, whether they have at least one point in common).


created June 13, 1996
last revised June 14, 1996

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