Our first example will demonstrate various value types in OCaml, including strings, integers, floats, and booleans. Here’s the full source code:
To run the program, save the code in a file named values.ml and use the OCaml compiler:
Let’s break down the code:
In OCaml, strings are concatenated using the ^ operator.
For integer arithmetic, we use standard operators like +.
For floating-point arithmetic, we use special operators like /. for division.
Boolean operations use && for AND, || for OR, and not for negation.
We use Printf.printf for formatted output, similar to C’s printf function.
Note that OCaml is a statically-typed language with type inference, so we don’t need to explicitly declare types in most cases. The compiler will infer the correct types based on how we use the values.