Chapter 14
Haskell Hands-On

by Paul Callaghan

The preceding chapter was a glimpse into the mindset of an experienced Haskell programmer. Now it’s time to do some programming. Let’s try a kata.

We’re going to use Dave Thomas’s word-chain kata,[7] and through it we’ll explore how functional programmers approach a problem, we’ll learn some Haskell syntax and libraries, and we’ll touch on performance and optimization of Haskell code.

The objective of the kata is to find a series of steps from one word to another, where in each step a single letter is changed such that the resulting string is also a valid word. For example, turning “lead” into “gold” with the path ["lead", "load", "goad", "gold"]. For simplicity, I’ll assume four-letter words and lowercase throughout.

Before we begin, remember the golden rule: it’s all about data. So our approach will be all about identifying relevant data structures and transformations, and about decomposing big problems into smaller problems until we can solve them in a straightforward way. Remember the silver rule too: “Trust your compiler.” So we’ll focus on clear code first without worrying too much about efficiency.

So let’s get you set up. I’m going to use the Glasgow Haskell Compiler (GHC)[8] as the main development tool here. GHC is the most advanced implementation of Haskell, covering the standard language (Haskell-98) and many experimental extensions and libraries, including some based on cutting-edge research. As well as compiling Haskell to fast native code, it also provides a REPL-style tool called GHCi.[9] The Haskell Platform[10] provides an easy-to-install package of the main Haskell tools for standard platforms, including GHC and GHCi, Cabal (similar to Ruby’s Gem tool), and other build tools, plus many of the popular extra libraries (cf. Gems) already installed. Such additional libraries can be found on Hackage.[11] Also, there are some IDEs for Haskell, but I just use vi. The full code for this chapter is in a Github Gist.[12] To play with the code, save it to a file with a .hs extension and run ghci on the file (or hugs, if you’re using that), then run various bits of code as you wish.