麻烦连接函数的结果

kin*_*nix 3 f#

我的教授让我们用F#实现笛卡尔积.他正在为我们提供一种类型:

type SET =
   | I of int list                                  // I [1;2;3]
   | S of string list                               // S ["a";"b";"c"]
   | IS of (int * string) list                      // IS [(1, "a");(2, "b")]
   | II of (int * int) list                         // II [(1,2); (3,4); (5,6)]
   | SS of (string * string) list                   // SS [("a","b"); ("c","d")]
   | SI of (string * int) list                      // SI [("a", 1); ("b", 2); ("c", 3)]
   | SISI of ((string * int) * (string * int)) list // SISI [(("a", 1), ("b", 2)); (("c", 3), "d", 4))]
   | SIIS of ((string * int) * (int * string)) list // SIIS [(("a", 1), (2, "b")); (("c", 3), (4, "d"))]
Run Code Online (Sandbox Code Playgroud)

在这样做的过程中,我们还获得了一个"产品"功能,其行为如下:

let product s1 s2 =
  match (s1, s2) with
    | (I s1, I s2) -> II (pairs s1 s2)
    | (S s1, S s2) -> SS (pairs s1 s2)
    | (I s1, S s2) -> IS (pairs s1 s2)
    | (S s1, I s2) -> SI (pairs s1 s2)
    | (SI s1, IS s2) -> SIIS (pairs s1 s2)
    | (SI s1, SI s2) -> SISI (pairs s1 s2)
Run Code Online (Sandbox Code Playgroud)

赋值是创建一个"对"的函数,它组装SET元素的笛卡尔积.我们被告知要创建一个可以在列表中分配值的函数.我这样做了:

let rec dist a L =
    match L with
    | [] -> []
    | h::t -> (a,h) :: dist a t
Run Code Online (Sandbox Code Playgroud)

到目前为止,我的函数看起来像这样:

let rec pairs s1 s2 = 
    match s1 with
    | [] -> []
    | h::t -> dist h s2 // AND (pairs t s2)
Run Code Online (Sandbox Code Playgroud)

我不明白我需要如何修改匹配语句的最后一行以正确反映我们想要的输出.这将成功完成部分功能,但只有第一位(与列表头分布的事实相关,但不是尾部).如何将此函数在s1头上的第一个循环的结果连接到'pair t s2',以便在第一个段中创建的元素将以防止预期类型冲突的方式匹配第二个段?

kin*_*nix 5

对不起,这是一个非常愚蠢的问题.如果任何人都可以帮助类似的案例,可以通过将最后一行写为:

let rec pairs s1 s2 = 
    match s1 with
    | [] -> []
    | h::t -> (dist h s2) @ (pairs t s2)
Run Code Online (Sandbox Code Playgroud)

我之前尝试过使用::运算符进行所有连接,但这是此操作的错误操作符类型.