Несколько моих друзей работают над простой рекурсивной функцией в SML, и до сих пор не смогли ее создать из-за отсутствия документации по SML и его синтаксису.Я пытался найти что-то сам, чтобы помочь им, но пока безуспешно.
Вот функция, которую я сделал в Java.Это работает, и я хотел бы преобразовать концепцию этой функции в 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);
}
}
}
Любые идеи?
Редактировать: Вот то, что я думаю, должно быть
Следующий код основан на абсолютно отсутствии предыдущего опыта работы с SML, и, поскольку я фактически удалил написанный код, он основан на битах, которые я могу запомнить.Я знаю, что это неправильный и очень вероятный отвратительный код, но, пожалуйста, потерпите меня на этом.
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;