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.
Continue reading about Learning me a Haskell
Semantically similar articles hand-picked by GPT-4
- Project euler is a fun way to become a better geek
- Haskell and randomness
- 25 lessons from 25 years of coding
- Why I still haven't learned Haskell
- Amdahl's law in action - 27s to 0.03s by changing a function
Learned something new?
Read more Software Engineering Lessons from Production
I write articles with real insight into the career and skills of a modern software engineer. "Raw and honest from the heart!" as one reader described them. Fueled by lessons learned over 20 years of building production code for side-projects, small businesses, and hyper growth startups. Both successful and not.
Subscribe below 👇
Software Engineering Lessons from Production
Join Swizec's Newsletter and get insightful emails 💌 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. 👌"
Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.
Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.
Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.
Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev
Want to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz React components your whole team can understand with React for Data Visualization
Want to get my best emails on JavaScript, React, Serverless, Fullstack Web, or Indie Hacking? Check out swizec.com/collections
Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog
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 ❤️