OCaml/Introduction

From Wikibooks, open books for an open world
Jump to navigation Jump to search

What is OCaml ?[edit | edit source]

OCaml is a programming language, that is to say a way to give instructions to a computer in order to get back results or effects.

As Java, C# or Python, OCaml is a high level language, conceived for writing evolved applications or libraries, without caring about low level issues, like memory management, and made to encourage code and component reuse. Like those languages, OCaml has many specialized libraries for manipulating user interfaces, 3D graphics, web services, sound and music, mathematical objects and more.

As opposed to Java, C# or Python, all being imperative languages, OCaml is in the functional languages category. To build an imperative program, the coder creates sequences of statements, each modifying the data in memory or performing input/output. In contrast, we build functional programs out of functions in a more mathematical sense, similar to evaluating a long formula. OCaml also has imperative features (as well as object-oriented ones), so we sometimes classify it as multi-paradigm.

Creators of Java or C# claim that their languages are statically and strongly typed, but this is only partially true. In contrast with this, OCaml is truly statically and strongly typed, which means that the language does several automatic verifications and rejects programs it considers erroneous lacking sufficient rigor. Moreover, OCaml's typing is inferred, meaning that most of those verifications are made transparently, without giving additional information to OCaml, as opposed to Java or C# where we have to give the type of each variable, argument, method. On the other end of the spectrum, dynamically typed languages like Python don't have control over the sanity of runtime operations, but OCaml analyzes that when compiling, requiring us to be more rigourous when writing programs, but increasing program reliability and simplifying tests. Moreover, since type verifications are done at compile-time and not runtime, programs aren't slowed down by those coherence tests.

As opposed to Java, C# or Python, OCaml aims at being a declarative language, that's to say we rather describe a solution to a problem than building it step by step. To do it, we benefit from the extensibility of the OCaml language. Indeed, when an application needs some repetitive and complex operations, we can create a new primitive to do this operation by extending OCaml's syntax or by creating a sublanguage of OCaml itself for solving your problem. This way, some libraries, instead of bringing in new features for the language, modify the language itself.

As opposed to Java, C# or Python, OCaml tries as much as possible to be a high performance language. OCaml programs start much faster, usually run faster and will often need 4 times less memory than a Java or C# program. OCaml programs generally use a little more RAM than equivalent Python programs but will be executed about 10 times faster. In some cases, thankfully quite rarely, these performance gains occur to the detriment of comfort of coding.

And finally one thing, unlike Java or C#, and just like Python, OCaml remains an experimental language, which means it regularly gains totally new functionality. Versions of OCaml specialized in distributed programming (JoCaml, Acute), manipulating XML trees (OCamlDuce), writing compilers (MetaOCaml), writing shell scripts (Cash), and more also exist.

With some experience in OCaml programming, you'll figure out that OCaml programs are much shorter than similar programs in Java or C#, thanks to powerful abstraction primitives, OCaml's extensibility, type inference and a shorter syntax. This shortness is often an advantage, even if code is then more concentrated, so sometimes harder to read.

Why learn OCaml ?[edit | edit source]

This question has many responses.

For mathematicians, physicists, statisticians or other users of scientific programs, OCaml allows the development of quick, reliable programs, partially because the source code of the program will resemble the mathematical description of the problem, so it'll be easier to verify. Secondly, the functional decomposition permitted by OCaml avoids many errors. Lastly, modern functional programming simplifies the creation of generic and easily reusable libraries.

Programmers who know Java, C#, C, C++ or other languages will discover through OCaml other ways to see the world and to write their programs to depend less on the execution mode of the machine and more on the intended results, to change the programming language to adapt to the problem au fur et à mesure de la progression du projet, or (again) other techniques of decomposing and reusing code. Even if you can't use OCaml in your career, its concepts will allow you to analyze many problems through a perspective you didn't have before.

Python, Ruby and JavaScript programmers will discover a comfortable language in which there will be found many already known ideas, but in a more rigorous form and more adapted to the development of big, critical projects. On the other end, users of industrial languages -- where the essential concepts go back to the 1970s -- will find newer ideas that have come out of the research world. Many of the concepts considered innovative in industry are pulled directly from functional languages: exceptions, array bounds checking, garbage collection, type inference, generic types, specialized mini-languages, sour? enumerations, closures...