First steps with Haskell
The other day I was bored and itching for something to do. I decided that it was about time to learn a new programming language. After awhile, I figured that I should try tackling functional programming again. Back in 2008 I tried to learn Haskell, but at the time I just didn’t have it in me. I wasn’t sure I wanted to try Haskell again, so I took a look at Scala, Clojure, and Scheme. Scala and Clojure were both interesting, but I wanted to learn functional programming, so I went with Haskell. Haskell is often touted as a “purely functional programming language,” so it seemed perfect.
What I’ve Done So Far
I started out with Try Haskell. It is a nice introduction to some very basic language features, but you’re not going to get very far with their REPL. I don’t believe there is any state between lines, so you cannot declare functions (and use them later). But that is OK, it’s super easy to install the Haskell Platform on your computer.
The main resource I followed for a few days was Learn You a Haskell for Great Good. It’s a pretty decent book, but I couldn’t help feeling that it wasn’t really teaching me the “why” of functional programming, just the “how.”
I needed some practice, so with what I had learned so far, I set out to solve some problems on Project Euler. For example, here is my solution to Problem 1:
-- Sum of all natural positive numbers less than 1000 that are
-- divisible by 3 or 5
-- Ans: 233168
e1 :: Int
e1 = sum [x | x <- [1..999], x `mod` 3 == 0 || x `mod` 5 == 0]
That was easy! I defined a function “e1” that returns an Int. Using list comprehension, I take a list from 1 to 999 and only return numbers divisible by 3 or 5.
I started reading Real World Haskell (which is also available through Safari Books Online). I found it to be a much better resource. I already knew quite a bit, so I was able to fly through several chapters. Even so, those chapters did teach me more. The exercises in this book are also excellent.
I’ve also tried to use a few packages from Hackage. I wanted to plot something, so I first tried using gnuplot. I’m a fan of gnuplot, but the Haskell bindings are a bit on the unusable side (for a newbie, at least). I settled on EasyPlot (which still uses gnuplot under the hood). I was able to plot the results from my radioactive decay function in less than two minutes! After spending hours trying to work with the gnuplot packages, I was very impressed!
Some packages on Hackage have absolutely no documentation. I would stay away from them. Some of them have mediocre documentation. Very few have what I would consider acceptable documentation, especially for beginners. The problem is, as far as I can tell, that most Haskell programmers believe the language to be self documenting. I mean, you can always inspect the type of a function, right? While I admit that is useful to know, it doesn’t help a beginner get the grasp of the language. If anyone who maintains a package on Hackage is reading this, please make sure you have at least one example on how to use your module(s).
The most recent resource I found is Stephen Diehl’s, What I Wish I Knew When Learning Haskell. His section on monads helped me to understand them more than anything else.
What I’ll Do Next
Learning Haskell has been extremely rewarding so far. I love the language, I love it’s type system, and I’m starting to love the community’s abundant package system. I have a few projects in mind to help me continue to learn. I would like to write a ray tracer, since ray tracing is a recursive algorithm. Before I get to that, I will probably make a little REST API for a simple headless CMS I’m planning.
I’m not sure I will ever find a real use for Haskell, but if I do then I’ll be ready. Seriously, though, someone give me an excuse to use Haskell.