Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Joe Celkos Data And Databases Concepts In Practice

Authors: Joe Celko

1st Edition

1558604324, 978-1558604322

More Books

Students also viewed these Databases questions

Question

5. Members are highly motivated to take control of their work.

Answered: 1 week ago