当我将函数应用于OCaml中的已知参数列表时,如何避免警告?

P S*_*ved 2 ocaml

我怎样才能改变几个值a,b,c等来a',b',c'等,这样x'=f(x)?这些值绑定到特定名称,并且它们的数量在编译时是已知的.

我尝试以下列方式将函数应用于列表:

let [a';b'] = List.map f [a;b] in ...
Run Code Online (Sandbox Code Playgroud)

但它会产生警告:

Warning P: this pattern-matching is not exhaustive.                                                                                                         
Here is an example of a value that is not matched:                                                                                                          
[]
Run Code Online (Sandbox Code Playgroud)

有什么办法可以避免吗?

zrr*_*zrr 7

您可以编写一些函数来映射到统一元组,即:

let map4 f (x,y,z,w) = (f x, f y, f z, f w)
let map3 f (x,y,z) = (f x, f y, f z)
let map2 f (x,y) = (f x, f y)
Run Code Online (Sandbox Code Playgroud)

然后你可以在需要时使用它们.

let (x',y') = map2 f (x,y)
Run Code Online (Sandbox Code Playgroud)