我似乎根本无法弄清楚一个非常基本的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);
没有找到相关结果
已邀请:
2 个回复
碉罕城爸
只是一个布尔函数,它检查其参数是否相等。因此,例如,如果执行
,它将仅返回
(因为
不等于
),然后将该结果丢弃(因为在
之后还有其他表达式)。不会重新分配
。 SML中的变量是不可变的。如果要模拟可变变量,可以使用
和
运算符。但是,我不建议使用这种方法,因为它通常不是好的功能样式,在这种情况下也没有必要。最好的方法是用每个变量仅分配一次的方式重写代码。
疼嘶桐
表示或多或少在“某物内”,将标识符“ x”替换为“ y”的值。 您无法更改x的值。 进行替换(这不是实际的评估顺序或其他任何内容,但是应该可以让您了解正在发生的事情):
=>
=>
=>
->
=>
=>
无限循环来自何处。 您最好掌握有关ML编程的介绍性文字。