Midterm: Sample Questions (v 1.0)

This page includes questions by the instructor (John Boyland) that may appear on the midterm. At least one question (of the approx. four questions) on the midterm will be all but exactly the same as on this page.

The material on the exam will come from Chapters 1-18. You should be ready to do any of the exercises from the book that does not require research. At least one of the exam questions will be an exercise from the book.

Conceptual Terms

You should be able to define and explain the following concepts:

Programming

The quicksort function we wrote is very inefficient if the list is already sorted. Modify it (code given) to first check if the list is sorted, and simply return the argument in this case.

Another sorting algorithm that is efficient over sorted lists is Bubble-Sort. Bubble sort swaps two adjacent elements if they are in the wrong order. Implement this in ML using a recursive helper function which makes one pass through the list and returns a possibly reordered list with a boolean indicating whether there was any change. Then use another helper function to repeatedly call the helper function until there are no changes. Your bubblesort function may assume the list being sorted is a list of integers. It should work for any length of list.

Write a ``one-line'' program for a polynomial over a ring to evaluate the polynomial at the ``1'' value for the ring assuming that ``1'' is the identity element for the multiplication operation. Use foldr

evalatone ((a,m,z,i): 'a ring) (poly:'a list) = ...

Write a insertBST function to insert an element into a binary search tree (without attempting to keep the tree balanced) and write a SETadd function that uses it to add an element to a SET, where:

type 'a set = 'a tree * ('a * 'a -> bool);

A appendable sequence is like a list but with efficient append implementation. Internally, an append list is one of three forms: an empty sequence, a sequence of one element, or the appending of two sequences. For example the sequence {1,2,3} could be represented as

Append(Append(Empty,Single(1)), Append(Append(Single(2),Append(Empty,Empty)), Single(3)))
Write the following ML declarations: The append operation is very cheap; what is the drawback?

Activation records

Given the following program:

fun f1 x = 
  let
    fun f2a f = 
      ...
         f x
      ...
    fun f2b y = 
      let
        fun f3 z = y + z
      in
         ... 
	    (f2a f3)
	 ...
      end
  in
    f2b 3
  end;
Suppose that main calls f1 which calls f2b which calls f2a which calls f3 indirectly (through its parameter f). At this point, while f3 is adding y+z, show all active activation records, including each one's nesting link. (Hint: they can be stack allocated. Why?)