Question

ML or java please assume that you have the following datatype datatype 'a mylist = NIL...

ML or java please
assume that you have the following datatype
datatype 'a mylist = NIL | CONS of'a * 'a mylist;
write a function reverse of type 'a mylist -> 'a mylist that takes a mylist a and returns my list of all the elements lf a, in jnverse order
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java code

import java.util.ArrayList;
public class MyList {

   public static void main(String[] args) {
       // TODO Auto-generated method stub

      
  
      
       ArrayList<Integer> list = new ArrayList<Integer>();
      
       for(int i=0;i<10;i++) {
           list.add(i);
       }
      
       System.out.println("List Before passing to function : \n");
       print(list);
      
       list = reverse(list);
      
       System.out.println("List After passing to function : \n");
       print(list);
      
   }

   private static void print(ArrayList list) {
      
      
       for(int i=0;i<list.size();i++)
           System.out.print(list.get(i)+" ");
      
       System.out.println();
   }

   private static ArrayList reverse(ArrayList list) {
       // TODO Auto-generated method stub
       ArrayList result = new ArrayList<>();
      
       for(int i=list.size()-1;i>=0;i--) {
          
           result.add(list.get(i));
       }
      
       return result;
   }

}

Output

List Before passing to function :

0 1 2 3 4 5 6 7 8 9
List After passing to function :

9 8 7 6 5 4 3 2 1 0

Screenshot

Feel free to ask any doubts, if you face any difficulty in understanding.

Please upvote the answer if you find it helpful

Add a comment
Know the answer?
Add Answer to:
ML or java please assume that you have the following datatype datatype 'a mylist = NIL...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please place all of the following functions (defun …) into a SINGLE .lsp file As such,...

    Please place all of the following functions (defun …) into a SINGLE .lsp file As such, be sure to use the EXACT SAME function names and parameter numbers and orders I provide ​ Write a function ONEFIB that takes a single parameter n and returns the nth Fibonacci number. Write a function ALLFIB that takes a single parameter n and returns a list of the first n Fibonacci numbers. Do not worry about efficiency here. HINT: You can use ONEFIB...

  • *********************Java recursion********************** Plz help me write a method in java that traverse Through a integer linked...

    *********************Java recursion********************** Plz help me write a method in java that traverse Through a integer linked list recursively . The method returns a string with every element in the list and a space in between each element in reverse order. THE METHOD DOES NOT REVERSE THE LINKED LIST. IT JUST RETURNS A STRING CONTAINING THE ELEMENTS OF THE LIST IN REVERSE ORDER. ASSUME ALL THE ELEMENTS IN THE LINKED LIST ARE int... method signature is something like : public String...

  • Consider the list myList: myList = ["a", "America", "1", [5,3,9], "3", ["Mercy"]] #1 1) The Python...

    Consider the list myList: myList = ["a", "America", "1", [5,3,9], "3", ["Mercy"]] #1 1) The Python statement __________________ returns a #2 2) The Python statement __________________ returns America #3 3) The Python statement __________________ returns Ame #4 The type of the third element is _______. #6 The Python statement __________________ returns the following: ['Mercy'] #7 The python statement temp = _______ converts the third element to integer and assigns the result to temp. #8 Consider the fourth element of myList...

  • Assume you have the following declaration (in main) : int num[10] [7]; Assume that array num...

    Assume you have the following declaration (in main) : int num[10] [7]; Assume that array num is filled completely. Write functions to perform each of the following: a) A function that prints all elements in the array that are greater than 10 or less than 50. b) A function that finds the largest number in the array and returns its subscript. c) A function that finds and returns the average of all the numbers in the array. d) A function...

  • Implement the following methods (based on ArrayLists): (JAVA) a) merge method that takes two ArrayLists as...

    Implement the following methods (based on ArrayLists): (JAVA) a) merge method that takes two ArrayLists as input and returns the merged ArrayList containing the elements from both the input lists. b) enumerate method that takes an ArrayList as input and prints the enumerated contents of the input list. c) reverse method that takes an ArrayList as input and returns an ArrayList with the contents of input list in reverse order. Test your method implementations.

  • b) Write the function as Java code, including comments as you see fit. This problem concerns...

    b) Write the function as Java code, including comments as you see fit. This problem concerns creating a function String secondInOrder (Node start) which takes a linked list of Node elements (shown below), and returns the String in the list which comes second in alphabetic order. Recall that the String class has the method public int compareTo(String anotherString) which returns O if the strings are equal, a negative integer if this is earlier than anotherString and a positive integer otherwise....

  • require python 5. You have the following list stored in variable myList [ 3, 7, -1,...

    require python 5. You have the following list stored in variable myList [ 3, 7, -1, 2, -10, 6, 8) i. Write a program that creates a new list, with the same numbers as mylist, apart from any negative numbers, which should be removed. Although you are using mylist as an example, it should work for any list of numbers. ii. Now adjust your program so that instead of deleting them, any negative numbers are replaced with a zero.

  • Please help with all four questions regarding LISP Programming. Thank you. Please answer all questions with...

    Please help with all four questions regarding LISP Programming. Thank you. Please answer all questions with output plesase. LISP Programming Assignment It is a good idea to start this assignment early; Lisp programming, while not inherently difficult, often seem somewhat foreign at first, particularly when it comes to recursion and list manipulation. This assignment is loosely based on material by Dr. Henri Casanova.   Problem #1 Define a function that takes two arguments and returns the greater of the two. Problem...

  • 20. [5 points] Rewrite the following ML. function using patterns fun factn. ifn-0 then 1 else n fact (n-1) 21. [S points) Using map function, write an ML function int2real of type int list real l...

    20. [5 points] Rewrite the following ML. function using patterns fun factn. ifn-0 then 1 else n fact (n-1) 21. [S points) Using map function, write an ML function int2real of type int list real list that takes a list of integers and returns the same numbers converted to real. For example, if the input is [1,2,3], the output should be like [1.0,2.0,3.0 22. [5 points] Using foldr function, write a function duplist of type·a list 'a list that takes...

  • in ml language or sml Exercise 14 Write a function maxpairs of type (int * int)...

    in ml language or sml Exercise 14 Write a function maxpairs of type (int * int) list -> int lise that takes a list of pairs of integers and returns the list of the max elements from each pair. For example, if you evaluate maxpairs ((1,3), (4,2), (-3,-4)] you should get [3,4,-3).

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT