Objective Caml version 3.09.3 # let minimum x y = if (x < y) then x else y;; val minimum : 'a -> 'a -> 'a = # minimum 2 5;; - : int = 2 # minimum "a" "z";; - : string = "a" # minimum "a" 34;; Characters 12-14: minimum "a" 34;; ^^ This expression has type int but is here used with type string (* annonymous function example *) # (fun x -> x + 1) 3;; - : int = 4 # let list1 = [32; 123; -123; 234; 234; 54; 999];; val list1 : int list = [32; 123; -123; 234; 234; 54; 999] (* higher-order function example, find minimum of list *) # List.fold_left minimum (List.hd list1) (List.tl list1);; - : int = -123 # let avg = (List.fold_left (fun x y -> x + y) 0 list1) / (List.length list1);; val avg : int = 221 (* defining recursive functions/pattern matching *) # let rec fib x = match x with 0 -> 0 | 1 -> 1 | n -> (fib (x - 1)) + (fib (x - 2));; val fib : int -> int = # fib 5;; - : int = 5 # fib 9;; - : int = 34 (* user defined types example *) # type color = Red | Yellow | Blue;; type color = Red | Yellow | Blue # let x = Red;; val x : color = Red (* recursive types example *) # type nat = Zero | Succ of nat;; type nat = Zero | Succ of nat # let inc_nat x = Succ(x);; val inc_nat : nat -> nat = # let three = Succ (Succ (Succ Zero));; val three : nat = Succ (Succ (Succ Zero)) (* this function should throw exception when decrementing *) # let dec_nat x = match x with | Zero -> Zero | Succ(y) -> y;; val dec_nat : nat -> nat = # dec_nat three;; - : nat = Succ (Succ Zero) # inc_nat (dec_nat three);; - : nat = Succ (Succ (Succ Zero)) # three = inc_nat (dec_nat three);; - : bool = true (* error in pattern matching, non-exhaustive cases *) # let dec_nat x = match x with | Succ(y) -> y;; Characters 19-51: Warning P: this pattern-matching is not exhaustive. Here is an example of a value that is not matched: Zero ..match x with | Succ(y) -> y.. val dec_nat : nat -> nat = (* quicksort function definition *) # let rec qsort l = match l with | [] -> [] | pivot :: rest -> let left, right = List.partition (fun x -> x < pivot) rest in qsort left @ [pivot] @ qsort right;; val qsort : 'a list -> 'a list = # qsort list1;; - : int list = [-123; 32; 54; 123; 234; 234; 999] (* polymorphic recursive types example *) # type 'a binary_tree = Leaf of 'a | Tree of 'a * 'a binary_tree * 'a binary_tree;; type 'a binary_tree = Leaf of 'a | Tree of 'a * 'a binary_tree * 'a binary_tree (* binary tree of ints *) # let my_tree2 = Tree (10, Leaf 3, Leaf 16);; val my_tree2 : int binary_tree = Tree (10, Leaf 3, Leaf 16) (* binary tree of strings *) # let my_tree3 = Tree ("n", Tree("m", Leaf("c"), Leaf("t")), Leaf("z"));; val my_tree3 : string binary_tree = Tree ("n", Tree ("m", Leaf "c", Leaf "t"), Leaf "z") (* binary search for user defined binary tree type *) # let rec binary_search t v = match t with | Leaf(x) -> x = v | Tree(x, l, r) -> if (v = x) then true else if (v < x) then (binary_search l v) else (binary_search r v);; val binary_search : 'a binary_tree -> 'a -> bool = # binary_search my_tree2 3;; - : bool = true # binary_search my_tree2 2;; - : bool = false # binary_search my_tree3 "z";; - : bool = true # binary_search my_tree3 "e";; - : bool = false (* type conversion function for nat type *) # let rec int_of_nat x = match x with | Zero -> 0 | y -> 1 + nat_to_int (dec_nat y);; val int_of_nat : nat -> int = # int_of_nat three;; - : int = 3 # nat_to_int (Succ(Zero)) ;; - : int = 1