我正在尝试用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
所以,我的函数签名有问题。但我找不到它是什么。你能帮我吗?
感谢
您的类型注释不正确。所有参数周围都需要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
不确定这是否也在ocaml中编译(is f#),但interactive没有抱怨的版本是:
let rec for2 (body: int->unit) iStart iEnd =
if iStart = iEnd then ()
else
body iStart;
for2 body (iStart+1) iEnd