Rheinard Korf logo
Rheinard Korf
  • Posts
  • Notes
  • Archived

Get better at Swift by learning Haskell

A few months ago someone was desperately trying to get me to learn Haskell. I really wanted to, but kept putting it off. So yesterday I installed ghc a Haskell compiler and started playing. I should have done this ages ago!

So after that intro, its been a few weeks since my last post. Various things have happened, including me developing the dreaded coder’s pinky (painful wrists). So for a while there I was a bit worried and started teaching my computer how to code with my voice. Its doable, but for now simply replacing IDE shortcuts with voice commands, this is already a huge win… ok, back to Haskell.

As I’ve been learning Swift and going through the language I started seeing a lot of things that did not make sense to me, for one, I learned to code with variables at a young age. Why on earth is all Swift documentation trying to get me to use immutable objects? It did not make sense. Then I started seeing some more code that I didn’t understand… Closures in Swift looked so foreign to me. I’m used to anonymous functions in JavaScript, PHP 5.3+ and lambdas in Ruby, but Closures in Swift really had me totally confused.

Enter Haskell. I started noticing notation in Swift that I’ve seen before in a programming Slack channel. It looked similar to Haskell and I got curious. Turns out that Swift was very much inspired by Haskell and other languages. Thats all I needed to know to finally give Haskell a try.

Paradigm Shift

I started playing with Haskell and my mind was blown. Functional programming is very different to my OOP thinking. Favouring struct over class, everything immutable, list comprehension, infinite lists… this stuff is crazy. But as you play around with it things slowly start to make sense and you can’t help but wanting to learn more.

Back to Swift

After playing only a little bit with Haskell and looking at Swift code again, things are making a whole lot more sense. I realised that I can write functions in Swift without even using func structures, operator overloading, filter, map, reduce, but also writing my own generic functions (not so comfortable with that yet, but getting there).

Functional programming is not the be all end all (IMHO), but it sure helps a whole lot if you’re wanting a deeper and better understanding of what is possible with Swift (and I’m not talking iOS or OSX dev here, I’m talking about the language itself).

If you’re learning Swift or already quite comfortable with it, do yourself a favour, head over to “Learn You a Haskell”. Its a nice guide to help you learn Haskell (despite the comical name).

Happy coding!

© 2020 Rheinard Korf MixerTwitterGitHub