Shortcuts: The steps , Monads

How to Learn Haskell

How This Guide Works

So you want learn Haskell, and somehow or another you've ended up at this page. Good! This isn't really a tutorial though. Maybe you could call it a meta-tutorial. Our goal is to give you a better idea of the big picture when it comes to learning Haskell. So, you should be using this tutorial in conjunction with other resources. On that note, we point to lots of them from this page, so don't worry if you're not sure where to start. We've selected useful resources, tried to make connections, and attempted to offer some sort of linearity. Hopefully you can use this guide to better navegate your way through the learning process. Good luck!

If you have any questions or comments, the authors would love to hear from you! Our emails are on the main page.

Starting Out

Make sure you understand the reasons for learning Haskell

Unlike some modern languages, Haskell is not a language you will pick up in two days and then be able to write your homework 10x faster in. Haskell will make you sweat to write simple programs, but it can also make writing things you thought were really complex quite a bit simpler. Mostly, it's a good learning experience since the patterns and ways of thinking you develop will serve you in any computer science work you do. Find some articles or blog posts about "Why Haskell" and try to get an idea of what the Haskell world is like. Some good places to start are listed on the page Why Functional Programming.

Installing and running Haskell

The first thing you should do is install a Haskell compiler/interpreter on your system. HUGS and GHC are the most common options. We recommend GHC for its power and flexibility. It seems to be the de-facto standard, so using it will give you the same platform as most online tutorial writers. One last option to check out is Try Haskell. It's an online, interactive interpreter with limited functionality that can be useful if you don't want to install anything. (Note: it's currently in alpha, and only offers a subset of the language).

Resources:

Editors & GHCI

To make the most of the code examples you find, you should set up a good editor environment for yourself. We tend to use emacs and vim. In emacs, haskell-mode lets you automatically load ghci and evaluate your whole file just by typing C-c C-l. If you prefer vim, you'll probably want one terminal running vim and a second running ghci. You can use :r to reload a file once it's loaded into ghci.

Raghu has pointed out that haskell-mode is much easier to set up in emacs than xemacs. It should be doable in xemacs, but if you're new to the emacs world, we recommend just using plain emacs instead of xemacs. Don't be confused by the names either, emacs also has a gui mode built on X if that's what you prefer; the fork between xemacs and emacs was more of an ideological one -- you'll have to consult google to find out more!

If you want to use GHCI, you should learn how to use it, check out The GHCI page on the Haskell wiki, and realize that you're always writing code in the I/O monad when using GHCI.

GHCI is kind of weird. I said it. But, it's weird because it's awesome. As you start studying Haskell you'll soon find out all about the distinction between writing code "in a monad" and writing code outside of one. To give you a quick example of what I mean, compare:

a = 5
with
let a = 5

Consider this section to be your warning that GHCI is a little strange. Because everything is in the I/O monad when you use GHCI (this sentence doesn't need to make sense to you yet), expect it to seem strange until you've started reading some of the main tutorials and books we've referenced. We recommend writing your code in text files until you've built up a little experience with I/O and feel comfortable writing code directly in GHCI.

The Basics

First of all, you should probably pretend like you've never programmed before. If you run into a word like return or class don't assume that you know what that means; chances are that you don't. Here's a general idea of where you stand, depending on what languages you already know well: So now that you have a general idea of where you stand, and are starting to worry about how weird Haskell is going to be, let's go through a quick (probably incomplete) list of the main distinctions between programming in Haskell and writing code in more "traditional" imperative programming languages.

Common Patterns in Haskell Code

Before you dive into the details, we think it will serve you to have a basic understanding of some common functional idioms, mostly for dealing with lists of data. This tend to show up frequently in Haskell code, and if you're used to for loops, you might be caught off guard. These are all examples of functional programming's verb-centric style.

First of all, Haskell does have arrays, but they're almost never a good idea. Almost everything is done with lists, which are primitives in the language. Even strings are just lists of characters, so any list function is equally applicable to a string. So how do you iterate over your non-arrays and do things? Usually you'll use recursion. Essentially, you grab the first element from your list, do something, and then re-call your function on the remainder of your list. Most of the time, what you're doing fits into one of the following common patterns:

Principal Resources

Below are some of the best general language resources we've found; using these resources (or ones like them) is how you'll learn Haskell.

Tutorials:
Books:

Video Lectures:

IRC:

The Steps to Learning Haskell

These steps are not strictly linear nor are they a perfect guide to learning the language. Everyone learns languages differently, so you should find some good books or tutorials for your skill level and goals (some good ones are listed above) and use them in parallel with this guide. The steps below are in the order we expect you to master the concepts, not in the order you'll see them.

Syntax

The first thing you have to master is syntax if you want to read Haskell code. We don't suggest trying to learn syntax on its own, but before trying to tackle more advanced features of the language you should have a good understanding of all the strange syntax Haskell has to offer. A good guide is the Tour of the Haskell Syntax.

Commonly Used Functions & Basic Programming Techniques

You'll also probably want to have read chapters 1 and 2 of Learn you a Haskell, and/or chapters 1 - 7 of Real World Haskell and gotten some practice using the Prelude (Haskell's standard library).

Laziness

Laziness isn't that tricky of a concept, but it can lead to some pretty strange (and beautiful) things so make sure you have a good grasp of it. If you read any book or full-language tutorial it's guaranteed to talk about laziness, but for the lazy (hah!) the following resources offer concise introductory explanations of Haskell's laziness (in order of depth):

Two (advanced) places to focus in particular when studying laziness are the following:

Types

The type system can be deceptively simple since it's relatively easy to define your own data types in Haskell and do other routine things. Don't let yourself be fooled though; it's in fact quite deep and thorough understandings of typeclasses, newtype, and currying are essential to using Haskell effectively and to understanding the more advanced facets of the language. If you haven't already, be sure to read chapter 2 of Real World Haskell for an overview of how Haskell handles types. So does Learn you a Haskell. Once you can understand things like:

foo :: Int -> [Char] -> (a,b) -> ((a,b) -> Int -> Char) -> [Char]
you can move on to defining your own types using data, type, and newtype.

Learn you a Haskell has an excellent chapter on custom typeclasses and types. Reading this chapter should give you a much more thorough understanding of types and data in Haskell. Lastly, be sure you understand newtype, especially when used like this since it's a pretty common pattern in monad definitions and uses. A good example of that case is in this section of Real World Haskell.

Monads

The next big thing in your quest to understand Haskell is the Monad. Monad is a typeclass. So make sure you understand those first. If you don't have a good grasp of the core language, understanding monad examples on the internet will be very difficult. There are a ton of (mostly good) monad tutorials on the haskell wiki. Take a look at sections 8.1 and 8.3. Monad tutorials can normally be broken down into three categories:

The approach that you probably want to take is to start by skimming the "why" ones. Then you want to skim some code-based ones to get an idea of "what" a monad is in terms of programming. Before you start writing your own monads though, you should actually skip to our next section and do some I/O stuff as a way to practice using monads. Once you have a fairly good idea of what a monad is and how it might fit into a program, you should try to roll your own monad from scratch. We did that by building a limited Forth interpreter as one of our projects. Once you've then played with the internals of writing a monad, a good next step is to look through All About Monads' Catalog of Built-in Monads and try to play with their source code examples. Monads take a while to become "natural" so you'll mostly just want to read a lot of different tutorials, and try to write as much code as you can.

Before moving on make sure you:

I/O

IO is essential to writing useful programs, and also a terrific first exposure to using monads in your Haskell programs. Good resources are the following, but keep in mind that the best thing to do, as always, is write your own code!

The best thing you can do at this point is to try to find cool examples using IO to do things you care about on the internet or in whatever books you have. Trying (and failing at first) to write effective IO code in Haskell will teach you what you need to know pretty fast.

Advanced Stuff and Further Reading

Having learned everything mentioned so far, be aware that there's still a ton of stuff to learn. Some places to go from here:


Page content written by Gordon Sommers and CJ Carey.