LANGUAGE: OCAML In this problem, we will assume a language with statements like
ID: 647897 • Letter: L
Question
LANGUAGE: OCAML
In this problem, we will assume a language with statements like in Java or C but in which we can represent lists and use functions on them like in OCaml. In this language, we can write the following program fragment to reverse a list
The task in this problem is to using the ideas we discussed in class and that you used in Lab 9 to eventually write a let declaration that binds the identifier revprog to the encoding of this program in OCaml. However, you should do it in the following steps:
Explain (in a comment in the hw5.ml file) what the representation of a state would be for this program fragment. Then provide a type declaration that defines the type state based on the representation you have chosen. Assume for this part that all our lists are integer lists.
Write the get and put functions for each of the variables used in this program based on your encoding of the state.
Write a let declaration that binds the identifier revprog to the encoding in OCaml of the program fragment shown. You may use the definitions of seq, ifstat, and whilestatprovided to you in the file impprog.ml that you will find in the code subdirectory of the public repository for this course.
Using revprog, define a function revlist : (int list) -> (int list) that takes an integer list as input and returns its reverse as output. An example interaction using this function would be the following:
Your definition of revlist must use revprog to get credit, simply defining a function for reversing integer lists will not be sufficient.
Explanation / Answer
public class restr {
private static String hello = "Hello World";
public static void main(String[] args)
{
System.out.println(restr(hello));
}
public static String restr(String s) {
char c[] = s.toCharArray();
int i = 0, j = c.length - 1;
while (i < j) {
char tmp = c[i];
c[i] = c[j];
c[j] = tmp;
i++;
j--;
}
return new String(c);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.