Homework 1ML |
Modified: |
•Download Poly/ML
Unzip and run the Setup
•Open by something similar to:
Start | Programs | Poly ML |
Run | Poly ML
student apps (on Desktop) | network Shortcuts | Shortcut to PolyML.exe | Select ML_dbase.pmd
•

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:
1+4;
val it = 5 : int
Edit | Paste
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;
Write, test with the given expressions and print the following functions.
biggest [1,~3,6,4,~2];
val it = 6 : int
biggest [5];
val it = 5 : int
positive [1,2,3,4,~3,5];
val it = [1, 2, 3, 4, 5] : int list
positive [~1,~2];
val it = [] : int list
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
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
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
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));
}