如何匹配空元组列表?

Mar*_*son 1 f# tuples list

以下函数在我尝试匹配空列表时给出了编译错误:

let rec tuplesToList (acc: int list) (remaining: int*int list) =
    match remaining with
    | [] -> acc
    | (a, b) :: tail -> tuplesToList (a :: b :: acc)
Run Code Online (Sandbox Code Playgroud)

错误是:

This expression was expected to have type int * int list but here has type 'a list
Run Code Online (Sandbox Code Playgroud)

remaining一个简单的ints 列表而不是元组时,这很好用.如何匹配空元组列表?

sep*_*p2k 5

[]可以匹配一个空的元组列表.但是根据你的类型注释,remaining它不是一个元组列表,它是一个包含int和int列表的元组.元组列表将是(int*int) list.int * int list被解析为int * (int list),而不是(int * int) list.

如果您修复了类型,您的代码应该可以正常工作.