Science Fair Project Encyclopedia
Categories: Functional languages | ML programming language family | Object-oriented programming languages | Programming languages
Ocaml
Objective Caml, OCaml for short, is an advanced programming language that is part of the ML family. It is developed and distributed as open-source by INRIA. Ocaml was created in 1996 as a Caml Light successor. Its authors include Xavier Leroy, Jerome Vouillon and Damien Doligez .
CAML originally stood for Categorical Abstract Machine Language. OCaml has not been based on this abstract machine for a long time.
OCaml shares the functional and imperative features of ML, but contains object-oriented concepts and minor syntax differences.
Features
Performance distinguishes OCaml from other ML languages. The runtime system was designed to be fast, efficient, and frugal in memory usage. Ocaml provides a bytecode compiler, a script interpreter or toplevel evaluation loop, and an optimizing native code compiler. Code generated by the native code compiler is typically comparable to C/C++ in efficiency. See The Computer Language Shootout Benchmarks.
OCaml features include a static type system, type inference, parametric polymorphism, tail recursion, pattern matching, first class lexical closures, functors (parametric modules), exception handling, and incremental generational automatic garbage collection. Its syntax is quite concise. The object system provides for multiple inheritance, object construction directly (by specifying methods for a unique object) or from classes, and structural subtyping (objects are of compatible types if their methods are compatible, regardless of what was inherited from what).
OCaml features are pragmatically balanced between expressivity, new features, ease of interfacing with existing systems and libraries and
efficiency. Ocaml supports familiar functions
such as printf, and a foreign function interface which permits
easy linking with C primitives, including language support for efficient
numerical arrays in formats compatible with both C and
FORTRAN.
The OCaml distribution includes a preprocessor (permitting syntactical extensions), a debugger (capable of stepping backwards when investigating an error), a documentation generator, a profiler, and numerous general purpose libraries. The compiler is available for many platforms, including Unix, Windows, and Macintosh, with native code generation for major architectures (IA32, PowerPC, AMD64, Sparc, IA64, Alpha, HP/PA, MIPS, StrongARM), thus providing good portability.
Uses
Computer science
Natural science
OCaml is also widely used in physics, chemistry, biology and, more recently, bioinformatics:
Scientific computing OCaml examples.
Education
Ocaml is used as an introductory language in many universities, including:
- École Normale Supérieure
- Institut d'Informatique d'Entreprise
- EPITA
- Caltech
- Brown University
- University of Pisa
OCaml is also used to teach Computer Science (mainly algorithms and complexity theories) in the French Classes Préparatoires (Preparation Courses), for students who choose to study Computer Science (and has almost replaced Pascal for that).
Programs written in OCaml
Commonly used:
Fun programs:
- Several ICFP winners
- A 2D maze generator
- Gravity simulator
See also
- Standard ML
- F Sharp, an OCaml-like compiler for Microsoft .NET
- EML, a different kind of object-oriented extension to ML
- O'Haskell an object-oriented extension to Haskell, a different functional language
External links
- Caml language family official website
- OCaml libraries
- OCaml tutorial for C, C++, Java and Perl programmers
- A book on OCaml for scientific programming
- Comparison of the speed of various languages (with favorable results for Ocaml)
- Unison File Synchronizer
- Developing applications with Objective CAML
- LablGL (OpenGL+ interface)
- LablGTK (GTK+ interface)
- FFTW (a widely-used FFT library)
Code examples
Hello World
print_endline "Hello world!";;
Birthday paradox
The following OCaml script calculates the number of people who need to be in a room before the probability of two sharing the same birthday becomes larger than 50% (the so-called birthday paradox).
It also demonstrates OCaml as a scripting language. On a
unix-like machine, save it to a file, chmod it to
executable (chmod 0755 birthday.ml) and run it
from the command line (./birthday.ml).
#!/usr/bin/ocamlrun ocaml
let size = 365. ;;
let rec loop p i =
let p' = (size -. (float (i-1))) *. p /. size in
if p' < 0.5 then
Printf.printf "answer = %d\n" i
else
loop p' (i+1) ;;
loop 1.0 2
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details


