Ocaml 模式匹配/函数成员

Ocaml pattern matching/ member of function

以下函数试图匹配 (int * stmt list) list(其中 stmt 只是别处定义的类型)和 return stmt list.

 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
      )    

首先这给了我一个未绑定值的错误,但我将如何完成这个?

当您使用 when 时,它不是另一个会绑定 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
  )