表达式类型错误

Wrong type of the expression

这个问题与密切相关。 MyMap 的创建方式与该问题的 StringMap 相同。

# lsc;;
- : (MyMap.key * testtype) list =
[(("", "Test1"),
  {name = ""; state = ""; region = "Test1a"});
(("", "Test1a"),
  {name = ""; state = ""; region = "Test1b"});
(("", "Test2"),
  {name = ""; state = ""; region = "Test2"})]

我想创建一个函数,它会给我一个来自 lsc 的过滤列表。我想按照(x, _) _过滤。例如,它等于 Test1,那么函数将 return 记录列表

[{name = ""; state = ""; region = "Test1a"}; {name = ""; state = ""; region = "Test1b"}]

目前,我只想过滤 lsc 以获得

[(("", "Test1"),
  {name = ""; state = ""; region = "Test1a"});
(("", "Test1a"),
  {name = ""; state = ""; region = "Test1b"})]

MyMap.filter (fun (x,_) _ -> x="Test1") lsc;; 给我以下错误:

Error: This expression has type (MyMap.key * testtype) list
       but an expression was expected of type
         'a MyMap.t = 'a Map.Make(PaireCles).t

开始,我认为这是可行的方法,但我总是遇到同样的错误。有解决办法吗?

Map.filter function takes a function and a map data structure, not an association list, which you have. You can use List.filter过滤键*值对列表,

List.filter (fun (x,_) -> x = "Test1") lsc