我无法将我的大脑围绕尾递归,特别是在 ocaml 中,也解释了为什么人们在最后调用“in”函数。PS我说的是最基本的尾递归函数。
let rec some_none list =
match list with
| [] -> list
| hd::tl ->
if hd = 0 then
[None] @ some_none tl
else
[Some hd] @ some_none tl;;
Run Code Online (Sandbox Code Playgroud)
当我运行这个程序时它返回
Error: This expression has type int list but an expression was expected of type
'a option list
Type int is not compatible with type 'a option
Run Code Online (Sandbox Code Playgroud)
我怎样才能将常规更改a' list为a' option list?
ocaml ×2