What learning Haskell has taught me
July 21, 2012 Leave a comment
I’ve been learning to program in Haskell using the book Learn You a Haskell For Great Good (free online!). After the first few chapters, I decided to try tackling some of the problems on Project Euler. I’m quite surprised at what I’ve learned about my programming habits.
For the uninitiated: Haskell is a purely-functional programming language, and Project Euler is a site that has a bunch of math problems meant to be solved programatically.
Most importantly, I’ve realized that my gut instinct when something runs too slow is throw more memory at it. “I know! I’ll cache the value in an array and save the calculation!” However, when coding in Haskell, the “purely functional” detail tends to ruin that idea pretty quickly. By using Haskell to solve the problems on Project Euler, I’m forced to actually refine the algorithm itself and research the math behind a subject rather than just throwing memory at it. This research also led me to reading about prime number sieves, RSA encryption, and a bunch of other information I would’ve never thought to even look up.
I’ve also learned that I tend to think in terms of loops. When approaching an algorithm, I break things down into “OK, I need to do this, this and this, and then repeat that until this comes up” However, Haskell doesn’t have for or while loops; you’ll generally do things recursively. Until learning Haskell, I’d mentally filed recursion under “one of those things you cover in class and then never actually need”. Whoops. Turns out recursion is surprisingly powerful.
What it comes down to is this: In just two days, learning Haskell has significantly changed how I approach programming. If you’ve never played with purely-functional programming, I highly recommend trying it out. Plus, learning you a Haskell is all in the name of great good; you can’t afford not to! Click the cover below to go right to the book’s website and follow along for free: