Assignment 12
SML and Exception Handling
Due December 2nd, 12:30 p.m.
(Must be done with your group)

  1. [skill 21.1] For the following patterns, describe what they match:
    1. h1::h2::nil
    2. h1::h2::t
    3. 1::2::t
    4. "Hi"::"There"::nil
  2. [skill 21.1] Write a polymorphic SML function powerset that given a set (represented as a list) will return the set of all its subsets.  Note that this is a polymorphic version of the function that you did in recitations, which worked only on sets of integers.  E.g., powerset (["a", "b"]) should return [], ["a"], ["b"], ["a","b"] and powerset([1,2]) should return [],[1],[2],[1,2].
  3. [skill 21.1] Consider the following two SML functions:

    fun f1(nil,v) = nil
      | f1(h::t,v) = if v=h then f1(t,v) else h::f1(t,v);

    fun f2(a,0) = a
      | f2(h::t,n) = f2(t,n-1)
      | f2(nil,n) = nil;

     

    1. Clearly describe what function f1 does

    2. Give the type that will be inferred for f1 by the SML compiler

    3. Clearly describe what function f2 does

    4. Give that type that will be inferred for f2 by the SML compiler

  4. [skill 21.1] Write a function find such that find(xs,i)  returns the ith smallest integer in the list xs.  Your code should be complete: i.e., if you need any helper functions you should write the code for them also.
  5. [skill 22.3] Page 577, Problem set, Problem 9 (ps: the question says "...following Java skeletal program" but the program really uses C++ syntax and semantics).
  6. [skill 22.2] Give an example program that illustrates the usefulness of the "finally" part of Java's try-catch-finally statement.  Explain your example.
  7. [skill 22.3] Consider the following code fragment in Java syntax:

    try {
      ... /* Try block */
    }
    catch (E1 e) { ... /* Catch block */ }
    finally {... /* Finally block */ }
    print("After try"); /* print does not throw any exceptions */

    For each of the following cases, indicate what code will be executed, in what order, and what exceptions, if any, will be thrown, and in what order.  A block "terminates normally" if it does not throw any exceptions and does not execute a "return" statement.

    1. All the blocks (if executed) will terminate normally. 

    2. The try block throws an exception of type E1, the catch block (if executed) will throw an exception of type E2, and the finally block (if executed) will terminate normally. 

    3. The try block executes a "return" statement, the catch block and finally blocks (if executed) terminate normally. 

    4. The try block terminates normally, the catch block (if executed) will throw an exception of type E2, and the finally block (if executed) will throw an  exception of type E3. 

  8. [synthesis] Read up about exceptions in SML (from the tutorial or elsewhere) and compare and contrast exceptions and exception handling mechanisms in Java and SML.