Expression matching Ocaml -
my question simple: how translate c code :
if (x == y + 1) // code else if (x == y - 1) // code else if (x == y + 2) ....
basically thinking of using pattern matching seemed appropriate. unfortunatly pattern doesn't work:
match x | y + 1 -> code | y - 1 -> code | y + 2 -> code | _ -> code
the compiler doesn't seem happy , have found out, expressions on pattenr matching did not tolerated. therefore tried putting them in values thusly:
let v1 = y + 1 in let v2 = y - 1 in let v3 = y + 2 in match x | v1 -> code | v2 -> code | v3 -> code | _ -> code
unfortunatly warnings saying values v1 v2 , v3 unused , the match cases use them unused too.
how can match expression other expressions?
thanks
actually, c code valid ocaml code.
if x = y + 1 (* code *) else if x = y - 1 (* code *) else if x = y + 2 ....
pattern matching not replacement if then
, has completly different purpose. ocaml allows construct types such type 'a option = none | of 'a
, pattern matching should used deconstruct types. sould not used other purposes.