Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help in writing these functions in OCaml functional programming language asap!!! Write a function called smallest_index which will take one argument (a list of

image text in transcribed

image text in transcribed

image text in transcribedimage text in transcribedimage text in transcribed

Please help in writing these functions in OCaml functional programming language asap!!!

Write a function called smallest_index which will take one argument (a list of integers) and return the index of the smallest element (not the value!). The index is for the first element, for the second element, and so on. Note that we don't normally talk of "indices" for lists (as opposed to arrays, where we do), but we will do so here. If there is more than one copy of the smallest element, return the index of the first occurrence of the element in the list. Don't use higher-order list processing functions in this problem; just write a regular recursive list processing function. It can implement a recursive or iterative process as you see fit. Most especially, you are not allowed to call any sorting functions in this function (such as the function in the OCaml standard libraries). If the input list is empty, raise an exception with a meaningful error message. The easiest way to do this is to call the function with the error message; it will raise the exception for you. Examples \# smallest_index [];; Exception: Invalid_argument "smallest_index: not enough elements". \# smallest_index [1];; - : int =0 \# smallest_index [2;1];; int =1 \# smallest_index [2;1;3];; : int =1 \# smallest_index [2;1;1];; int =1 \# smallest_index [2;1;1];; : int =1 \# smallest_index [2;1;3;1;4;5;0];; : int =6 Write a function called which takes two arguments: a non-negative argument and a list of integers. (You may assume that the argument is non-negative; you don't have to check for this.) It should flip the order of the first elements while leaving the rest unchanged, returning a new list. Since this will be done functionally, the input list will not (and cannot) be altered. If there aren't enough elements in the list to do the flipping, the function should raise the exception with a reasonable error message. Use your functions and to write a short function called which finds the smallest element in the list and flips the part of the list starting from the head and going up to and including that element. This is one iteration of the block sorting algorithm. Again, this is a purely functional function, so the input list is not altered; the new list is returned from the function. Now that we have defined block_sort1, define two block sorting functions: which generates a recursive process which generates an iterative process Examples These examples work for both functions. We'll just illustrate the answers using \# block_sort_r [];; - : int list =[] \# block_sort_r [1];; - : int list =[1] \# block_sort_r [1;1];; : int list =[1;1] \# block_sort_r [1;2];; : int list =[1;2]

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

Step: 3

blur-text-image

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

Main Memory Database Systems

Authors: Frans Faerber, Alfons Kemper, Per-Åke Alfons

1st Edition

1680833243, 978-1680833249

More Books

Students also viewed these Databases questions