我似乎根本无法弄清楚一个非常基本的SML问题(小代码)

|| 只是基本的Casaer密码。我已经测试了所有子功能,只是cryptoChar()并不是特别有用。我得到一个无限循环。它应该是递归的。这是所有代码:
fun replace (str : string, index : int, newChar : char) : string = String.substring(str,0,index) ^ String.str(newChar) ^ String.substring(str,index+1,(size str) - index - 1;

fun encryptChar (msgStr : string, shiftAmnt : int, index : int) : string =   
    let val asciiCode = 0  
    in  
        if (not (String.sub(msgStr, index) = #\" \")) then  
        (  
            asciiCode = ord( String.sub(msgStr, index) ) + shiftAmnt;  
            if (asciiCode < ord(#\"A\")) then asciiCode = asciiCode + 26  
            else if (asciiCode > ord(#\"Z\")) then asciiCode = asciiCode - 26  
            else asciiCode = asciiCode;  
            msgStr = replace(msgStr, index, chr(asciiCode))  
        )  
        else asciiCode = asciiCode;  
        index = index + 1;  
        if (index < (size msgStr - 1)) then encryptChar(msgStr, shiftAmnt, index)  
        else msgStr  
    end  
;  

fun encrypt(msgStr : string, shiftAmnt : int) : string = encryptChar (String.map Char.toUpper msgStr, shiftAmnt mod 26, 0);
    
已邀请:
这里的问题是您误用了“ 1”。在变量定义之外,
=
只是一个布尔函数,它检查其参数是否相等。因此,例如,如果执行
asciiCode = ord( String.sub(msgStr, index) ) + shiftAmnt;
,它将仅返回
false
(因为
asciiCode
不等于
ord( String.sub(msgStr, index) ) + shiftAmnt
),然后将该结果丢弃(因为在
;
之后还有其他表达式)。不会重新分配
asciiCode
。 SML中的变量是不可变的。如果要模拟可变变量,可以使用
ref
:=
运算符。但是,我不建议使用这种方法,因为它通常不是好的功能样式,在这种情况下也没有必要。最好的方法是用每个变量仅分配一次的方式重写代码。     
这确实是非常基本的,令人惊讶的是您在如此复杂的情况下遇到了它。 您从其他语言移植了吗? 您需要忘记所有有关使用赋值编程的知识。
let val x = y in something
表示或多或少在“某物内”,将标识符“ x”替换为“ y”的值。 您无法更改x的值。 进行替换(这不是实际的评估顺序或其他任何内容,但是应该可以让您了解正在发生的事情):
   encryptChar(\"THIS\", amount, 0)
=>
let val asciiCode = 0  
in  
    if (not (String.sub(\"THIS\", 0) = #\" \")) then  
    (  
        asciiCode = ord( String.sub(\"THIS\", 0) ) + amount;  
        if (asciiCode < ord(#\"A\")) then asciiCode = asciiCode + 26  
        else if (asciiCode > ord(#\"Z\")) then asciiCode = asciiCode - 26  
        else asciiCode = asciiCode;  
        \"THIS\" = replace(\"THIS\", 0, chr(asciiCode))  
    )  
    else asciiCode = asciiCode;  
    0 = 0 + 1;  
    if (0 < (size \"THIS\" - 1)) then encryptChar(\"THIS\", amount, 0)  
    else str 
end ;
=>
        if (not (String.sub(\"THIS\", 0) = #\" \")) then  
        (  
            0 = ord( String.sub(\"THIS\", 0) ) + amount;  
            if (0 < ord(#\"A\")) then 0 = 0 + 26  
            else if (0 > ord(#\"Z\")) then 0 = 0 - 26  
            else 0 = 0;  
            \"THIS\" = replace(\"THIS\", 0, chr(0))  
        )  
        else 0 = 0;  
        0 = 0 + 1;  
        if (0 < (size \"THIS\" - 1)) then encryptChar(\"THIS\", amount, 0)  
        else str  
=>
        if (not (String.sub(\"THIS\", 0) = #\" \")) then  
        (  
            0 = ord( String.sub(\"THIS\", 0) ) + amount;  
            if true then false
            else if false then false
            else true;
            false
        )  
        else true;  
        false;  
        if (0 < (size \"THIS\" - 1)) then encryptChar(\"THIS\", amount, 0)  
        else \"this\" 
->
        if (not false) then  
        (  
            false;
            false;
            false
        )  
        else true;  
        false;  
        if true then encryptChar(\"THIS\", amount, 0)  
        else \"THIS\" 
=>
        (  
            false;
            false;
            false
        )  
        false;  
        encryptChar(\"THIS\", amount, 0)
=>
    encryptChar(\"THIS\", amount, 0)
无限循环来自何处。 您最好掌握有关ML编程的介绍性文字。     

要回复问题请先登录注册