OCaml
OCaml
Installation
Ubuntu
apt install ocaml
Alma Linux
dnf config-manager --set-enabled powertools
dnf install ocaml
Arch Linux
pacman -S ocaml
Source Code
# Download {version}-tar.gz
tar xzf {version}-tar.gz
cd ocaml-{version}
./configure
make
make install
Examples
Fibonacci Numbers
(* Fibonacci Numbers *)
let rec fib(n) =
if n < 2 then
n
else
fib(n - 2) + fib(n - 1)
;;
let n = int_of_string Sys.argv.(1);;
print_int(fib n);;
(* example
interpreter:
ocaml fib.ml 39
compile (byte code):
ocamlc -o fib fib.ml
compile (native):
ocamlopt -o fib fib.ml
run:
./fib 39
*)
Pattern Matching
let rec fib n = match n with
0 | 1 -> n
| n -> fib(n - 2) + fib(n - 1)
;;
let n = int_of_string Sys.argv.(1);;
print_int(fib n);;