开发者

Converting Java to Standard ML (SML)

A couple of my friends are working on a simple recursive function in SML, and so far have failed to create it due to a lack of documentation of SML and its syntax. I've tried to find something myself in order to help them, but have been unsuccessful so far.

Here's the function I made in Java. It works and I'd like to convert the concept of this function into SML.

 private static int shift;
 private static boolean firstRun = true;

 private static void crackThatThing(int clearText, int cryptoText) {
  if (firstRun) { // Make sure that the shift is only set once
   firstRun = false;
   shift = ((cryptoText % 10) - (clearText % 10)) % 10;
   crackThatThing((clearText / 10), (cryptoText / 10));
  } else {
   if (clearText > 0 && cryptoText &开发者_StackOverflow中文版gt; 0) {
    if (shift != ((cryptoText % 10) - (clearText % 10)) % 10) {
     // The shift value changed - this is not a valid encryption!
     System.out.println("This is not a valid encryption!");
    } else {
     // If the shift value is the same as before, continue with the next number
     crackThatThing((clearText / 10), (cryptoText / 10));
    }
   } else {
    // The encryption is valid
    System.out.println("The encryption is valid. The shift is: " + shift);
   }
  }
 }

Any ideas?

Edit: Here's what I think it should be

The following code is based on absolutely no previous experience with SML whatsoever, and since I actually deleted the code I had written, this is based on the bits I can remember. I know it's wrong and very likely hideous code, but please bear with me on this one.

var notValid = "This is not a valid encryption!";
var valid = "The encryption is valid. The shift is: ";

var shift = 11; (* This is just to initialize it *)
var firstRun = true;

fun crackThatThing(clearText, cryptoText) =
if firstRun = true then
  firstRun = false andalso
  shift = ((cryptoText mod 10) - (clearText mod 10) mod 10) andalso
  crackThatThing(clearText div 10, cryptoText div 10)
else
  if clearText > 0 andalso cryptoText > 0 then
    if not (shift = ((cryptoText mod 10) - (clearText mod 10) mod 10)) then
      notValid
    else
      crackThatThing(clearText div 10, cryptoText div 10)
  else
    valid;


There exists plenty of books and resources on the net (See below).

You need the functions div and mod and then some general functional principles, such as recursion to solve this. I'm not going to give you any code for this as it is a weekly assignment. However I'll be more than happy to help on more specific issues not related to this assignment.

Links

  • A Gentle Introduction to ML
  • Tips for Computer Scientists on Standard ML (Revised)
  • The Standard ML Basis Library
  • Programming in Standard ML '97
  • Danish text: Supplerende noter i funktionsprogrammering (I can't find a link to the newest version)

All the examples I've seen only cover very simple if-else statements

Then I would dare to say that you haven't looked properly! See the list of links above, at least a few of them contains introductions to SML on different levels.

the documentation of SML is ridiculous compared to other languages.

You are not really referring to which documentation you are talking about, but I can only guess that it is not the definition/commentary. Anyways it seems you don't know what you are talking about!

My friends have been able to "convert" it to SML using two functions instead of just one, but it seems stupid to do that when it should be really simple.

Indeed, and it is actually really simple once you understand the functional principles. Again I will be happy to give pointers on specific issues.


From looking at your code, the main thing that I see is that you're trying to assign to variables (you wrote something like firstRun = false, but = is actually the equality operator). Variables in ML cannot be assigned to (so "variable" is a misnomer; they are actually constants in a sense). You can either re-write your code to not use mutation, or you can use explicit mutable cells (call "references"). For mutable cells, basically, you use the ref function to create a mutable cell with a given initial value. You use the ! operator to get the value of the cell; and you use the := operator to change the value inside the cell.

However, I think it would be a bad idea in your case to use mutable global state. It is bad style and it seems it would make your function not usable more than once. From what I can see, you use global variables to keep track of information as you make recursive calls to your function. You can instead define an inner function (local to the outer function) that does the recursion, and that way you can keep track of stuff in local variables of the outer function and/or pass additional variables when you recurse the inner function, without exposing state to the global scope.

Also you don't declare variables with var; you probably want val

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