Ocaml模式匹配/函数成员
下面的函数试图匹配Ocaml模式匹配/函数成员,ocaml,Ocaml,下面的函数试图匹配(int*stmt list)列表(其中stmt只是在别处定义的类型)并返回stmt列表 let rec findinlist (r: int) (l1: (int * stmt list) list ) (l2: stmt list) = (match l1 with | [] -> l2 | h:: _ when h= (r,s) -> s | _ :: t -> findinlist r t l2
(int*stmt list)列表
(其中stmt
只是在别处定义的类型)并返回stmt列表
let rec findinlist (r: int) (l1: (int * stmt list) list ) (l2: stmt list) =
(match l1 with
| [] -> l2
| h:: _ when h= (r,s) -> s
| _ :: t -> findinlist r t l2
)
首先,这会给我一个未绑定的值s错误,但我该如何实现这一点呢?当您使用
时,
它不是另一个绑定s
的模式匹配。它只是一个布尔测试,需要一个布尔值(比如if
语句)
也许你想要这样的东西:
let rec findinlist (r: int) (l1: (int * stmt list) list ) (l2: stmt list) =
(match l1 with
| [] -> l2
| (r',s):: _ when r' = r -> s
| _ :: t -> findinlist r t l2
)
更详细地说,@Sheeft所说的是,当采用布尔表达式时,
。它不执行模式匹配,它只是一个测试(谓词)。是的,我有点太快了,也许,我会将它添加到答案中(希望你允许)。我没有任何异议。很高兴在您做出更改后删除我的评论。