Search code examples
pattern-matchingocamltuples

Matching of tuples


From what I understood I can use pattern-matching in a match ... with expression with tuples of values, so something like

match b with
  ("<",  val) -> if v < val then true else false
| ("<=", val) -> if v <= val then true else false

should be correct but it gives me a syntax error as if the parenthesis couldn't be used:

File "ocaml.ml", line 41, characters 14-17: Error: Syntax error: ')' expected

File "ocaml.ml", line 41, characters 8-9: Error: This '(' might be unmatched

referring on first match clause..

Apart from that, can I avoid matching strings and applying comparisons using a sort of eval of the string? Or using directly the comparison operator as the first element of the tuple?


Solution

  • val is a reserved keyword in OCaml, so you can't use it as a variable name. If you use something else instead of val, it will work.

    As a side note: if condition then true else false is equivalent to condition.