Friday, 28 March 2008

Nice ray tracer in OCaml

The Bla Repository just posted an interesting ray tracer example, written entirely in OCaml.

Thursday, 27 March 2008

Getting the most out of Static Typing

The OCaml Journal just published an article explaining how OCaml's static type system can be leveraged to improve program reliability and development speed:

"The OCaml language arms programmers with one of the most sophisticated static type systems of any general-purpose programming language. This static type system can be used to remove large classes of common bugs that are otherwise tedious or impossible to track down. The benefits in terms of programmer productivity and program reliability are tremendous. However, leveraging such a type system is an art that requires significant effort to learn. This article describes idiomatic OCaml style and a variety of techniques that can be used to leverage the static type system in order to catch errors earlier and more easily..."

To read this article and more, subscribe to The OCaml Journal today!


Sunday, 9 March 2008

Run-time compilation with LLVM

The OCaml Journal just published an article describing basic use of the LLVM library:

"The Low-Level Virtual Machine (LLVM) project brings high-performance static and JIT compilation to code generators written in the OCaml programming language. This article describes the basic use of LLVM from OCaml including both static and JIT compilation, argument passing, control flow and the compilation of expression trees..."

To read this article and more, subscribe to The OCaml Journal today!


Saturday, 1 March 2008

Book Review: OCaml for Scientists

Fabrice Marchant has kindly written and published the following review (also available in French) of the book OCaml for Scientists:

"I have read OCaml for Scientists, the work of Dr Jon Harrop, which is dedicated to the OCaml programming language. Even though science and technical computing are inherent aspects of this book this is not restrictive with the exception of a few complicated examples in the final chapter.

The OCaml language combines functional and imperative programming. It is very fast and has garbage collection. Type inference makes coding particularly concise, safe and easy. OCaml's embedded module language and object orientation form a powerful software engineering tool.

Without requiring knowledge of ML-type languages, OCaml for Scientists teaches the reader how to use OCaml for scientific computing, including the use of several powerful libraries among the examples.

There are few prerequisites for correctly following the book because the author introduces the classical concepts of programming (such as recursion) before progressively developing.

That being said, the knowledge of an imperative programming language (like C or Pascal) or a functional one (such as Lisp or Scheme) is not absolutely required but would make the book more straightforward.

An important part, at the beginning of the book, is dedicated to a proper understanding of the language itself: here one finds coverage of the functionalities offered by OCaml's many keywords, illustrated by numerous clear examples.

This book does not content itself with being only a tutorial of the language. The author shows the proper usage of the possibilities of OCaml - such as the best programming practices - and often goes further in justifying things by studying the internal representation used by the compiler.

Working side-by-side with practical examples, the clarity of the exposition of this part of the book constitutes what is probably one of the most efficient mediums offered to the beginner, to rapidly acquire an understanding of the basic language necessary to program.

Dr Harrop teaches things in such a manner that reading is always easy, even when covering non-trivial scientific subjects. The syntactic coloration, on large white pages, makes the book pleasant to open.

These two aspects work together to give an impression of clarity and simplicity.

The book, stuffed with examples, has a practical view of the different programming components comprising the OCaml system: The customisable top-level, the many compilers, the dependency visualiser and profiler, are all presented together with their usage.

The book's examples are worth a special mention. With the exception of the contents page, it is difficult to find a page which does not present several concrete examples of programming. Some of them are surprisingly elaborate.

Similarly, on the practical side: The use of certain libraries, such as "command line arguments, big arrays, vector matrix and lexing and parsing, are presented in detail.

Despite the scientific focus, the first few chapters are of general interest.
Other parts such as visualisation (with OpenGL) or lexing and parsing, are perhaps particularly useful for game designers.

In addition to OCaml for Scientists, I own "Programmation fonctionnelle, générique et objet (Une introduction avec le langage OCaml)" by Philippe Narbel. The two works complement each other, OCaml for Scientists being more concrete. To cite only the unique parts of OCaml for Scientists, it presents libraries and studies optimisation in detail.

Certainly, IMHO, a section detailing the writing of functors would have been welcome. However, studying the book gives an understanding of the language such that one can undertake substantial programming projects.

The book, preceded by a glossary, is divided into ten thick chapters:

Introduction, Program Structure, Data Structures, Numérical Analysis, Input and Output, Visualization, Optimization, Libraries, Simple Examples and Complete Examples, Bibliography, Advanced Topics and Troubleshouting.

To conclude, OCaml for Scientists is an excellent book, clear and practical, which can be used as a reference work."