Question

Program has to be written in ML language. Write functions in ML to do the following:...

Program has to be written in ML language. 

Write functions in ML to do the following:
1. Given a list, return that list with its first and third elements deleted. Assume the length of the list
is at least 3.

2. Given a list of real pairs, return the list containing the larger element in each pair. Examples:

larger_in_pair([]) = []
larger_in_pair([(1.0,2.5),(4.7,3.6),(5.5,8.8)] = [2.5,4.7,8.8]

3. Given two lists of integers, return the list of the sums of the elements in corresponding position in
the input lists. The shorter list has to be seen as extended with 0's. Examples:

add2lists([1,3],[2,4]) = [3,7]
add2lists([2,4],[3]) = [5,4]
add2lists([1,4],[5,3,1,8,9]) = [6,7,1,8,9]
add2lists([],[]) = []

4. Use the function Map and an anonymous function to turn a list of integer pairs into a list of integer,
each of which is the product of the two elements in a pair.

5. Use the function Reduce and an anonymous function to find the minimum of a list of reals.

6. Use the function Filter and an anonymous function to find those elements of a list of reals that are
between 3.0 and 4.0 (inclusive).

Homework Answers

Answer #1

1//length of list
fun length(list)=3;

           if(list=nil) then 0

            else 1+length(tl(list))

// returning first element of list
fun first_elem []=[]
| first_elem((list)::1)=list::first_elem 1;

//   returning last element of list      
fun rem_elem nil = nil
| rem_elem (a::nil) = nil
| rem_elem(a::b::l) = a::rem_elem(b::l) ;

2//return largest element
fun larger_in_pair(list) = if list=[] then 0 else max(hd(list), larger_in_pair(tl(list)));

3//return sum
fun sumoflist (List []) =[]
| sumoflist (List(n,t1,t2)) = n + sumoflist(t1) + sumoflist(t2);

5//return min
fun list[] = []
min_no = reduce(lambda x,y: x if x < y else y, list)
print(min_no)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
Please do the following in python: Write a program (twitter_sort.py) that merges and sorts two twitter...
Please do the following in python: Write a program (twitter_sort.py) that merges and sorts two twitter feeds. At a high level, your program is going to perform the following: Read in two files containing twitter feeds. Merge the twitter feeds in reverse chronological order (most recent first). Write the merged feeds to an output file. Provide some basic summary information about the files. The names of the files will be passed in to your program via command line arguments. Use...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class Zillion implements a decimal counter that allows numbers with an effectively infinite number of digits. Of course the number of digits isn’t really infinite, since it is bounded by the amount of memory in your computer, but it can be very large. 1. Examples. Here are some examples of how your class Zillion must work. I’ll first create an instance of Zillion. The string...