My First Moments with Haskell

As of this moment, I have worked through section 4.5.2 of the very well written Yet Another Haskell Tutorial. What have I learned so far?

  1. How to use HUGS and GHC to play with and compile Haskell.
  2. The basics of arithmetic, pairs, tuples, and lists.
  3. The basic built in functions (map, foldr, foldl, etc.)
  4. How to create source files
  5. How Haskell treats types

What impressions do I have this far? Functional programming seems to be a pretty good deal in my eyes. The most appealing aspect is its base in real mathematics. All computer science algorithms are obviously based in some sort of mathematics, but with functional programming it seems that the good kind of mathematics becomes more apparent. This would be the type of mathematics that is translated almost directly to code with no worry about writing loops and other structures to manage your data.  A quick example of this would be the Fibonacci Sequence. Mathematically this looks like this:

X1 = 1
X2 = 1 + X1
Xn = Xn-1 + Xn-2

In Haskell it looks like:

fib 1 = 1;
fib 2 = 1 + fib(1);
fib x = fib(x-2) + fib(x-1);

or

1
2
3
4
fib n =
  if n == 1 || n == 2
    then 1
    else fib (n-1) + fib (n-2)

As you can see above, we have simply translated a recursive mathematical formula almost directly into code! That is powerful! Another functional programming feature used is pattern matching. Notice the fib 1, fib 2, and fib n from the first example? Whenever the code encounters a call to the fib function, it matches the parameter against 1,2, or n in order to determine which function to call. Sweet!

Maybe it's just me, but the sheer mathiness of Haskell and functional programming makes me giddy! It makes me imagine a world where code is written correctly because it follows mathematics! Now that is exciting.

  1. I'd recommend a more up to date introduction, like http://book.realworldhaskell.org/read/ which covers using GHC (the high performance compiler for Haskell), and parallel programming.

  2. Thanks. Great suggestion! Real World Haskell is definitely a better source for learning Haskell.

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">