Rahul Sharma (Editor)


Updated on
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
First appeared  1985
OS  Cross-platform
Paradigm  multi-paradigm: functional, imperative; object-oriented in OCaml
Designed by  Gérard Huet, Guy Cousineau, Ascánder Suárez, Pierre Weis, Michel Mauny (Heavy Caml), Xavier Leroy (Caml Light, OCaml)
Stable release  4.04.0 / November 4, 2016; 3 months ago (2016-11-04)
Typing discipline  strong, static, inferred

Caml (originally an acronym for Categorical abstract machine language) is a dialect of the ML programming language family, developed at INRIA and formerly at ENS.


Like many descendants of ML, Caml is statically typed, strictly evaluated, and uses automatic memory management.

OCaml is currently the main implementation of Caml, and adds many new features to the language including an object layer.


In the following, # represents the OCaml prompt.

Factorial function (recursion and purely functional programming)

Many mathematical functions, such as factorial, are most naturally represented in a purely functional form. The following recursive, purely functional Caml function implements factorial:

The function can be written equivalently using pattern matching:

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 int -> int, meaning that this function maps ints onto ints. For example, 12! is:

Numerical derivative (higher-order functions)

Since OCaml is a functional programming language, it is easy to create and pass around functions in OCaml programs. This capability has an enormous number of applications. Calculating the numerical derivative of a function is one such application. The following Caml function d computes the numerical derivative of a given function f at a given point x:

This function requires a small value delta. A good choice for delta is the cube root of the machine epsilon.

The type of the function d indicates that it maps a float onto another function with the type (float -> float) -> float -> float. 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 delta to d, to obtain a more specialised function:

Note that the inferred type indicates that the replacement d is expecting a function with the type float -> float as its first argument. We can compute a numerical approximation to the derivative of x 3 x 1 at x = 3 with:

The correct answer is f ( x ) = 3 x 2 1 f ( 3 ) = 27 1 = 26 .

The function d is called a "higher-order function" because it accepts another function (f) as an argument.

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.

Discrete wavelet transform (pattern matching)

The 1D Haar wavelet transform 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 (h1 and h2) off the front and storing their sums and differences on the lists s and d, respectively:

For example:

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(Cardelli 1984, p. 210.).


The first Caml implementation was written in Lisp, in 1987 by staff at French Institute for Research in Computer Science and Automation (INRIA)

Its successor, Caml Light, was implemented in C by Xavier Leroy and Damien Doligez - and the original was then retrospectively nicknamed "Heavy CAML" because of its higher memory and CPU requirements.

CAML Special Light was a further complete rewrite that added a powerful (applicative) module system to the core language.

OCaml is currently the main implementation of Caml, and adds many new features to the language including an object layer.


Caml Wikipedia

Similar Topics
Camlann Medieval Village
Goonda Guru