Skip to content
Swizec Teller - a geek with a hatswizec.com

Learning me a Haskell

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

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] >= 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

Did you enjoy this article?

Published on December 23rd, 2011 in Clojure, Functional programming, Haskell, JavaScript, Languages, Learning, Programming, python, Uncategorized

Learned something new?
Want to become a high value JavaScript expert?

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.

Start with an interactive cheatsheet 📖

Then get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career.

"Man, love your simple writing! Yours is the only email I open from marketers and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"

~ Ashish Kumar

Join over 10,000 engineers just like you already improving their JS careers with my letters, workshops, courses, and talks. ✌️

Have a burning question that you think I can answer? I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

Ready to Stop copy pasting D3 examples and create data visualizations of your own?  Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization

Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev

Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️