Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed
image text in transcribed
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. 2 Some programmers claim that their ML programs are so clearly written as to be "self-documenting." requiring no comments. Indeed, some programmers have made this claim for their code in just about every programming language ever invented. But no program is ever as self-documenting as it seems to its author 116 Chapter 7-A Second Look at ML Exercise 3 Define a function less of type int * int list -> int list so that less le, L) is a list of all the integers in that are less than e. Exercise 4 Define a function repeats of type 'a list -> bool so that repeats (L) is true if and only if the list 1. has two equal elements next to each other

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

Structured Search For Big Data From Keywords To Key-objects

Authors: Mikhail Gilula

1st Edition

012804652X, 9780128046524

More Books

Students also viewed these Databases questions

Question

What are the six main categories of nonverbal signals? [LO-5]

Answered: 1 week ago