当我尝试在ML中运行此程序时,我突然出现连续错误:
fun find(s,file) =
let fun findHelper(true, true, ch, w, file, acc, acc2) = TextIO.output(TextIO.stdOut, acc2^"\n")
| findHelper(b1, b2, ch, w, file, acc) = ch = valOf(TextIO.input1(TextIO.file)) acc2^str(ch)
if ch = "" then
if w = acc then b1 = true
else acc = ""
else if ch = "\n" then b2 = true
else acc^str(ch)
in
findHelper(false, false, "", s, file, "", "")
end
错误代码是:
project.sml:61.3 Error: syntax error: inserting LPAREN
project.sml:65.12 Error: syntax error: inserting RPAREN
我插了一堆括号无济于事,老实说,我甚至不知道为什么会出现这个错误。错误集中在"如果ch =""然后" ,但是ch的另一个实例没有错误,所以我不知道为什么它出错而不是另一个。
答案 0 :(得分:1)
语法错误是您的内部函数包含
形式的表达式 A if B then C else D
(其中A的形式为" x = y")。要按顺序计算表达式,必须使用分号运算符和括号,因此:
(A; if B then C else D)
然而,这只会让你超越语法错误,但不会有太多帮助。代码没有多大意义。你似乎在某种程度上假设你可以使用=
来分配变量 - 事实并非如此。 SML是一种函数式语言,变量是不可变的。你想要使用的是递归。