Learning Haskell from a book

Overview

This article relates to the author’s own efforts learning the functional programming language Haskell. Three prominent books were used, and some of the strengths and weaknesses of each are discussed, Hopefully this might be of use for someone attempting the same thing.

Motivation

Functional programming (FP) has been becoming more prominent in recent years, breaking out of its academic roots to find industrial application. Many modern languages such as Python and Ruby now incorporate some functional elements, and even the recent C++ standard has incorporated functional features such as lambda functions. However, these are not ‘pure’ functional languages, and they allow the programmer to fall back on traditional programming methods.
To really understand what FP can offer, and the way of thinking about programming that it provides, it can be beneficial to use a language such as Haskell. Haskell was created from the outset to aid research into functional programming, and its purity forces the programmer to learn a new way of thinking. This then makes it easier to understand and assess the compromises made by other languages.
The lure of FP for the non-academic is likely the promise of greater power and expressiveness: better software in less time. This hope stems from the emphasis in functional programming on the specification of what should be computed, rather than requiring a detailed recipe of how a particular computation should be performed. By letting go of these details, the programmer is able to work at a higher level of abstraction. Whilst this may not be suitable for all tasks, the trend in certain spheres towards composing systems of multiple independent blocks [link] rather than a single monolithic piece of software relaxes the requirement that a system should be written in a single language which can be guaranteed to be capable of all tasks, usually C or C++. With greater freedom to choose the most appropriate tool for the particular sub-problem, higher level languages can be chosen to express more abstract system concepts, leaving C for low-level systems programming tasks and areas where control over the implementation details is actually desired (such as tuning algorithmic code for performance), rather than being simply a laborious necessity.

Difficulties of Haskell

The beginning Haskell programmer, even (perhaps especially) one with well developed programming skills will be faced with two main challenges:

  1. Functional programming requires a different way of thinking about problems, and Haskell, being a “pure” functional language does not provide the programmer with an easy escape to imperative techniques learned in a language such as C.
  2. Its academic and mathematical roots means that the programmer will soon face some theoretical concepts. In contrast, this is rarely the case when learning traditional procedural or object oriented programming.

One of the measures of these texts is how well they introduce and explain these concepts without either overwhelming the reader or alternatively glossing over too much.

Initial book choice

Real World Haskell

The author’s first choice was Real World Haskell. The aim of this book is to show how Haskell can be used to solve a range of real-world problems, while introducing the main concepts of the language. This has an obvious parallel to Peter Siebel’s excellent Practical Common Lisp, which helps the reader to build an understanding and appreciation of the Lisp language through tackling a range of illustrative problems such as parsing binary file formats, socket programming and creating a spam filter. Real World Haskell tackles a similar range of problems including parsing binary files, barcode recognition and the creation of a web client podcast catcher or “podcatcher” application.
Initially, this seemed a good choice. The text is readable and not too dry, and the examples later in the book seemed well chosen. However, for this reader it came unstuck when tackling the exercises. Normally when stuck on exercises relatively early in a book it is usually a simple matter of reviewing the text a few pages back to reach a better understanding of some part which has not been fully grasped. This didn’t seem to work in this case (either I was too thick, the examples were too difficult or the text didn’t adequately explain the concepts). From past experience, it is best not to dwell on this, and simply go and read something different. Real World Haskell was put back on the shelf to be returned to as a reference once the core language had been mastered.

Second book choice

Learn you a Haskell
Next on the list was Learn You a Haskell. This proved to be a much better introductory text, covering a broad range of the language in a concise way. This book was read cover to cover, being relatively short at 360 pages. The book progresses from introductory material on list handling and the Haskell type system, through to more advanced topics such as applicative functors, monoids and monads.
The relatively concise nature of this book is a big advantage, since it never feels overwhelming. The downside is that the more advanced topics in later chapters are perhaps a bit thin on explanation, and could benefit from a more rounded treatment. This is nitpicking, however, since by the time the reader has progressed that far, he or she is likely to be equipped to understand material from other sources on topics of interest.
As an introductory text, to get the reader over the initial learning hump, Learn You a Haskell has much to recommend it.

Third book choice

To fill in some of the bits that Learn You a Haskell glosses over, a copy of Haskell: The Craft of Functional Programming was purchased. It has a more academic slant, being apparently written as a teaching tool by its author for use as a course text. Judging by the number of copies in the Edinburgh branch of Blackwells near to the University, it would appear to have been successful in this aim. Its coverage of Haskell is somewhat narrower than Learn You a Haskell, but tends to provide more context and more detailed explanation. It also provides more of the theoretical basis, offering introductory sections on topics such as proofs by induction using Haskell, which are omitted from Learn You a Haskell. Despite this, the material presented never demands too much of the non-academic reader. The third edition of the book also covers domain specific languages (DSLs), which is a topic of growing interest not covered by Learn You a Haskell.
Haskell: The Craft of Functional Programming
It follows the traditional textbook model with plenty of examples and exercises for the reader, which, combined with the more thorough explanation expands its length beyond Learn You a Haskell despite the narrower scope of its language coverage. The exercises are well pitched, stretching the reader to develop on the work covered in the chapter. It makes use of several examples such as picture manipulation and the development of strategy to play the superficially simple game rock, paper scissors which span several chapters. These are used to illustrate different aspects of the language and how they can be combined. The downside of this approach is obviously that it makes it a little more difficult to dip in and out of chapters of interest, which may frustrate the impatient.
If, like this author, you have no immediate need to use Haskell for a particular project, then these exercises are excellent for providing regular programming practice in small bursts to consolidate key concepts and the functional way of thinking. The progression of the exercises has been well judged to avoid much mindless repetition without getting the reader hopelessly stuck. The solutions to the problems also illustrate the elegance and expressiveness of Haskell itself, and are good fun to do, much like a regular Sudoku puzzle for programmers.

Summary

Real World Haskell gives examples of more sophisticated use of the language, but probably falls short as a text for explaining Haskell to someone with limited prior knowledge. It is probably best suited to being used as a reference or cookbook by someone with reasonable background gained elsewhere.
Both Learn You a Haskell and Haskell: The Craft of Functional Programming are excellent introductory texts. Which to choose depends on preference: Learn You a Haskell provides a concise introduction, covering more ground but trading depth of explanation, whereas Haskell: The Craft of Functional Programming provides richer background material and more examples, at the cost of scope of coverage and expanded length of book.