Write Code (and explain your code) in ML programming Language Recall that merges
ID: 3843313 • Letter: W
Question
Write Code (and explain your code) in ML programming Language Recall that mergesort sorts a list by splitting it in two, sorting each half, then merging the two sorted halves into one sorted whole. Here are some functions that do that. merge(x, y) merges the two sorted lists into one split(x) splits an unsorted list into a pair of unsorted lists, each half the size mergesort (x) sorts the list x Please finish following program by replacing??? with proper code. fun merge ([], y) = y merge (x, []) = x merge(a::x, b::y) = if a int list merge ([1, 4, 5], [2, 3, 4]); val it = [1, 2, 3, 4, 5] int list fun split ([1]) = ([], ()) split (a::[]) = ([a], []) split [a::b::y) (a::fst (split (y)), b:: snd (split (y))); val split = fn:'a list -- rightarrow 'a list split 'a list split ([7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7]); val it = ([7, 5, 3, 1, 3, 5, 7], [6, 4, 2, 2, 4, 6]): int list * int list fun mergesort ([]) = ??? mergesort ([a]) = ??? mergesort (x) = ??? mergesort ([4, 3, 2]); val it = (2, 3, 4): int list mergesort ([1, 3, 6, 3, 7, 3, 8, 9, 2, 5]); val it = [1, 2, 3, 3, 5, 6, 7, 8, 9): int list.Explanation / Answer
IN FIRST INSTANCE OF ????:
if (a<b) b :: merge(a :: x,y)
else a :: merge(x,b :: y);
IN SECOND INSTANCE OF ????:
fun mergesort([])=[]
| mergesort[a]=[a]
| mergesort[x]=
let
val(x,y) = split
in
print("xList: "^printList(x));
printf("yList: "^printList(y));
merge(mergesort x,mergesort y)
end;
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.