Friday, June 19, 2009

Haskell: First Impressions

I first heard of Haskell while in college. I learned that it is a functional programming language, like Lisp. And that was the end of the story. Later, when I started working, I kept hearing a lot of praise of Lisp and even played a little with car and cdr and cons (Scheme in particular), but that was pretty much it, and a long time ago (Go here if you want to know how those names came to be). Recently (OK, yesterday) Haskell reappeared on my radar. Maybe because I discovered this online tutorial, and liked the look of it. I mean, cartoons!! So started reading. Haven't finished yet, but I already think it's quite good. Here's a program to compute the pairs of amicable numbers (OEIS: A063990).

answer = [(a,b) | a <- [1,2..], b <- [sum (proper_divisors a)], a < b && a == sum (proper_divisors b)] where proper_divisors n = [x | x <- [1..(n-1)], n `mod` x == 0]

The thing I find impressive is, it reads almost like the definition written on paper. And while I am sure I've coded it in the most inefficient way possible, it took only about 10 minutes and 168 characters to write. Think about it, by trimming a few unnecessary whitespaces, I can send it over SMS. Bwuhahah :p.*

Cutting the long story short, I am enjoying Haskell. I don't think I get the functional thingy yet, and I am not sure if I'll ever use it professionally. And while we are at it, I am also not sure how deep I will go. But one thing for sure, I am having fun, and that is pretty important.

Enjoy!!

[For the interested, there are quite a few good online resources for learning Haskell. In addition to the tutorial I mentioned above, there is The Haskell Tutorial, Haskell wikibook, and a series of posts by MarkCC whose writing I generally admire. Please let me know if you know anything else which is good].

Update:
Just discovered this. O'Reilly's Real world Haskell is available online. I don't know how good it is, but it was the Winner of Best Technical Book, Jolt Awards 2009, so it probably is.


* To whom might be a question ;-)

4 comments:

Onkar said...

yeah! I had a real good taste of haskell and scheme in last semester, and I loved functional programming. The fact that you do can do things "non-destructively" makes it good and challenging at times. You start loving and admiring functional programming only when you do some really good recursive programming. There is one more great concept called lambda calculus, which gave birth to such functional programming languages, see if you can learn something about it too. I think MarkCC has a really good article on lambda calculus. It's really powerful, you will see. If one really wants to enjoy the fun of recursion, functional programming is the way to go ...
they say ,
to iterate is human; to recurse - divine !

that quite says all about it ...

Mohsin said...

Thanks for the informative comment man.. Yes, functional languges make you appreciate recursion in a better light.. and you are right about challenges too.. And I'll be surely reading the lamda calculus article..
now that u have been there and done that, how about a post about your experiences.. I am surely interested in reading it..

Pappul said...

When I first read "Haskell" ... i read it as "HassKhel" ... i thought it was something to do with u having a weekend filled with Hasi and Khel ... but my mind has a limited scope of thinking ... so plz forgive me ... :P

I agree that Lisp was a very fun language to study ... and Haskell looks like the same ... wud be fun giving it a shot ... !!

Mohsin said...

hehe.. sahi interpretation.. wo bhi important hai..
and yes Haskell is fun.. without the parentheses ;-)..
give it a try..