Haskell programming language

posted Dec 6, 2010 11:57 AM by Amin Ariana   [ updated Dec 6, 2010 2:02 PM ]

Why Haskell?

A letter from my friend Sean E. Johnson on the virtues of Haskell:

"Hey Amin,

I mentioned Haskell while I was over at your place, but I did a poor job extolling its virtues.

Basically it is a purely functional, lazy programming language. This means that the order that you write things in doesn't matter. A few practical consequences of this are that there are no "if" statements or "while" or "for" loops. These loops are instead replaced by map, filter, fold, and recursion. Also, the laziness of the language means that handling infinite lists is quite easy. For example, if you want a function that returns all the prime numbers, it's return type should be an infinite list of integers. This is ugly to do in most languages. In C# it would require the "yield" statement and iterators. Or the more traditional way would be to pass in a max value, and stop computation once you reach it. This is of course flawed because what if you do not know the max number without processing the list of primes.

Here is an simple (but inefficient) way to get the list of all primes in Haskell (primes is an infinite, ascending list of all the prime numbers):

primes :: [Integer]
primes = filter isPrime [2..]

isPrime :: Integer -> Bool
isPrime n = null (filter (\x -> x `mod` n == 0) [2..n-1])

You know everything in the code example above. The "mod" is the modulus operator (sometimes written as %).

Anyway, even though I didn't make a good case of Haskell while I was at your place, if you are interested, you should do the following tutorial: learnyouahaskell.com

If you find it isn't your thing, then 30 minutes wasted, oh well. But I did manage to convince (edited: one of our skeptical former colleagues) that Haskell is the prettiest programming language under the Sun, for what that's worth.

Enjoy,
Sean"