Home » Archives » October 2009 » Haskell

[Previous entry: "2009 vs 1959 Crash Test"] [Next entry: "Comhaltas Ceoltóirí Éireann"]

10/11/2009: "Haskell"


This weekend was spent learning Haskell, or a least starting to learn it. Haskell is a purely functional programming language that's really starting to surge in popularity.

After looking through several Haskell tutorials on the web, I've found the best to be Learn You a Haskell for Great Good by Miran Lipovača. His tutorial isn't quite finished, but what's there is quite good, complete with cute little hand-drawn pictures.

As an example, here is Quicksort first in Perl:


sub quicksort
{
my @a = @_;
!$a[0] ? () :
(quicksort(grep {$_ < $a[0]} @a[1..$#a]),
$a[0],
quicksort(grep {$_ >= $a[0]} @a[1..$#a]));
}


and in Haskell:


quicksort :: (Ord a) => [a] -> [a]
quicksort [] = []
quicksort (x:xs) =
let smallerSorted = quicksort [a | a <- xs, a <= x]
biggerSorted = quicksort [a | a <- xs, a > x]
in smallerSorted ++ [x] ++ biggerSorted


My biggest beef with Haskell so far is the same one I have with Python: whitespace matters. Fortunately, you can use curly braces and semicolons instead of whitespace for formatting. It just takes some serious time to determine how to use {;} properly since almost all posted examples use whitespace formatting.

Once I have the nuts and bolts of Haskell under my belt the next thing is to try to apply it to actual real-world problems rather than the toy problems seen in tutorials. The shift from procedural to functional languages requires a real shift in the way you think.

I've also started looking through the related book by Mark Dominus, Higher Order Perl, which treats Perl as a functional language and shows how to use recursion, iterators and closures to maximum effect. It's an excellent book and I'm considering paying for the dead tree version. There's a free PDF of his book available, too.



Email: jim@jimandbarb.DELETETHISPART.net
(please remove the DELETETHISPART before sending me mail!)