Question
use ML to write this code Write a file named p1.sml that contains a function named rinsort, which accepts only one parameter: a real list.
Write a file named p1.sml that contains a function named rinsort, which accepts only one parameter: a real list. It must return the sorted list of type real using an insertion sort, in ascending order, smallest to largest. Note that there may be from zero to an arbitrarily large number of real values in the input list parameter, and any value in the list may appear any number of times up to the number of entries in the list. Your solution must use recursion.
Your rinsort function must use a second function that you must also write. You must name this helper function rinsert. You may not embed this function within the rinsort function. The rinsert function must accept two parameters. The first is a real value to be inserted into the second, which is a real list. It must return a properly sorted real list, in ascending order, that contains all the members of the original second parameter plus the first parameter. (Note: Separation of the two functions allows independent testing of rinsort and rinsert.)
Remember that an empty list and a list of one element are both by definition sorted. Correct insertion of an item into a sorted list produces a sorted list.
The way your rinsort function should work is that if it is passed an empty list it will return that list. If the list is non-empty, it should call rinsert to insert the head of the list into the list returned by a call to rinsort using the tail of the list passed as its parameter. Thus, rinsort will be recursive.
The rinsert function must also be recursive. Since the second parameter is a sorted list, the simplest case is when the real to be inserted is smaller than the head of the sorted list passed. In this case rinsert will return the concatenation the value to be inserted with the sorted list passed. Otherwise, it returns the head of the list passed concatenated with the list returned by a recursive call to rinsert with the item to be inserted and the tail of the sorted list as parameters. Again, remember that insertion into an empty list results in a list of one item, which is by definition sorted.
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