The following function gives a compilation error at the point I try to match an empty list:
let rec tuplesToList (acc: int list) (remaining: int*int list) =
match remaining with
| [] -> acc
| (a, b) :: tail -> tuplesToList (a :: b :: acc)
The error is:
This expression was expected to have type int * int list but here has type 'a list
This works fine when remaining is a simple list of ints rather than tuples. How can I match an empty list of tuples?
[]is just fine to match an empty list of tuples. But according to your type annotation,remainingis not a list of tuples, it’s a tuple containing an int and an int list. A list of tuples would be(int*int) list.int * int listis parsed asint * (int list), not(int * int) list.If you fix the type, your code should work fine.