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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started