Question: MACHINE LANGUAGE Exercise 6': Write a function insert that takes as input a number and a sorted list of numbers and outputs a list with

MACHINE LANGUAGE

Exercise 6': 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.

Exercise 6'': 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.

Exercise 7': Do Exercise 7 but with your sort function from Exercise 6'' instead of the book's quicksort function. You will need to modify your version of insert so that it takes a comparison function as a parameter. Use the names sort2 and insert2 for the new versions of sort and insert. (Notice that sort2 and insert2 are somewhat like "generic" functions in Java or "template" functions in C++.)

Exercise 8': Write a function twiddle that takes as input a list of triples, and returns a list of pairs where each triple from the input has its middle element removed and its first and third elements swapped. Write this using pattern matching.

Exercise 9': Write a function flipper that flips alternate elements from a list. So if the input list is [a1, a2, a3, a4, a5, a6], then the output list should be [a2, a1, a4, a3, a6, a5]. If the length of the input list is odd, then the last element of the input list should also be the last element of the output list. Write this using pattern matching and the cons operator.

MACHINE LANGUAGE Exercise 6': Write a function insert that takes as input

exercise 6': 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. Exercise 6": 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. Exercise 7': Do Exercise 7 but with your sort function from Exercise 6" instead of the book's quicksort function. You will need to modify your version of insert so that it takes a comparison function as a parameter. Use the names sort2 and insert2 for the new versions of sort and insert. (Notice that sort2 and insert2 are somewhat like "generic" functions in Java or "template" functions in C++.) Exercise 8': Write a function twiddle that takes as input a list of triples, and returns a list of pairs where each triple from the input has its middle element removed and its first and third elements swapped. Write this using pattern matching. Exercise 9': Write a function flipper that flips alternate elements from a list. So if the input list is [a1, a2, a3, a4, a5, a6], then the output list should be [a2, a1, a4, a3, a6, a5]. If the length of the input list is odd, then the last element of the input list should also be the last element of the output list. Write this using pattern matching and the cons operator

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!