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