Many mathematical functions, such as factorial, are most naturally represented in a purely functional form. The following recursive, purely functional Caml function implements factorial: let rec fact n = if n=0 then 1 else n * fact;;
The function can be written equivalently using pattern matching: let rec fact = function | 0 -> 1 | n -> n * fact;;
This latter form is the mathematical definition of factorial as a recurrence relation. Note that the compiler inferred the type of this function to be, meaning that this function maps ints onto ints. For example, 12! is: # fact 12;; - : int = 479001600
Since Caml is a functional programming language, it is easy to create and pass around functions in Caml programs. This capability has an enormous number of applications. Calculating the numerical derivative of a function is one such application. The following Caml function computes the numerical derivative of a given function at a given point : let d delta f x = -. f ) /. ;;
This function requires a small value. A good choice for delta is the cube root of the machine epsilon. The type of the function indicates that it maps a onto another function with the type. This allows us to partially apply arguments. This functional style is known as currying. In this case, it is useful to partially apply the first argument to, to obtain a more specialised function:
let d = d ;;
val d : -> float -> float =
Note that the inferred type indicates that the replacement is expecting a function with the type as its first argument. We can compute a numerical approximation to the derivative of at with:
d 3.;;
- : float = 26.
The correct answer is. The function is called a "higher-order function" because it accepts another function as an argument. We can go further and create the derivative of f, by applying while omitting the argument:
let f' = d ;;
val f' : float -> float =
The concepts of curried and higher-order functions are clearly useful in mathematical programs. In fact, these concepts are equally applicable to most other forms of programming and can be used to factor code much more aggressively, resulting in shorter programs and fewer bugs.
The 1D Haar wavelettransform of an integer-power-of-two-length list of numbers can be implemented very succinctly in Caml and is an excellent example of the use of pattern matching over lists, taking pairs of elements off the front and storing their sums and differences on the lists and, respectively:
let haar l =
let rec aux l s d = match l, s, d with , , d -> s :: d | , s, d -> aux s d | h1 :: h2 :: t, s, d -> aux t | _ -> invalid_arg "haar" in aux l ;; val haar : int list -> int list =
For example: # haar ;; - : int list =
Pattern matching allows complicated transformations to be represented clearly and succinctly. Moreover, the OCaml compiler turns pattern matches into very efficient code, at times resulting in programs that are shorter and faster than equivalent code written with a case statement.
History
The first Caml implementation was written in Lisp by Ascánder Suárez in 1987 at the French Institute for Research in Computer Science and Automation. Its successor, Caml Light, was implemented in C by Xavier Leroy and Damien Doligez, and the original was nicknamed "Heavy Caml" because of its higher memory and CPU requirements. Caml Special Light was a further complete rewrite that added a powerful module system to the core language. It was augmented with an object layer to become Objective Caml, eventually renamed OCaml.