Question

In: Computer Science

In this assignment you will practice using the pattern-matching constructs described in chapter 7 of your...

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 *).

  • To reiterate - Use the pattern matching constructs described in chapter 7 to define your functions.
  • Do the two steps below:
    • Download the following file to a folder. It contains dummy, hence incorrect, implementations of all functions described below. Your task is to provide the correct implementation for each function. You will be submitting only this file. See the next item for instructions on how to test your code.
    • Download the following file and use it for testing your code, like this: sml test_hw2.sml. It has 10 tests written for the various functions. When you submit your assignment, I will test the functions you write in the same way.
  • Other cautions, caveats etc.
    • For each function that fails one or more tests, you lose 14 points.
    • If you are unable to write a particular function, leave the dummy definition for that function in your file so that the remaining tests can be executed.
    • You are expected to use good programming style, such as naming, indentation, and avoiding redundancy when possible. You should also place a descriptive comment on each of your functions.
    • If the solution to a previous problem is useful in helping you solve a later problem, you may call the earlier function from the later function. You are also allowed to define "helper" functions to solve these problems.
    • You are expected to write all the code you use in this assignment. You may not use any functions in any libraries (for example List.partition) with the exception of standard functions abs and length, if needed.
    • Note: As with the previous assignment, this assignment does not require a lot of code.

Functions to implement (first 4 are from Chapter 7 of your textbook):

  1. Exercise 6. Follow the algorithm outlined in your textbook on page 116. The code follows a pattern similar to that of the mergesort function discussed in class. A helper function named partition does most of the work. Write your own partition function. You may not use any functions in any libraries (for example List.partition)
  2. Exercise 8. Once again, write your own function from scratch. Do not use any membership testing functions in any libraries.
  3. Exercise 9. Again, write your own function from scratch. Do not use any union computing functions in any libraries.
  4. Exercise 10. Again, write your own function from scratch. Do not use any intersection computing functions in any libraries.
  5. A range function similar to the range function in Python. For example, range (2,12,3) returns the list [2,5,8,11]
  6. A slice function, with functionality similar to the Python list slice operator. For example, slice ([11,22,3,14,5,6], 1, 4) returns the list [22,3,14]

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");

Solutions

Expert Solution

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");

Related Solutions

Practice Problems (Chapters 7 and 8) Chapter 7        1.         Which would you expect to be more...
Practice Problems (Chapters 7 and 8) Chapter 7        1.         Which would you expect to be more variable: (a) the distribution of scores in a population or (b) the distribution of sample means based on random samples of 25 cases from this population. Explain. Given a normal distribution of scores that has a mean of 50 and a standard deviation of 10, what is the probability of selecting a score that is greater than 65? If a random sample of 10...
Pattern matching using python3 re module Write a regular expression that will find out all the...
Pattern matching using python3 re module Write a regular expression that will find out all the words that ends with 4 consecutive vowels at the end. Example: import re f=open("/usr/share/dict/american-english",'r') pattern='a[a-z]*d$' words=f.readlines() matchlist=[word for word in words if re.match(pattern,word)] =============================== Generate random string follow a specific pattern You will take a username and ask user for the password. User has to enter a strong password in order to create his or her account. The criteria for strong password is, a)...
In this assignment, you will practice solving a problem using object-oriented programming and specifically, you will...
In this assignment, you will practice solving a problem using object-oriented programming and specifically, you will use the concept of object aggregation (i.e., has-a relationship between objects). You will implement a Java application, called MovieApplication that could be used in the movie industry. You are asked to implement three classes: Movie, Distributor, and MovieDriver. Each of these classes is described below. The Movie class represents a movie and has the following attributes: name (of type String), directorsName (of type String),...
HEALTHCARE ADMINISTRATION For this week’s assignment, you will be creating a marketing tool for your practice...
HEALTHCARE ADMINISTRATION For this week’s assignment, you will be creating a marketing tool for your practice to advertise the new vaccine clinic. You can either create a brochure to send out in the mail or posters to display around town. Consider your audience (who are you targeting?) while you create this item. Think about what would catch your eye as a customer when putting this together. You can create this tool in Word as a poster with clip art, etc....
This assignment will give you practice in Handling Exceptions and using File I/O. Language for this...
This assignment will give you practice in Handling Exceptions and using File I/O. Language for this program is JAVA Part One A hotel salesperson enters sales in a text file. Each line contains the following, separated by semicolons: The name of the client, the service sold (such as Dinner, Conference, Lodging, and so on), the amount of the sale, and the date of that event. Prompt the user for data to write the file. Part Two Write a program that...
This assignment is to give you practice using struts, arrays, and sorting. (Objective C++ and please...
This assignment is to give you practice using struts, arrays, and sorting. (Objective C++ and please have a screenshot of output) In competitive diving, each diver makes dives of varying degrees of difficulty. Nine judges score each dive from 0 through 10 in steps of 0.5. The difficulty is a floating-point value between 1.0 and 3.0 that represents how complex the dive is to perform. The total score is obtained by discarding the lowest and highest of the judges’ scores,...
Project Assignment The purpose of this assignment is for you to gain practice in applying the...
Project Assignment The purpose of this assignment is for you to gain practice in applying the concepts and techniques we learned in class. In order to complete the assignment, please do the following: 1. find or create a data set containing values of at least one interval or ratio variable for at least one-hundred cases (n >= 100); 1 2. provide basic descriptive statistics to summarize the central tendency and variability of the data; 3. provide at least one table...
Captial Rationing as described in Chapter 7 is said to involve a high degree of subjective...
Captial Rationing as described in Chapter 7 is said to involve a high degree of subjective judgement. Do you think this makes it an unreliable method of evaluating investment opportunities, relative to the simple method of Net Present Value? What would convince you to put more confidence in this method? 8
Define the 7 Levels of Evidence described in the Sewell textbook. Define the term ‘research practice...
Define the 7 Levels of Evidence described in the Sewell textbook. Define the term ‘research practice gap’ and name two causes. Provide a brief definition of a ‘Just Culture’. How can informatics be used to monitor errors and support a Just Culture? What is BCMA and how does it contribute to patient safety? Define telenursing and describe how it occurs. Describe at least 2 examples of patient assessments that may be accomplished using telenursing. What is the Leapfrog Group and...
The purpose of this C++ programming assignment is to practice using an array. This problem is...
The purpose of this C++ programming assignment is to practice using an array. This problem is selected from the online contest problem archive, which is used mostly by college students worldwide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest. For your convenience, I copied the description of the problem below with my note on the I/O and a sample executable. Background The world-known gangster Vito Deadstone...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT