OCaml from the Very Beginning by John Whitington
Publisher: Coherent Press
Learning a new programming language is always a challenge. It's not so much about getting accustomed to a new syntax or memorizing a bunch of reserved words. Rather, the challenge lies in learning to think different. It's about how to think in the new language, how to express thoughts in it and ultimately solve problems in an idiomatic way. That challenge grows if the language is based on an unfamiliar paradigm. Today, functional programming presents such a challenge to a whole generation of object-oriented programmers. With its no-prerequisites approach, OCaml from the Very Beginning offers a first step onto the functional road that will be useful no matter what technology you ultimately decide to invest in.
OCaml is a high-level language that serves well to introduce functional programming. The language has been on my radar for some years now. The prime characteristic of OCaml is that it is a statically typed language with powerful type inference. That means we get expressive and succinct programs, yet we enjoy the benefits of statical typing such as run-time speed and compile-time checks. In addition, type inference serves as a useful guide during the development, particularly when experimenting at the interactive prompt. Those features help to smooth the learning curve since we can now focus on what we need to express rather than drowning in mechanism. That seems to be the intent of John Whitington's book too.
OCaml from the Very Beginning takes a low-ceremony approach. Already on page 27 John introduces tail-recursion (an optimization technique that allows recursive functions of arbitrary depth without consuming stack space). Tail-recursion is introduced by example rather than by any lengthy theoretical discussion. Interestingly, the concept isn't even named until later allowing the reader to get some context and experience first. Other functional programming fundamentals like higher-order functions and partial function application get a similar treatment. It's an approach that works well and allows the book to keep a high pace covering a lot of ground.
The book teaches a functional style of programming. John waits until chapter 12 to introduce side-effects in his programs and then in the following chapter, when the reader has spent the bulk of the book thinking functionally, mutability is introduced. This means that the object system in OCaml is virtually ignored, which sure was a bit surprising to me given the capital O in the language name. The focus is on basic data structures like lists and trees and fundamental algorithms operating on those structures to insert, delete, search and sort. We also get an introduction to user-defined types, exceptions and OCaml's module system. All code build heavily on OCaml's elegant pattern matching features. The chapters themselves flow nicely and build upon each other, frequently referring back to earlier lessons.
I'd like to see this book as a solid introduction preparing the reader for more advanced texts on OCaml or its younger relative F#. Or perhaps the ideal audience is programmers looking to get into a functional mindset? OCaml from the Very Beginning is a book I'd definitively recommend to anyone interested in learning functional programming. Note that the motivation to learn is a crucial point since there isn't really anything in this book that will convince you to make a paradigm shift. Similarly, you won't learn how to solve larger real-world problems or get accustomed to the OCaml tool chain. But if the motivation is there, this book is an excellent starting point and a highly recommended read.
Reviewed July 2013