Getting started with Haskell

As I'm always on the lookout for better programming models, and I was reading a lot of intriguing things about functional programming, I decided to use a break between projects to finally take the plunge and learn some Haskell.

As I'm doing this with an eye for its practical applicability rather than just expanding my programming horizons, my first point of interest was the tools for writing Haskell software.

After doing some research, I settled on the following steps to setup an environment for Haskell development.

Installation

There are a couple of options to install the compiler on Mac OS. One is to download the Haskell platform installer. The platform includes the compiler (GHC), Haskell REPL GHCi, package manager Cabal, GHCi debugger as well as some other tools like a documentation generator, a profiler and code coverage tool.

The other option is to use Homebrew, which is my preference for managing software. Homebrew has recently removed the Haskell platform formula due to incompatibilities. Instead, they now recommend installing the compiler (which also includes the REPL) and Cabal as separate formulas:

brew install ghc
brew install cabal-install

Editor/IDE options

There aren't many full-fledged IDEs for Haskell at this point. The Haskell site lists available solutions.

One IDE I tried out is FP Haskell Center. It's an online IDE which integrates with Github for source control, does continuous compilation and comes with Stackage libraries included. It's definitely an interesting product as it's written in Haskell and it shows how much can be done in the browser. It also has a free Community plan. However, as always is the case, I found the limitations of the browser to be inconvenient, and started looking for other options.

JetBrains RubyMine (and presumably their other IDEs such as WebStorm) now has a plugin for Haskell (see announcement: Haskell plugin for IntelliJ) which provides syntax highlighting, code completion, integration with the compiler etc. In my short test, I couldn't get it to do anything beyond syntax highlighting, so I abandoned this path for the present.

Alternatively, SublimeEdit and TextMate can be kitted out with Haskell bundles which provide some useful functionality. I settled on TextMate as it is my everyday editor.

Hasklig font

There is a really nice font developed specifically for Haskell development called Hasklig. It uses ligatures to provide a nicer representation of Haskell specific symbols:

I'm using this font in TextMate as it makes my Haskell code look much prettier.

Package management

Haskell's library and program repository is called Hackage. Cabal can then be used to install stuff from Hackage.

Due to changing versions of the software in Hackage, and the lack of per project dependency installation mechanism, dependency management was somewhat problematic until recently (this is what's referred to as "Cabal hell").

Fortunately, there have been a couple of developments which serve to resolve this problem.

Stackage is a "stable Hackage" (provided by FP Complete). It provides stable, properly compiling snapshots of Hackage for different compilers. The Haskell web site now recommends using this.

The second part of the solution is the addition of "sandboxes" to Cabal (as of version 1.20). They are similar to the functionality RVM provides for Ruby, for example, i.e. they allow you to install dependencies per project, without interfering with each other.

You can read this introduction to Cabal sandboxes.

So far I'm writing toy programs which only depend on Haskell Prelude so I haven't actually used Cabal and sandboxes yet.

Standard library

Prelude is Haskell's standard library, and it's included automatically in every program.

Keep in mind that not all Haskell developers are happy with Prelude (see e.g. discussion at Haskell's Prelude is adding Foldable/Traversable and So many preludes). There are alternatives to Prelude such as basic-prelude and classy-prelude which aim to address the deficiencies in the standard library.

That's it for now!

In the following posts, I plan to document Haskell learning resources, and more of what I learn about programming in Haskell.