functional-programmingocaml

How to write `result?1:0` in OCaml?


in java we can write result?1:0. It is a short way to get a value depending on the result bool.

How can I write such a thing in OCaml?


Solution

  • It sounds like this is a duplicate question. However, note that everything in OCaml is an expression. So the answer is if result then 1 else 0. You may need to parenthesize this depending on the context. (In the C family, a form I sometimes use for your expression is !!result.)