对于 OCaml 中没有整数的语句
For statement in OCaml without integers
出于学习原因,我想在 OCaml 中使用 for 循环而不是递归。
我研究了一下并应用了一个例子 (https://ocaml.org/learn/tutorials/camlp4_3.10/foreach_tutorial.html):
let a_list = ["hello"; "world"] in
for s in a_list do
print_endline s
done
这是有道理的。但是,当我查看它时(例如 Visual Studio),它告诉我 's' 是一个 int 变量。因此,我不能做类似的事情(其中 'a'、'b'、... 是任何类型的变量):
let a_list = [a;b;c...] in
for s in a_list do
match s with
...
done
有没有办法像Python那样使用for语句?我的意思是,使用 's in list' 并且 s 的类型将是它的类型而不是 'int'?
有些东西我在那里没有看到。
提前致谢!
OCaml中没有foreach。递归是惯用的风格,或者如果你真的想避免递归,你可以使用 while
循环。 For 循环只迭代整数。
您可能对所链接教程的性质感到困惑:这是一个为 OCaml 实现(旧式)语法扩展的教程,而不是在 OCaml 中使用 for
循环的教程.
出于学习原因,我想在 OCaml 中使用 for 循环而不是递归。
我研究了一下并应用了一个例子 (https://ocaml.org/learn/tutorials/camlp4_3.10/foreach_tutorial.html):
let a_list = ["hello"; "world"] in
for s in a_list do
print_endline s
done
这是有道理的。但是,当我查看它时(例如 Visual Studio),它告诉我 's' 是一个 int 变量。因此,我不能做类似的事情(其中 'a'、'b'、... 是任何类型的变量):
let a_list = [a;b;c...] in
for s in a_list do
match s with
...
done
有没有办法像Python那样使用for语句?我的意思是,使用 's in list' 并且 s 的类型将是它的类型而不是 'int'?
有些东西我在那里没有看到。
提前致谢!
OCaml中没有foreach。递归是惯用的风格,或者如果你真的想避免递归,你可以使用 while
循环。 For 循环只迭代整数。
您可能对所链接教程的性质感到困惑:这是一个为 OCaml 实现(旧式)语法扩展的教程,而不是在 OCaml 中使用 for
循环的教程.