Great resources to learn Haskell

by rmontanaro on September 26, 2010

It’s a funny thing when you’re asked about what is functional programming(FP) at my college. You’re not quite sure what’s this new way of thinking, and you scramble some words around “… based on functions…” as an answer. Dare I say it, but I doubt that 95% of my professors can even come close to a good answer. This is not an issue in other universities, as I’ve been told, but here it is. You can’t grasp anything from “Well, there’s that LISP thing… came out on the late fifties…”, except to get an intrinsic feel that functional programming is inherently old, and not worth learning.

Today I want to talk about a very special flavor of functional programming. You too don’t know what it is? No problem. Take your time and read What is Functional Programming carefully. It’s a recent and well-made compilation of important concepts of FP . Back? Let’s go on.

I’m having classes this semester about Haskell (turned out that one of my teachers is aware of FP). So far, I can’t recommend it enough! I’m very excited, though there’s a steep learning curve. I can’t solve most of programmable and complex problems I face everyday, but eventually I will. And that’s because of the great references I’m getting the knowledge from. I’d like to share and present the three of them as I discuss a little about Haskell, as I believe that you can learn much more when you read different perspectives about the same subject. Differently from other posts around the web, I am recommending these books because I really believe they can form a good body of knowledge. Eventually I’ll post a review about each one here.

Learn You a Haskell for Great Good!: A Guide for Beginners

Learn You a Haskell For Great Good!

Learn You a Haskell For Great Good!

LYAH, for short, is the perfect introduction to Haskell. It was created by Slovenian fellow Miran Lipovača to help him understand the language. The book will be published next year, but here’s the gold: it’s available online!

Sure, there are a lot of tutorials out there. But this one differs from every other since it explains the details and don’t leave you wondering why something is that way.  It merges theory and practice brilliantly.

For example, Haskell is a lazy language. It’s not as bad as it sounds, it just means that the computation will happen at the last possible moment. And lists are the building blocks of a Haskell program. Knowing this, we are allowed to have infinite lists. Suppose the following code:

let ones = 1:ones

This code creates an infinite list of number ones. And it’s a perfectly fine piece of code, because of laziness. Here’s what happens when we call ones:

ones
1:ones
1:1:ones
1:1:1:ones
...

And we get an infinite list. Sure this list is not useful, but if we needed to, how would we use it, if it will never stop computing infinite number ones? We use ones with functions:

ghci> take 10 ones
[1,1,1,1,1,1,1,1,1,1]

It’s almost english: “Take the first ten elements from whatever ones is made of”. This code deals with on-the-fly computed ones, since it’s lazy. We don’t have to wait this list to compute all its elements to take the first 10. Haskell knows that the infinite is unreachable.

Sounds great, hum? This example is very simple and yet so truthful! After a few days learning from LYAH, you’ll become aware of how much ground is covered. And if you’d have to stick to one reference only, this would be my advice.

Real World Haskell

Real World Haskell

Real World Haskell

Not everyone knows that Haskell is capable of everything. Perhaps that misconception comes from the fact that Haskell is not a mainstream language.

RWH tries to prove that you can create robust and fast computer apps. It’ also a book, but as LYAH, you can find it for free here! Gotta love the web. And the online resource is even better, since readers can leave comments next to every paragraph and code snippet. Any questions that might arise are probably already answered.

One thing to note about LYAH is that apart from the superb job presenting the language and its essentials, it does not cover what’s needed for a real world app, like databases, GUI programming and even multi-core programming. Fortunately you can find it on RWH. It’s all there! I/O, Error Handling, Testing…

