English: The Haskell Logo, a stylized >Ξ»= . Th...

Image via Wikipedia

A couple of days ago I decided that doing my graduation thesis on a topic that, when suggested, brought a sparkle to my mentor’s eye and made him suggest I might want to think about picking a co-mentor just wasn’t hard enough – so I decided to do the whole thing in Haskell.

I want to show you what I’ve learned of Haskell in just a few hours, you can skip the next five-ish paragraphs to get to the juicy code examples πŸ™‚

Now I’ve never done Haskell before, I’ve heard about how awesome it is and that it’s super fast and super awesome and utterly and strictly functional and did I mention I’ve heard it’s awesome? Until a couple of days ago I didn’t even really know what Haskell looked like!

The graduation thesis really seemed like the perfect excuse to get into Haskell – and I’ve been looking for one for a while. Somehow when I started out learning functional programming with Clojure it didn’t really reel me in, Haskell so far looks like it might capture my heart.

Maybe it’s just the cool writing of Miran Lipovaca who published Learn you a HaskellΒ in April this year (and made it available for free on the great wide interwebs), that’s making the learning curve slightly easier to digest than it was when I was doing Clojure. (he was also my classmate in high school and is my sort-of classmate now in college, which makes the whole thing that much awesomer)

The first thing I noticed is just how bloody expressive Haskell is, it feels like I’m writing much less code than I’m used to in both JavaScript and Python to achieve cool stuff. Definitely less than when I was trying to learn Clojure by just perusing the docs.

After 3 hours of learning Haskell, here’s how it compares to my Python and Javascript – I had originally wanted to compare to Clojure, but that was just embarrasing πŸ™‚

Sum multiples of 3 and 5 under 1000

Haskell:

sum [x | x <- [1..999], mod x 5 == 0 || mod x 3 == 0]

Python:

sum([x for x in range(1000) if x%3 == 0 or x%5 == 0])

Javascript:

for (var x=0,s=0; x < 1000; x++, (x%3==0 || x%5==0) ? s+=x : s) {}
s

Sum of even fibonacci terms under 4,000,000

Haskell:

fib :: [Int] -> [Int]
fib terms
  | head terms < max' = fib ((sum (take 2 terms)):terms)
  | otherwise = terms
  where max' = 4000000
 
sum [x | x <- fib [1], mod x 2 == 0]

Python:

def fib(terms):
     return terms if terms[0] >= 4000000 else fib([terms[0]+terms[1]]+terms)
sum(filter(lambda x: x%2 == 0, fib([1,1])))

Javascript:

function fib(l) { return (l[0] &gt;= 4000000) ? l : fib([l[0]+l[1]].concat(l)) }
fib([1,1]).filter(function (a) { return a%2 == 0}).reduce(function (a,b) { return a+b })

I was writing the python and javascript examples directly in the console, hence less new lines. But the point is, even after just very little time, haskell is proving to be pretty much as expressive as my two primary languages where I know all a lot of the tricks.

This looks like the beginning of a long relationship.

Enhanced by Zemanta

Learned something new? Want to improve your skills?

Join over 10,000 engineers just like you already improving their skills!

Here's how it works πŸ‘‡

Leave your email and I'll send you an Interactive Modern JavaScript Cheatsheet πŸ“–right away. After that you'll get thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over my 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

PS: You should also follow me on twitter πŸ‘‰ here.
It's where I go to shoot the shit about programming.