Assignment #3

CS 152
due November 1, 2005
100 points

Give Scheme definitions for each of the functions that are specified below, and test them with the 0-argument test function of the test file a3test.scm that is available on the class web site. If you cannot define a function correctly, have it return a dummy value so that the test function will work correctly.

You needn't check for illegal input in any of your functions. Your recursive functions needn't be tail recursive.

  1. (10 points) Define a predicate letter-initial? that takes a string as input and returns #t iff the input string begins with a capital or lower-case letter.

  2. (10 points) Define a function update-alist that will take a symbol, a function of one variable, and an association list, and return an association list that associates the given symbol with the result of applying the function to its old value, and preserves all other associations of the input list. Recall that you needn't remove the old assocation from the list. If the name isn't in the association list, then leave the list unchanged.

  3. (15 points) Use define-struct to define a name type with components for first name, middle name, and last name. Also define a predicate name-ci<? that takes two names and returns #t iff the first precedes the second in lexicographical order. In your lexicographical ordering, last name should have precedence over first name, which should have precedence over middle name. You may assume that all three components are strings. Your string comparisons should be case-insensitive.

  4. (15 points) Define a function insertion-sort takes a list and a comparison predicate for list elements, and sorts the input list using that predicate. That is, you should return a sorted version of the input list. You may assume that the comparison predicate is appropriate for all pairs of list members.

  5. (25 points) Define two functions remove-duplicates and remove-old-associations. The first takes an input list, and returns the second and subsequent occurrences of all (top-level) members of the list. More precisely, it returns a new list that is equal to the input list, except that each element appears only once, and each element appears in the new list in a position corresponding to its first appearance in the old list.
    The second function works analogously -- it takes an association list, and returns a new list from which the second and subsequent associations for each first component have been removed. You may want to consider using list reversal.

  6. (25 points) Define a function tokenize that takes an input string, and returns a list of the string tokens in the string. Here the tokens are defined to be substrings delimited by characters satisfying the char-whitespace? predicate. The reversal trick of the previous problem might be helpful here as well.

    Examples for most of these functions are given below:

    > (letter-initial? "%g;")
    #f
    > (letter-initial? "PI")
    #t
    > (update-alist 'c add1 '((a . 11) (b . 12) (c . 13)))
    ((c . 14) (a . 11) (b . 12) (c . 13))
    > (name-ci<? (make-name "john" "quincy" "adams") (make-name "john" "" "adams"))
    #f
    > (insertion-sort (string->list "Math 42") char<?)
    (#\space #\2 #\4 #\M #\a #\h #\t)
    > (remove-duplicates '(c a b c))
    (c a b)
    > (remove-old-associations '((c . 14) (a . 11) (b . 12) (c . 13)))
    ((c . 14) (a . 11) (b . 12))
    
    As always, you are to turn in both hard and soft copies of all of your code (but not the test file, unless you modify it. You are also to turn in a hard copy of the results of your testing.