我试图匹配从List.Assoc.find
返回的浮点数(浮点选项)。
我正在使用模式匹配,但似乎不起作用。
let evalVar (_x: string) (_q:envQueue): float =
match List.Assoc.find _q _x with
Some(s) -> s
| None -> 0.0
我收到此错误:
Error: This pattern matches values of type 'a option
but a pattern was expected which matches values of type
equal:(string -> string -> bool) -> float option
答案 0 :(得分:1)
List.Assoc.find
的签名是:
utop # List.Assoc.find;;
- : ('a, 'b) List.Assoc.t -> equal:('a -> 'a -> bool) -> 'a -> 'b option =
<fun>
List.Assoc.find
期望equal
参数是一个函数。
Ocaml将代码List.Assoc.find _q _x
解释为:
_q
:是第一个参数,因此其类型为('a, 'b) List.Assoc.t
。
由于未提及equal
,_x
是上述签名的第三个参数,因此它的类型为字符串(由函数{的类型约束指定) {1}}。
以下是evalVar
的示例:
List.Assoc.find