Question: Below is code from a .sml file that needs to be modified into two parts Part 1: Write a function insert that takes as input

Below is code from a .sml file that needs to be modified into two parts

Part 1: Write a function insert that takes as input a number and a sorted list of numbers and outputs a list with the number inserted into the list at the correct place so that the returned list is still sorted. Do this recursively and use pattern matching. Hint: You need to consider only three cases: the given list is empty, the given number is less than the first element of the list, or the given number is greater than the first element of the list.

Part 2: Use your insert function to write a sort function (a recursive version of "insertion sort"). Hint: To sort a list you insert the first element of the list into the sorted rest of the list.

(* this is a quicksort function of type int list -> int list. *) fun quicksort [] = [] | quicksort lst = let val pivot = hd lst fun quicksortSplit (_, [], less, more) = (less, more) | quicksortSplit (pivot, head::tail, less, more) = if head < pivot then quicksortSplit (pivot, tail, head::less, more) else quicksortSplit (pivot, tail, less, head::more); val (less, more) = quicksortSplit (pivot, tl lst, [], []) val small = quicksort less val big = quicksort more in small@[pivot]@big end;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!