我一直在努力让这段代码返回一个((int * int) list)
选项。它取两个整数列表,并将它们组合成一个整数对列表。
let rec safe_zip (ls1: int list) (ls2: int list) : ((int * int) list) option =
match (ls1, ls2) with
| ([],[]) -> None
| ([],_) -> None
| (_,[]) -> None
| (h1::t1, h2::t2) ->
Some (List.append [(h1,h2)] (safe_zip (t1 t2)))
但是(safe_zip (t1 t1))
显示了一个错误,说
This expression has type int list -> (int * int) list option
but an expression was expected of type (int * int) list`
如何修复这个错误?
这个调用:
safe_zip (t1 t2)
传递一个参数给safe_zip
。但safe_zip
有两个参数
OCaml中的函数调用仅通过将表达式放在彼此旁边来表示。参数没有加括号
例如,添加两个列表:
List.append [1; 2] [3; 4]
注意两个参数之间没有圆括号
第二,注意safe_zip
不返回一个列表。它返回一个list option
。所以你不能把它的结果附加到另一个列表中。