一个非常基本的SML问题,我只能';似乎不明白(小代码)
只是一个基本的卡塞尔密码。我已经测试了所有的子函数,只是encryptChar()并不特别有效。我得到一个无限循环。它应该是递归的。以下是所有代码:一个非常基本的SML问题,我只能';似乎不明白(小代码),sml,ml,Sml,Ml,只是一个基本的卡塞尔密码。我已经测试了所有的子函数,只是encryptChar()并不特别有效。我得到一个无限循环。它应该是递归的。以下是所有代码: 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 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);
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=
设val AsciCode=0
在里面
如果(不是(String.sub(msgStr,index)=#“”),那么
(
ascicode=ord(String.sub(msgStr,index))+shiftAmnt;
如果(ASCICODEord(#“Z”)),则ASCICODE=ASCICODE-26
else ASCICODE=ASCICODE;
msgStr=替换(msgStr,索引,chr(ASCICODE))
)
else ASCICODE=ASCICODE;
指数=指数+1;
如果(索引<(大小msgStr-1)),则encryptChar(msgStr,shiftAmnt,索引)
else msgStr
结束
;
fun encrypt(msgStr:string,shiftAmnt:int):string=encryptChar(string.map Char.toUpper msgStr,shiftAmnt mod 26,0);
这里的问题是您误用了=
。在变量定义之外,=
只是一个布尔函数,它会检查其参数是否相等。因此,如果您这样做,例如ascicode=ord(String.sub(msgStr,index))+shiftAmnt;
,它只会返回false
(因为asciocode
不等于ord(String.sub(msgStr,index))+shiftAmnt
),然后丢弃该结果(因为在;
之后有其他表达式)。它不会重新分配asciocode
)
SML中的变量是不可变的。如果您想模拟可变变量,可以使用
ref
s和:=
运算符。但是我不推荐这种方法,因为它通常不是很好的函数样式,在这种情况下也不是必需的。更可取的方法是重写代码,使每个变量都是唯一的分配一次。这的确是非常基本的,令人惊讶的是,你在如此复杂的情况下遇到了它。你是从其他语言移植过来的吗 你需要忘记你所知道的关于使用作业编程的一切
let val x = y in something
表示或多或少的“在“某物”中,用“y”值替换标识符“x”。您无法更改x的值 进行替换(这不是实际的评估顺序或任何东西,但它应该让您了解发生了什么): => => => 这就是你无限循环的来源 您最好掌握一篇关于ML编程的介绍性文章
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)