我正在尝试用ml编写一个函数,就像for循环一样(是的,我知道它不是语言应该如何工作)。所以这是我目前的代码:
fun for (f:int->unit) start_i:int end_i:int =
let fun for2 (f:int->unit) start_i:int end_i:int i:int =
if i=end_i - 1 then
f i
else
(f i;
for2 f start_i end_i (i + 1))
in
for2 f start_i end_i start_i
end
但是sml(和Ocaml也是)给了我这个错误:
test.ml:1.2-1.5 Error: syntax error: replacing FUN with VAL
test.ml:2.6-2.9 Error: syntax error: replacing FUN with VAL
所以,我的功能签名有问题。但我无法找到它是什么。你能救我吗?
由于
答案 0 :(得分:1)
您的类型注释不正确。你需要围绕所有参数的parens。
fun for (f:int->unit) (start_i:int) (end_i:int) =
let fun for2 (f:int->unit) (start_i:int) (end_i:int) (i:int) =
if i=end_i - 1 then
f i
else
(f i;
for2 f start_i end_i (i + 1))
in
for2 f start_i end_i start_i
end
答案 1 :(得分:-1)
不确定这是否也会在ocaml中编译(是f#),但是交互式不抱怨的版本是:
let rec for2 (body: int->unit) iStart iEnd =
if iStart = iEnd then ()
else
body iStart;
for2 body (iStart+1) iEnd