I haven’t read it all, though, but it’s already invaluable because of the advices you get here and there (for example, you shouldn’t use tabs in your Haskell code, only spaces due to portability. Unix uses a 8 space wide indentation, whereas Windows uses 4. This is a very important thing to remember since Haskell, as Python and F#, is an off-side rule programming language.). The only complaint is that the book proposes to present exercises, but there aren’t that many. This is not a problem, with Project Euler and Ninety-Nine Haskell Problems out there.

The Haskell School of Expression

The Haskell School of Expression

The Haskell School of Expression

You are safe reading the two previous books. The first gives you a great description of the language, with a lot of examples, and the second provides what’s needed for real problem solving, presenting a multitude of essential concepts to the Haskell programmer’s toolbox. The Haskell School of Expression presents a distinct approach.

Every time  I’m learning a new language, I tend to put it against math problems, so I can absorb the syntax quickly. If you are like me, this is the perfect complement to the other two resources, because of all the mathematical insight it supplys.

THSE explains everything using multimedi. For example, already in chapter two the development of a module to handle geometric shapes is started, and in chapter three you learn how to draw these shapes in graphic windows. Pretty neat for a book with 24 chapters, even though it does not teach Haskell from the ground up. THSE is not meant to be the first one you read about Haskell, “but for bright students, I suspect most of the material can be covered”, says the author in the early pages. Other topics covered by this book are animations, functional music composition, and even Proof By Induction, so you can demonstrate and prove that your programs are following a mathematical correctness.

All in all THSE is an enjoyable read. Since it’s not focused on language itself, I’d recommend to follow it with at least one of the previous books.

Exercising Haskell a little bit

Let’s solve a simple problem using this amazing language. From Project Euler, problem 48 states:

The series, 1^1 + 2^2 + 3^3 + … + 10^10 = 10405071317.

Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + … + 1000^1000.

One thing about the problems from Project Euler is that the computational approach to solve them (there are problems that you can find a solution with just pen and paper) shouldn’t take long to run. What about this problem? Will it take a considerably amount of time sum up this series?

Not at all. Let’s suppose for a moment that Haskell takes x-1 multiplications to find the value of a^x (actually, Haskell uses an optimized exponentiation function called Exponentiation by Squaring). There would be 0 multiplications for the number 1, one for the number 2, two for 3, and so on. The last number, 1000^1000, would lead to 999 multiplications. That gives us a total of 499500 multiplications. On modern computers, it’s OK to solve a problem like this by brute-force. It’ll take a glimpse of an eye to run!

This is one way to solve this problem

sum [x^x | x <- [1..1000]] `mod` 10^10

Outstanding! The solution came out in just one line.

We need the base and exponents from 1 to 1000, and the easiest way to get these values is by creating a list. [1..1000] is the equivalent of writing all the natural numbers from 1 to 1000. We’ll use this list as ‘input’ for another list, but this time each element x (we access each one with x <-[1..1000]) will be raised to the x-nth power, and a new list is created: [1, 4, 27, 256.. 1000^1000]. We then use the function sum, that receives a list of numbers an returns their sum, letting us close to the solution. Finally, only the last ten digits are necessary, so we use the function `mod` to get the remainder of the division of the first argument (the sum) by the second (10^10). We could write 10000000000, but I choose to increase readability using 10^10. Note that we don’t have to handle big numbers in a special way like we do on other languages, as Haskell has built-in support for integers of any magnitude.

Not every code snippet is going to be this sharp and succinct, but one of the Haskel strengths is its syntax, which in turn leads to snappy and legible code. Hopefully by now you don’t have any more doubt whether you should learn it or not. With patience and time, you’ll realize that Haskell is a nice addition to the arsenal of any computer programmer. Grab a book and start now!

You can buy the books mentioned here:

Learn You a Haskell For Great Good!
Powell’s – Amazon US | UK | DE | FR

Real World Haskell
Powell’s – Amazon US | UK | DE | FR

The Haskell School of Expression
Powell’s – Amazon US | UK |DE | FR

Bookmark and Share

{ 11 comments… read them below or add one }

annuaire September 26, 2010 at 11:40 AM

I like this. Bookmarked!

Reply

Yitz September 26, 2010 at 12:01 PM

These are great reviews. They give a good feel for what each book is really about. Thanks!

Another introductory Haskell book that has been gaining popularity during the past few years is “Programming in Haskell”, by Graham Hutton. http://www.cs.nott.ac.uk/~gmh/book.html

You may want to add it in to your mix.

Reply

mjunk September 26, 2010 at 7:19 PM

Opa, favoritado, logo terei tempo para me dedicar a haskell(ou não, já que lisp me deixou mimado), thanks.

Reply

tommc September 26, 2010 at 8:55 PM

And then once you’ve read that material, read the excellent typeclassopedia.

http://www.haskell.org/sitewiki/images/8/85/TMR-Issue13.pdf

Reply

Michael Vanier September 27, 2010 at 2:03 AM

I’ve written a series of extremely detailed monad tutorials, deriving monads from first principles, which are located here:

http://mvanier.livejournal.com/3917.html

(and follow the links; 8 articles in all so far). The emphasis in these articles is on getting a deep understanding of the concepts behind monads, how they are defined, and how to use them. Feedback is welcomed.

Reply

Abhishek Ghose September 27, 2010 at 2:53 AM

Since “Every time I’m learning a new language, I tend to put it against math problems, so I can absorb the syntax quickly.”

you might find this of help:

http://www.amazon.com/Haskell-Logic-Maths-Programming-Computing/dp/0954300696

Reply

Abhishek Ghose September 28, 2010 at 2:37 AM
Torque Wrench September 27, 2010 at 10:53 AM

What blog platform are you using? I’ve been looking for a new one, and this seems much cleaner then wordpress.

Reply

rmontanaro September 27, 2010 at 4:11 PM

I’m actually using WordPress itself!

Reply

Stephen Blackheath September 27, 2010 at 4:04 PM

I learnt Haskell after years of mainstream languages. Haskell gets my work done faster, and it made me rediscover the joy of programming. Welcome! I hope you have a lot of fun.

Make sure you check out the #haskell IRC channel on freenode. You usually get at least four answers to any question.

Reply

Alex Aidar October 10, 2010 at 1:13 AM

Thank you for publishing this article to my weekly software development compilation.

http://www.softwaredevmanagement.blogspot.com

Reply

Leave a Comment

{ 1 trackback }

Next post: