Caml

Caml

Infobox programming language
name = Caml

paradigm = multi-paradigm: functional, imperative; object-oriented in OCaml
year = 1985
typing = strong, static
designer = Gérard Huet, Guy Cousineau, Ascánder Suárez, Pierre Weis, Michel Mauny (Heavy Caml), Xavier Leroy (Caml Light, Objective Caml)
developer =
latest_release_version =
latest_release_date =
latest_test_version =
latest_test_date =
dialects =
implementations = OCaml, Caml Light
influenced_by = ML
influenced = F#
operating_system =
license =
website = http://caml.inria.fr

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 all descendants of ML, Caml is statically typed, strictly evaluated, and uses automatic memory management.

The first Caml implementation in Lisp was nicknamed "Heavy CAML" because of its memory and CPU requirements relative to its successor Caml Light which was implemented in C by Xavier Leroy and Damien Doligez.In addition to a complete rewriting, "CAML Special Light" added a powerful (applicative) module system to the core language.

Currently, the main implementation of Caml is Objective Caml, which adds many new features to the language including an object layer.

Examples

In the following, # represents the OCaml prompt.

Hello World

print_endline "Hello world!";;

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: let rec fact n = if n=0 then 1 else n * fact(n-1);;The function can be written equivalently using pattern matching: let rec fact = function
0 -> 1
n -> n * fact(n-1);;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: # fact 12;; - : int = 479001600

Numerical derivative (higher-order functions)

As 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": let d delta f x = (f (x +. delta) -. f (x -. delta)) /. (2. *. delta);;This function requires a small value "delta". A good choice for delta is the cube root of the machine epsilonFact|date=May 2008.

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: # let d = d (sqrt epsilon_float);; val d : (float -> float) -> float -> float = 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: # d (fun x -> x *. x *. x -. x -. 1.) 3.;; - : float = 26.The correct answer is f'(x) = 3x^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: # let haar l = let rec aux l s d = match l, s, d with [s] , [] , d -> s :: d
[] , s, d -> aux s [] d
h1 :: h2 :: t, s, d -> aux t (h1 + h2 :: s) (h1 - h2 :: d)
_ -> invalid_arg "haar" in aux l [] [] ;; val haar : int list -> int list = For example: # haar [1; 2; 3; 4; -4; -3; -2; -1] ;; - : int list = [0; 20; 4; 4; -1; -1; -1; -1] 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 statementFact|date=February 2007.

ee also

*Standard ML
*F#, an OCaml-like language for Microsoft .NET
*Objective Caml
* Categorical abstract machine

External links

* [http://caml.inria.fr/ Caml language family official website]

Books

* [http://pauillac.inria.fr/cousineau-mauny/main.html The Functional Approach to Programming with Caml] by Guy Cousineau and Michel Mauny.


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать курсовую

Look at other dictionaries:

  • Caml — (prononcé camel, signifie Categorical Abstract Machine Language) est un langage de programmation généraliste conçu pour la sécurité et la fiabilité des programmes. Il se prête à des styles de programmation fonctionnelle, impérative et orientée… …   Wikipédia en Français

  • Caml — Семантика: Объектно ориентированное Появился в: 1985 Автор(ы): INRIA Релиз: 3.11.2 (20.01.2010) …   Википедия

  • CAML — may mean:* Caml, a dialect of the ML programming language * Collaborative Application Markup Language, an XML based markup language used with the Microsoft SharePoint collaborative portal applicationSee also: * Camel (disambiguation) …   Wikipedia

  • Caml — Meta Language (ML) beschreibt eine Familie funktionaler Programmiersprachen mit statischer Typisierung, Polymorphie, automatischer Speicherbereinigung und im Allgemeinen strenger Auswertung. ML ist allerdings keine rein funktionale Sprache wie… …   Deutsch Wikipedia

  • Caml — ● /ka mail/ np. m. ►LANG Categorical Abstract Machine Language. métalangage fonctionnel (voir langage fonctionnel) mis au point à l INRIA à partir de 1984, et dont l objectif était de remplacer Pascal (et le modèle impératif) dans l enseignement… …   Dictionnaire d'informatique francophone

  • Caml Light — is a lightweight and portable implementation of the Caml programming language, a dialect of the ML programming language family.It is implemented in C and compiles programs to bytecode.Caml Light is not actively developed, having been replaced by… …   Wikipedia

  • Caml Light — est une implémentation légère du langage de programmation Caml développé par l INRIA. Elle est stable et facilement portable. Cette version de Caml permet une programmation fonctionnelle et impérative. Caml Light ne permet pas la programmation… …   Wikipédia en Français

  • Caml Lights — may refer to:* Camel Lights, a brand of cigarette. See Camel (cigarette). * Caml Light, a functional programming language …   Wikipedia

  • Caml-light — ● np. m. ►LANG Version légère de Caml, essentiellement destinée à l enseignement, mais qui n est plus développée …   Dictionnaire d'informatique francophone

  • CAML — cystic adenomatoid malformation of the lung …   Medical dictionary

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”