-5
  1. My Exercise gave me this as the original coded.
  2. They then wanted us to do a java implementation of it.
  3. I thought that I had now I can't get any of it to make sense.
  4. There is a spot in this that uses a variable "n" that screws it all up
    somehow.
  5. This was my real problem unless there is more of it that can be debugged.
  6. 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
    
  7. This is the program that I attempted.

  8. 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);
        }
    }
    
sshine
  • 15,635
  • 1
  • 41
  • 66

1 Answers1

2

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:

  1. 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.

  2. 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.

  3. You could pass the parameters of the outer functions on to the inner functions using extra function arguments. E.g. let the function g take x and y as arguments, and let the function h take x, y and n as arguments.

    Those functions never need to change the variables outside their scope anyway, because they can rely on return values alone.

Community
  • 1
  • 1
sshine
  • 15,635
  • 1
  • 41
  • 66