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 > 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
divandmodand 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
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.
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!
Indeed, and it is actually really simple once you understand the functional principles. Again I will be happy to give pointers on specific issues.