- My Exercise gave me this as the original coded.
- They then wanted us to do a java implementation of it.
- I thought that I had now I can't get any of it to make sense.
- There is a spot in this that uses a variable "n" that screws it all up
somehow. - This was my real problem unless there is more of it that can be debugged.
I am just trying to learn this stuff before my final next week.
fun f x y = let val a = x+1 fun g 0 = 0 | g n = let val a = x+y fun h 0 = 0 | h k = a+n+g(n-1) in h (n-1) end in if x = 0 then g y else a + g(f (x-1) y) end
This is the program that I attempted.
The original version of the ML code that I am supposed to implement is above.
public class f { static double k; static double x; static double y; static double a, a2; static double n; public static double fn(double x, double y) { a = x + 1; if(x == 0) g(y); return a + g(fn((x-1), y)); } public static double g(double n) { if(n == 0) return 0; a2 = (x + y); f.n = h(n-1); return f.n; } public static double h(double k) { if(k==0) return 0; else { k = a2 + n + g(n-1); return k; } } public static void main(String[] args) { /*double arg2[] = null; for (int i = 0; i < args.length; i++) { arg2[i] = Double.parseDouble(args[i]); }*/ fn(2,1); } }

- 15,635
- 1
- 41
- 66

- 1
- 1
-
1What are you trying to do? What's the problem? Please be specific and clear in asking questions. – progyammer Dec 06 '16 at 18:03
-
I am not so sure about what the program is supposed to do. Either I am learning ML in one of my classes and this is just an exercise that is supposed to help me but it is making me more confused. – Chad Zessin Dec 06 '16 at 18:09
-
What do you mean you want to write a ML program in Java? Are you trying to translate the ML code into Java? – EJoshuaS - Stand with Ukraine Dec 06 '16 at 18:30
1 Answers
Asking for homework help on StackOverflow is a sensitive subject, which is probably the reason why you got all the downvotes. See this guide for certain expectations of the asker:
- Make a good faith attempt to solve the problem yourself first. ✓
- Ask about specific problems with your existing implementation. ✗
- Admit that the question is homework. ✓
- Be aware of school policy. ✗ (I somehow doubt this, at least.)
- Never use code you don't understand. □ (Leaves to be seen.)
How do I write an ML program in Java?
You... don't. You either write an ML program, or you write a Java program.
Converting this ML program into a Java program isn't trivial because the ML program uses nested scopes, which is somewhat atypical in Java code. This is supposedly exaggerated for the purpose of the exercise.
Here are some suggestions:
Do not use a section of static variables:
static double k; static double x; static double y; static double a, a2; static double n;
They don't correspond very well to the lexical scoping of ML, and you risk updating them from the wrong scope.
You can use the lexical scoping of lambda expressions in Java. This would not be idiomatic for Java, but it would make the translation more direct.
You could pass the parameters of the outer functions on to the inner functions using extra function arguments. E.g. let the function
g
takex
andy
as arguments, and let the functionh
takex
,y
andn
as arguments.Those functions never need to change the variables outside their scope anyway, because they can rely on return values alone.