Question

In: Computer Science

SML Complete the following programs. 1. Write a function listify that takes a list and returns...

SML Complete the following programs.

1. Write a function listify that takes a list
and returns a list of lists
where each element of first list
becoming its own single-element list

(Fill in the code here)

fun main() = let
val lst = (explode "rad");
in print (PolyML.makestring ( listify lst )) end;

2. Write a function splitlist that takes a list
of pairs and returns a pair of lists
that has the firsts of the pairs in one list
and the seconds of the pairs in the other list

(Fill in the code here)

fun main() = let
val lst = [("a","b"),("c","d"),("e","f")];
in print (PolyML.makestring ( splitlist lst )) end;

3. Write a function sumPairs that takes a list
of pairs of ints and returns a list
of the sum of each pair

(Fill in the code here)

fun main() = let
val lst = [(0,1),(1,0),(10,10)];
in print (PolyML.makestring ( sumPairs lst )) end;

Solutions

Expert Solution

NOTE:: If you have any query please ask in the comments section but do not give negative rating to the question as it affects my answering rights......I will help you out with your doubts...the program works fine i have tested it already.. you just need to copy pate them......GIVE A THUMBS UP !! if you like it

(1)

#include <iostream>
#include <list>
using namespace std;
list<list<int>> listify(list<int> lst){
    list<list<int>> result;
    list<int> tmp;//to make a temporary list which is then pushed to result 
    for(auto l:lst){
        tmp.push_back(l);
        result.push_back(tmp);
        tmp.clear();
    }
    return result;
}
int main() {
    list <int> lst={1,2,3,4,5,6};
    list<list<int>> res=listify(lst);
    cout<<"The result has following list of lists: ";
    for(auto i:res){
        cout<<"\nlist elements: ";
        for(auto j:i){
            cout<<j<<" ";
        }
    }
    return 0;
}

Output:-->

(2)

#include <iostream>
#include <list>
#include <vector>
using namespace std;
vector<pair<list<int>,list<int>>> splitlist(list<pair<int,int>> lst){
    vector<pair<list<int>,list<int>>> result;
    list<int> tmp1,tmp2;
    for(auto i:lst){
        tmp1.push_back(i.first);
        tmp2.push_back(i.second);
        result.push_back({tmp1,tmp2});
        tmp1.clear();
        tmp2.clear();
    }
    return result;
}
int main() {
    list <pair<int,int>> lst={{1,2},{3,4},{5,6},{7,8}};
    vector<pair<list<int>,list<int>>> res=splitlist(lst);
    cout<<"The result of the split function: ";
    for(auto i:res){
        cout<<"\nPair::\n";
        cout<<"\tfirst list elements: ";
        for(auto j:i.first){
            cout<<j<<" ";
        }
        cout<<"\n\tsecond list elements: ";
        for(auto k:i.second){
            cout<<k<<" ";
        }
    }
    return 0;
}

Output:-->

(3)

#include <iostream>
#include <list>
using namespace std;
list<int> sumPairs(list<pair<int,int>> lst){
    list<int> result;
    for(auto i:lst){
        result.push_back(i.first+i.second);
    }
    return result;
}
int main() {
    list <pair<int,int>> lst={{1,2},{3,4},{5,6},{7,8}};
    cout<<"The list of pairs is: "<<endl;
    for(auto i:lst){
       cout<<"{"<<i.first<<" "<<i.second<<"}"<<"\t";
    }
    list<int> res=sumPairs(lst);
    cout<<"\nThe result of the sumPairs function: "<<endl;
    for(auto i:res){
       cout<<i<<" ";
    }
    return 0;
}

Output:-->


Related Solutions

Write a function that takes a list of integers as input and returns a list with...
Write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2] Do not use any special or built in functions like append, reverse etc.
1.Write a function div7(lst) which takes in a list of integers, and returns a list of...
1.Write a function div7(lst) which takes in a list of integers, and returns a list of booleans of the same length, such that for each integer in the original list, the boolean in the output list is True if that integer was divisible by 7, or False if not. Use list comprehensions in python, the function only could be at most two lines long. Here is some examples: >>> div7([14, 5, 7, 3, 29, 28, 10]) [True, False, True, False,...
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
We were asked this: Write a function that takes a list, and returns a list representing...
We were asked this: Write a function that takes a list, and returns a list representing each word whose reverse is also in the list. def find_reversals(lst: List[str]) -> List[str]: Each pair, such as 'abut', 'tuba', should be represented by the first element encountered. Don't report the same pairs twice. Don't list palindromes. I wrote this, which might not be optimal but passes the unit test! def find_reversals(lst): #Place to save to found_reversals=[]    #Make each string lowercase for i...
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
Use Scheme Language Write a Scheme function that takes a list and returns a list identical...
Use Scheme Language Write a Scheme function that takes a list and returns a list identical to the parameter except the third element has been deleted. For example, (deleteitem '(a b c d e)) returns ‘(a b d e) ; (deleteitem '(a b (c d) e)) returns ‘(a b e).
Write a LISP function COUNTX which takes an atom and a list and returns the number...
Write a LISP function COUNTX which takes an atom and a list and returns the number of top-level occurrences of the atom in the list. For example: (COUNTX ‘A ‘(A (A B) B A B A (B A)) Returns the value 3, the other two A’s are not at the top level
In DrRacket Write a function, removeAll, which takes two lists, list-a and list-b and returns a...
In DrRacket Write a function, removeAll, which takes two lists, list-a and list-b and returns a list containing only the items in list-a that are not also in list-b. E.g., (remove-all '(a b b c c d) '(a c a)) -> '(b b d)
Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT