如何在函数参数中指定列表类型?

Mik*_*son 4 f#

我有一个功能,需要两个列表,并生成一个笛卡尔积.

let cartesian xs ys = xs |> List.collect (fun x -> ys |> List.map (fun y -> x * y))
Run Code Online (Sandbox Code Playgroud)

我的问题是我传递了两个Int64类型的列表,我收到错误,因为该函数需要两个Int32类型的列表.

如何明确设置列表类型?

Tay*_*ood 5

在其中一个参数中添加类型注释应该有效:

let cartesian (xs: int64 list) ys =
  xs |> List.collect (fun x -> ys |> List.map (fun y -> x * y))
Run Code Online (Sandbox Code Playgroud)

或者,用于inline推断呼叫站点的类型:

let inline cartesian xs ys =
  xs |> List.collect (fun x -> ys |> List.map (fun y -> x * y))
> cartesian [1;2;3] [1;2;3];;
val it : int list = [1; 2; 3; 2; 4; 6; 3; 6; 9]
> cartesian [1L;2L;3L] [1L;2L;3L];;
val it : int64 list = [1L; 2L; 3L; 2L; 4L; 6L; 3L; 6L; 9L]
Run Code Online (Sandbox Code Playgroud)