In: Computer Science
In this assignment you will practice using the pattern-matching constructs described in chapter 7 of your textbook to write several short SML functions. If at all possible, write all functions using pattern-matching constructs. Include your name as a comment in the file, like this (* John Q Adams *).
Functions to implement (first 4 are from Chapter 7 of your textbook):
first download file contains this information:
(* #1 - quicksort *)
fun quicksort x:int list = nil;
(* #2 - member *)
fun member (e, s) = true;
(* #3 - returns the union of sets (lists) s1 and s2*)
(* You may assume that s1 and s2 do not have any duplicate elements
*)
fun union (s1, s2) = nil;
(* #4 - returns the intersection of sets (lists) s1 and s2 *)
(* You may assume that s1 and s2 do not have any duplicate elements
*)
fun intersection (s1, s2) = nil;
(* #5 - Return list of integers from start (inclusive) to stop
(exclusive) by step *)
fun range(start, stop, step) = nil;
(* #6 - Return a slice of a list between indices start
inclusive, and stop exclusive. Assume first element of list is at
index 0*)
fun slice(aList, start, stop) = nil;
Second downloadable file contains this information:
(* Sort a list of integers. *)
fun myMergeSort nil = nil
| myMergeSort [e] = [e]
| myMergeSort theList =
let
(* From the given list make a pair of lists
* (x,y), where half the elements of the
* original are in x and half are in y. *)
fun halve nil = (nil, nil)
| halve [a] = ([a], nil)
| halve (a::b::cs) =
let
val (x, y) = halve cs
in
(a::x, b::y)
end;
(* Merge two sorted lists of integers into
* a single sorted list. *)
fun merge (nil, ys) = ys
| merge (xs, nil) = xs
| merge (x::xs, y::ys) =
if (x < y) then x :: merge(xs, y::ys)
else y :: merge(x::xs, ys);
val (x, y) = halve theList
in
merge(myMergeSort x, myMergeSort y)
end;
fun pass_fail x = if x then "Pass" else "\t\tFail";
fun test(function, input, expected_output) = pass_fail (function(input) = expected_output);
fun test_set(function, input, expected_output) = pass_fail (myMergeSort(function(input)) = expected_output);
print("*********TEST RESULTS******************\n");
print("\n1.Quicksort "^test(quicksort, [1,3,2,5,4,8,7,9,0],
[0,1,2,3,4,5,7,8,9])^"\n");
print("\n2.Quicksort "^test(quicksort, [1,3,2,5,4,2, 8,6,7,9,0],
[0,1,2,2,3,4,5,6,7,8,9])^"\n");
print("\n3.Member "^test(member, (2,[1,3,2,5,4,2, 8,6,7,9,0]),
true)^"\n");
print("\n4.Member "^test(member, (12,[1,3,2,5,4,2, 8,6,7,9,0]),
false)^"\n");
print("\n5.Union "^test_set(union, ([1,3,2,5],
[0,1,2,33]),[0,1,2,3,5,33])^"\n");
print("\n6.Union "^test_set(union, ([1,3,2,5],
[5,2,3,1]),[1,2,3,5])^"\n");
print("\n7.Union "^test_set(union, ([1,3,2,5],
[]),[1,2,3,5])^"\n");
print("\n8.intersection "^test_set(intersection, ([1,3,22,5],
[5,12,3,1]),[1,3,5])^"\n");
print("\n9.intersection "^test_set(intersection, ([1,3,2,5],
[15,12,13,11]),[])^"\n");
print("\n10.intersection "^test_set(intersection, ([1,3,2,5],
[5,2,3,1]),[1,2,3,5])^"\n");
print("\n11.Range "^test(range, (2,12,3), [2,5,8,11])^"\n");
print("\n12.Range "^test(range, (12,34,5),
[12,17,22,27,32])^"\n");
print("\n13.Range "^test(range, (10,2,~1),
[10,9,8,7,6,5,4,3])^"\n");
print("\n14.Slice "^test(slice, (range(2,20,2),2,7),
[6,8,10,12,14])^"\n");
print("\n15.Slice "^test(slice, (range(1,100,4),3,12), [13, 17, 21,
25, 29, 33, 37, 41, 45])^"\n");
Due to time constraint i cannot solve the range and slice functions...please write them...i think theyre easy
(* #1 - quicksort *)
fun par_helper([], x, l, r) = (l, r)
| par_helper(h::t, x, l, r) =
if h <= x then
par_helper(t, x, l @ [h], r)
else
par_helper(t, x, l, r @ [h]);
fun par(l, x) = par_helper(l, x, [], []);
fun quicksort [] = []
| quicksort (h::t) =
let
val (left, right) = par(t, h)
in
quicksort left @ [h] @ quicksort right
end;
(* #2 - member *)
fun member (x, nil) = false | member (x, y::ys) = x=y orelse member (x,ys);
(* #3 - returns the union of sets (lists) s1 and s2*)
(* You may assume that s1 and s2 do not have any duplicate elements *)
fun union([],y) = y
| union(a::x,y) =
if member(a,y) then union(x,y)
else a::union(x,y);
(* #4 - returns the intersection of sets (lists) s1 and s2 *)
(* You may assume that s1 and s2 do not have any duplicate elements *)
fun intersection([],y) = []
| intersection(a::x,y) =
if member(a,y) then a::intersection(x,y)
else intersection(x,y);
(* #5 - Return list of integers from start (inclusive) to stop (exclusive) by step *)
fun range (start, stop , step) = nil
(* #6 - Return a slice of a list between indices start inclusive, and stop exclusive. Assume first element of list is at index 0*)
fun slice(aList, start, stop) = nil;
(* Sort a list of integers. *)
fun myMergeSort nil = nil
| myMergeSort [e] = [e]
| myMergeSort theList =
let
(* From the given list make a pair of lists
* (x,y), where half the elements of the
* original are in x and half are in y. *)
fun halve nil = (nil, nil)
| halve [a] = ([a], nil)
| halve (a::b::cs) =
let
val (x, y) = halve cs
in
(a::x, b::y)
end;
(* Merge two sorted lists of integers into
* a single sorted list. *)
fun merge (nil, ys) = ys
| merge (xs, nil) = xs
| merge (x::xs, y::ys) =
if (x < y) then x :: merge(xs, y::ys)
else y :: merge(x::xs, ys);
val (x, y) = halve theList
in
merge(myMergeSort x, myMergeSort y)
end;
fun pass_fail x = if x then "Pass" else "\t\tFail";
fun test(function, input, expected_output) = pass_fail (function(input) = expected_output);
fun test_set(function, input, expected_output) = pass_fail (myMergeSort(function(input)) = expected_output);
print("*********TEST RESULTS******************\n");
print("\n1.Quicksort "^test(quicksort, [1,3,2,5,4,8,7,9,0], [0,1,2,3,4,5,7,8,9])^"\n");
print("\n2.Quicksort "^test(quicksort, [1,3,2,5,4,2, 8,6,7,9,0], [0,1,2,2,3,4,5,6,7,8,9])^"\n");
print("\n3.Member "^test(member, (2,[1,3,2,5,4,2, 8,6,7,9,0]), true)^"\n");
print("\n4.Member "^test(member, (12,[1,3,2,5,4,2, 8,6,7,9,0]), false)^"\n");
print("\n5.Union "^test_set(union, ([1,3,2,5], [0,1,2,33]),[0,1,2,3,5,33])^"\n");
print("\n6.Union "^test_set(union, ([1,3,2,5], [5,2,3,1]),[1,2,3,5])^"\n");
print("\n7.Union "^test_set(union, ([1,3,2,5], []),[1,2,3,5])^"\n");
print("\n8.intersection "^test_set(intersection, ([1,3,22,5], [5,12,3,1]),[1,3,5])^"\n");
print("\n9.intersection "^test_set(intersection, ([1,3,2,5], [15,12,13,11]),[])^"\n");
print("\n10.intersection "^test_set(intersection, ([1,3,2,5], [5,2,3,1]),[1,2,3,5])^"\n");
print("\n11.Range "^test(range, (2,12,3), [2,5,8,11])^"\n");
print("\n12.Range "^test(range, (12,34,5), [12,17,22,27,32])^"\n");
print("\n13.Range "^test(range, (10,2,~1), [10,9,8,7,6,5,4,3])^"\n");
print("\n14.Slice "^test(slice, (range(2,20,2),2,7), [6,8,10,12,14])^"\n");
print("\n15.Slice "^test(slice, (range(1,100,4),3,12), [13, 17, 21, 25, 29, 33, 37, 41, 45])^"\n");
(* #1 - quicksort *)
fun par_helper([], x, l, r) = (l, r)
| par_helper(h::t, x, l, r) =
if h <= x then
par_helper(t, x, l @ [h], r)
else
par_helper(t, x, l, r @ [h]);
fun par(l, x) = par_helper(l, x, [], []);
fun quicksort [] = []
| quicksort (h::t) =
let
val (left, right) = par(t, h)
in
quicksort left @ [h] @ quicksort right
end;
(* #2 - member *)
fun member (x, nil) = false | member (x, y::ys) = x=y orelse member (x,ys);
(* #3 - returns the union of sets (lists) s1 and s2*)
(* You may assume that s1 and s2 do not have any duplicate elements *)
fun union([],y) = y
| union(a::x,y) =
if member(a,y) then union(x,y)
else a::union(x,y);
(* #4 - returns the intersection of sets (lists) s1 and s2 *)
(* You may assume that s1 and s2 do not have any duplicate elements *)
fun intersection([],y) = []
| intersection(a::x,y) =
if member(a,y) then a::intersection(x,y)
else intersection(x,y);
(* #5 - Return list of integers from start (inclusive) to stop (exclusive) by step *)
fun range (start, stop , step) = nil
(* #6 - Return a slice of a list between indices start inclusive, and stop exclusive. Assume first element of list is at index 0*)
fun slice(aList, start, stop) = nil;
(* Sort a list of integers. *)
fun myMergeSort nil = nil
| myMergeSort [e] = [e]
| myMergeSort theList =
let
(* From the given list make a pair of lists
* (x,y), where half the elements of the
* original are in x and half are in y. *)
fun halve nil = (nil, nil)
| halve [a] = ([a], nil)
| halve (a::b::cs) =
let
val (x, y) = halve cs
in
(a::x, b::y)
end;
(* Merge two sorted lists of integers into
* a single sorted list. *)
fun merge (nil, ys) = ys
| merge (xs, nil) = xs
| merge (x::xs, y::ys) =
if (x < y) then x :: merge(xs, y::ys)
else y :: merge(x::xs, ys);
val (x, y) = halve theList
in
merge(myMergeSort x, myMergeSort y)
end;
fun pass_fail x = if x then "Pass" else "\t\tFail";
fun test(function, input, expected_output) = pass_fail (function(input) = expected_output);
fun test_set(function, input, expected_output) = pass_fail (myMergeSort(function(input)) = expected_output);
print("*********TEST RESULTS******************\n");
print("\n1.Quicksort "^test(quicksort, [1,3,2,5,4,8,7,9,0], [0,1,2,3,4,5,7,8,9])^"\n");
print("\n2.Quicksort "^test(quicksort, [1,3,2,5,4,2, 8,6,7,9,0], [0,1,2,2,3,4,5,6,7,8,9])^"\n");
print("\n3.Member "^test(member, (2,[1,3,2,5,4,2, 8,6,7,9,0]), true)^"\n");
print("\n4.Member "^test(member, (12,[1,3,2,5,4,2, 8,6,7,9,0]), false)^"\n");
print("\n5.Union "^test_set(union, ([1,3,2,5], [0,1,2,33]),[0,1,2,3,5,33])^"\n");
print("\n6.Union "^test_set(union, ([1,3,2,5], [5,2,3,1]),[1,2,3,5])^"\n");
print("\n7.Union "^test_set(union, ([1,3,2,5], []),[1,2,3,5])^"\n");
print("\n8.intersection "^test_set(intersection, ([1,3,22,5], [5,12,3,1]),[1,3,5])^"\n");
print("\n9.intersection "^test_set(intersection, ([1,3,2,5], [15,12,13,11]),[])^"\n");
print("\n10.intersection "^test_set(intersection, ([1,3,2,5], [5,2,3,1]),[1,2,3,5])^"\n");
print("\n11.Range "^test(range, (2,12,3), [2,5,8,11])^"\n");
print("\n12.Range "^test(range, (12,34,5), [12,17,22,27,32])^"\n");
print("\n13.Range "^test(range, (10,2,~1), [10,9,8,7,6,5,4,3])^"\n");
print("\n14.Slice "^test(slice, (range(2,20,2),2,7), [6,8,10,12,14])^"\n");
print("\n15.Slice "^test(slice, (range(1,100,4),3,12), [13, 17, 21, 25, 29, 33, 37, 41, 45])^"\n");