开发者

Matching of tuples

开发者 https://www.devze.com 2023-01-03 13:06 出处:网络
From what I understood I can use pattern-matching in a match ... with expression with tuples of values, so something like

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消