Assignment #3

CS 152
due October 31, 2006
100 points

 

Define each of the functions below, in Scheme. So that the test function will run without crashing, do not leave any functions undefined -- if you can't construct a good definition for a function, have it return a dummy value.

 

  1. (5 points) Define a function quadratic that takes a list of three coefficients of a quadratic polynomial (the leading coefficient first) and returns a list of the two zeros of the polynomial. Don't worry if the zeros are equal or complex. Recall that the quadratic formulas gives the zeros as
    [-b ± √(b2-4ac)] / 2a
    You needn't check for illegal input, including the case where the leading coefficient is 0.

  2. (10 points) Define a function has-whitespace? that takes a string as argument, and returns #f if the string contains no whitespace characters. You may return any value other than #f if it does. You needn't check whether the argument is a string.

  3. (15 points) Define a function remove-noncharacters? that takes an input list and returns the list obtained by removing every noncharacter from the list. You needn't check for illegal input.

  4. (15 points) Define a function clean-alist that takes as its only argument an association list, and removes all superfluous elements from this list. Recall that the same mapping may generally be represented by several different association lists. You needn't check for illegal input. Hint: use a tail-recursive helper function.

  5. (30 points) Define a structure type time-of-day, an error-checking constructor build-time-of-day for structures of this type, a binary equality testing predicate time-of-day-equal? for structures of this type, and a function time-of-day->string that converts structures of this type to strings.

    The build-time-of-day function should take two integers as arguments. The first should be in the range from 0 through 23 (representing hours in a 24-hour format) and the second should be in the range from 0 through 59 (representing minutes). If the function is given a noninteger argument or an argument outside the correct range, it should return a string representing an error message. Otherwise it should return a structure representing the intended time of day.

    The time-of-day-equal? predicate needn't check for illegal input.

    The time-of-day->string function should print a representation of the time of day in standard U.S. format -- with hours between 1 and 12, and the appropriate "a.m." or "p.m." suffix. The minutes should be represented as two digits even if the time is less than ten minutes after an hour. Your function should check that its argument is a time-of-day structure. It may assume that the structure was built with build-time-of-day (and not, for example, by evaluation of (make-time-of-day 'nine "o'clock")).

  6. (25 points). Define a function reachable-symbols that takes a start symbol for a grammar, a collection of rules for the grammar, and a list of nonterminals of the grammar, and returns a list of the nonterminal symbols that are reachable from the given start symbol.

    Assume that the collection of rules is represented as an association list that maps symbols to lists of right-hand sides of their rules, where each right-hand side is a list of symbols. So for example the rule set

    {E → E+T | T,   T → T*F | F,   F → x}
    is to be represented as
    ((E (E + T) (T)) (T (T * F) (F)) (F (x)))

    Your function is to check that the given start symbol is a member of the list of nonterminals. If not, it should return the empty list. You needn't check for any other kind of illegal input. Note that duplicate rules in the rule list are harmless for this function. In particular, you may assume that all keys (first components) in the association list are in the list of nonterminals.

    Note that this function could be used with only minor modification to check a grammar for direct or indirect left recursion.

    Hint 1: note that for purposes of reachability, it doesn't matter whether a grammar has two rules A → B C | D E or one rule A → B C D E, and that while it might be easier to process the second case.

    Hint 2: You might find it easiest to use top-down processing with a tail-recursive helper function that keeps track of symbols known to be reachable, if only to avoid infinite recursion if the grammar is left-recursive.

You may define any functions that you find useful, in addition to those mentioned above.

You are to test your program with the test file a3-test.scm, available from the class web site.

You are to turn in both hard and soft copies of all of your code (but not a3-test.scm, unless you modify it) as well as a hard copy of the results of your testing. The soft copy should be sent by email or turned in on diskette.