Zee
Zee

Reputation: 1420

Ocaml pattern matching/ member of function

The following function is trying to match a (int * stmt list) list (where stmt is just a type defined elsewhere) and return a 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
      )    

First off this gives me an unbound value s error but how would I go about accomplishing this?

Upvotes: 1

Views: 187

Answers (1)

Théo Winterhalter
Théo Winterhalter

Reputation: 5108

When you use when it is not another pattern matching which would bind s. It is just a boolean test that expects a boolean (like an if statement).

Maybe you want something like:

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
  ) 

Upvotes: 2

Related Questions