Homework 1

ML

Modified

Starting Poly ML

Home:

  1. Download Poly/ML 

  2. Unzip and run the Setup

  3. Open by something similar to:

Start | Programs | Poly ML |
Run | Poly ML

IUS:

student apps (on Desktop) | network Shortcuts | Shortcut to PolyML.exe | Select ML_dbase.pmd  

Programming

IMMEDIATE WINDOW - Poly ML opens in immediate execution mode as in the window at right. ML instructions can be entered directly and are executed immediately. 

EDIT WINDOW - The immediate execution window doesn't support editing. To enter and edit functions do any of the following:

The Source1 window at right is used for entering and editing the ML functions.

EXECUTING - ML execution is done in the immediate window. Either:

The top window at right shows the result of pasting the reverse function from the Source1 window to the immediate window.

Debugging - Function entry and exit and certain parameters can be traced during execution, useful. Just copy and paste the following:

PolyML.Compiler.debug := true;
open PolyML.Debug;
trace true; 

Assignment

Write, test with the given expressions and print the following functions.

  1. GCD returns the greatest common divisor of two integers. Write the recursive function that implements Euclid's algorithm:

        int GCD(int x, int y) {
            if (y==0) return x;
            else        return GCD(y, x % y);
       }

    The ML modulus operator is mod: 15 mod 4 is 3.

            gcd (9,18);
                val it = 9 : int
            gcd(16,25);
                val it = 1 : int
            gcd(27,81);
                val it = 27 : int
            gcd(36,81);
                val it = 9 : int
     
  2. Biggest returns the largest number in a list.
    1. biggest [1,~3,6,4,~2];
          val it = 6 : int
      biggest [5];
          val it = 5 : int

  3. Positive returns a list consisting of the positive numbers in a list.
    1. positive [1,2,3,4,~3,5];
          val it = [1, 2, 3, 4, 5] : int list
      positive [~1,~2];
          val it = [] : int list


  4. Intersect returns the intersection of two lists. Hint: Use member.
  5.         fun member (a,L) =
                if null L then false
                else if a = hd L then true
                       else member(a, tl L);

      intersect ([1,2,3,4], [3,4,5,6]);
          val it = [3, 4] : int list
      intersect ([1,2,3,4], [5,6,7,8]);
          val it = [] : int list

  6. Insert returns an ordered list with an element inserted.
    1. insert (4, [1,2,3,5]);
          val it = [1, 2, 3, 4, 5] : int list
      insert (4, []);
          val it = [4] : int list
      insert (1, [2,3,4]);
          val it = [1, 2, 3, 4] : int list

  7. Sort a list. Hint: Use insert.
    1. sort [5,4,3,2,1];
          val it = [1, 2, 3, 4, 5] : int list
      sort [];
          val it = [] : int list
      sort [4,2,5,1,3];
          val it = [1, 2, 3, 4, 5] : int list

Turn In

  1. Cover sheet with your name, date, and Homework 1.
  2. Print out of function definitions .
  3. Print interactions for Assignments 1-6 containing the evaluation results of all the functions using the given test examples.

 

Hints

Just for comparison, below is a Java version of the insertion sort.

static LinkedList insert(Integer a, LinkedList L) {
  if (L.size() == 0) L.add(a);
  else if (a.intValue() > ((Integer) L.getFirst()).intValue()) {
            Integer first = (Integer) L.removeFirst();
            insert(a, L).addFirst(first);
        }
        else L.addFirst(a);
  return L;
}

static LinkedList sort(LinkedList L) {
  if (L.size() == 0) return L;
  Integer first = (Integer) L.removeFirst();
  return insert(first, sort(L));
}