content="Data-structures and algorithms in Standard ML. Notes from a lecture course given by Michael Fourman at the University of West Australia, Spring 1994." /> rel="start" type="text/html" href="/mfourman/index.html" title="Michael Fourman" /> Sample Exam

Sample Exam

Michael P. Fourman

October 31, 2006

1 Introduction

This document contains information concerning the conduct of the mid-term examination, and some sample questions, representative of those that will be set.

2 Information

The mid-term examination for this course will be held, as advertised, on Thursday, April 14th, during the normal lecture period.

3 Sample Questions

The examination will contain one short question (worth 5 marks), and three longer questions. You should attempt the short question, and only two of the three longer questions, each of which will be worth 10 marks.

  1. 5 marks
    Give the responses of the ML system to the following sequence of declarations
    val a = 1;  
    val b = 2;  
    fun f a = a + b;  
    val b = 3;  
    f b;

  2. Long Question 10 marks
    The following datatype can be used to represent trees whose nodes can have an arbitrary number of children.
    datatype  ’a  Tree  = Tree  of ’a  * ’a  Tree  list
    1. What tree does the following expression denote (i.e draw a picture):
      Tree(1, [Tree(2, [ ]), Tree(3, [Tree(4,[ ])])])
    2. Define a function to calculate the number of leaves in such a tree.
    3. We can assign a level to each node in a tree as follows. The node at the root is at level 1. Its children are at level 2. Their children are at level 3 and so on.

      Suppose we are interested in trees where an internal node at level n always has exactly n children. Define a function check : ’a Tree ->bool that checks whether a given tree has this property.

  3. Long Question 10 marks
    The EQueue signature is like the signature Queue, but is extended with an additional operation multiple enqueue, menq:(Item  list * Queue) -> Queue, intended to add a number of items (in an arbitrary order) to the queue in a single operation.
    signature EQueue =  
        type Item  
        type Queue  
        val empty : Queue  
        val enq : (Item * Queue) -> Queue  
        val deq : Queue -> (Item * Queue)  
        val menq: (Item list * Queue) -> Queue  

    An implementation of a stack, including this operation, uses the type declaration

    type Queue = Item list list

    the operations empty and menq are implemented as follows:

    val empty = []  
    fun menq(items, q) = items :: q

    1. Complete the following declarations of the functions enq and deq for this implementation
      fun enq(item,       []) =  
        | enq(item, (h :: t)) =  
      fun deq((h :: t) :: r)  =  
        | deq([] :: r)        =  
        | deq []              =

    2. What is the complexity of the three operations
      1. enq,
      2. deq,
      3. menq

      for this implementation?

  4. Long Question 10 marks
    An implementation of sets of integers is designed to represent a set by a list without repetitions, kept in increasing order. Here is the function union : Set*Set -> Set from this implementation
    fun union(a, []) = a  
      | union([], b) = b  
      | union(ah :: at, bh :: bt) =  
              if ah < bh then ah :: union(at, bh :: bt)  
              else if ah = bh then ah :: union(at, bt)  
              else bh :: union(ah :: at, bt)

    1. What is the complexity of this implementation of union?
    2. Give an implementation of the operation insert : (int*Set) ->Set compatible with this representation
    3. Give an O(n) implementation of the operation intersect : Set*Set -> Set, compatible with this representation.

©Michael Fourman 1994-2006