Title here
Summary here
Our first example will demonstrate various value types in OCaml, including strings, integers, floats, and booleans. Here’s the full source code:
(* Strings, which can be concatenated with ^ *)
let () = print_endline ("ocaml" ^ "lang")
(* Integers and floats *)
let () = Printf.printf "1+1 = %d\n" (1 + 1)
let () = Printf.printf "7.0/3.0 = %f\n" (7.0 /. 3.0)
(* Booleans, with boolean operators as you'd expect *)
let () = Printf.printf "%b\n" (true && false)
let () = Printf.printf "%b\n" (true || false)
let () = Printf.printf "%b\n" (not true)To run the program, save the code in a file named values.ml and use the OCaml compiler:
$ ocamlc -o values values.ml
$ ./values
ocamllang
1+1 = 2
7.0/3.0 = 2.333333
false
true
falseLet’s break down the code:
^ operator.+./. for division.&& for AND, || for OR, and not for negation.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.
Comments powered by Disqus