Computer Science 3675
Fall 2015
Practice Questions for Quiz 6

  1. Type inference is a process where a compiler determines the type of an expression without explicitly being told its type. Does a Java compiler perform any type inference?

    Answer

  2. What is the name of an algorithm that can solve a system of equations over a free algebra?

    Answer

  3. Given the Cinnameg type definition

      Type Frog =
          green  String
        | yellow (Integer, Integer)
      %Type
    
    Which of the following are sensible? Answer all that are sensible. (Sensible means that they compile and do not lead to failure when they run.) Treat each as a separate problem.
    1. ! kermit = frog("Kermit", 3, 4).
    2. ! kermit = green("Kermit").
    3. ! kermit = yellow("Kermit").
    4. ! kermit = yellow(3,4).
    5. Match yellow(x,y) =~ yellow(4,7).
    6. Match green(s) =~ yellow(4,7).

    Answer

  4. Using the type Frog from the preceding question, define a function size(f ) so that: if f is a Frog of the form green(name), then size(f ) is the length of name; and if f is a Frog of the form yellow(m, n) then size(f ) is m.

    Answer

  5. Suppose that you are performing type inference and encounter expression f (x), where f 's current type is α, x's current type is β and the current type of f (x) is γ. Write one equation involving α, β and γ that completely characterizes type requirements induced by this expression.

    Answer

  6. Imagine that lists are not available. Write a type definition in Cinnameg of type StringList where a value of type StringList is a linked list of strings. Then do whatever is necessary to ensure that functions head, tail, nil? and operator :: are defined. Assume that :: has already been specified to be a binary operator. nil?(x) is true just when x is an empty list. Do not worry about what happens when the head or tail of an empty list is computed.

    Hint. There are two kinds of lists: empty lists and nonempty lists. What information does a nonempty list need to hold? Think of linked lists.

    Answer

  7. Suppose that type Tree is defined as follows in Cinnameg.

    Type Tree =
        leaf    Integer
      | nonleaf (Tree, Tree)
    %Type
    
    So a tree consists of leaves and nonleaves. Each leaf holds an integer. When we think of a tree, we think of the entire collection of leaves and nonleaves, including those in the subtrees.

    Write a definition of sum(t), which yields the sum of all of the numbers in leaves of tree t.

    Answer

  8. Using the type Tree from the preceding question, write a definition of toList(t), which yields a list of all numbers in leaves in tree t.

    Answer

  9. Repeat the previous exercise, but do not use concatenation.

    Answer

  10. A typed language is one where at least some type checking is done by the compiler.

    Parametric polymorphism (polymorphism with type variables) was not present in Java 1.1. It was added to Java with the release of Java 1.5. Why is polymorphism such important feature of a typed language that the designers of Java felt adding it to Java was necessary?

    Answer