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


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


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


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

    Sum of even fibonacci terms under 4,000,000


    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]


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


    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 an expert?

    Here's how it works 👇

    Leave your email and I'll send you thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

    Join Swizec's Newsletter

    And get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.

    "Man, love your simple writing! Yours is the only newsletter I open 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 14,000 engineers just like you already improving their 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 ❤️

    Created bySwizecwith ❤️