From C++ to Haskell

If you had been in OOPs for as long as you can remember, jumping into functional programming (FP) can be tiring and if you had chosen haskell, at times it would be disappointing. After a while with Haskell, you will never want to go anywhere else and you will feel sick with all other languages!. I spent 5 years with enterprise scale C++ code base then jumped into Haskell. I mentioned ‘enterprise’ OOPs, means, it is either legacy code or a huge codebase with leaky abstractions everywhere.

My learning curve could be summarized as follows:

  1. Unlearn traditional encapsulation, class members etc., from OOPs; Learn to appreciate concise code and small variable scopes in FP
  2. Work with types, function signatures, type classes etc., rather than classes
  3. Pattern matching and equational reasoning.. a humble assignment operator from OOPs has a completely different meaning in Haskell!
  4. Most stuff can be solved by recursing and accumulating.. infact, a recursive code is more easy to write and read when compared to iterative code.
  5. Abstract away iterations and conditional reasoning as much as possible, solve things by function composition and currying
  6. ‘Functions as first class citizens’ helps to find neat way to do things. Importantly, arranging function parameters to support better currying is super fun
  7. Lazy evaluations.. I hit this wall very hard only after I started working with disk IO.
  8. Type errors and ‘could not deduce’ errors. There were times, when I wrote only couple of lines of code, but ended up fixing type errors all day. There are also days when I would expect a particular type error, but instead spend time to fix all the other unexpected type errors. All said, it always feels good to fix compilation errors rather than firing up GDB on a C++ core dump! a SEGV fault in Haskell application is very very rare.
  9. Everything works in first shot! and no core dump! .. almost always this will happen and many a times, you would never think about writing unit tests. Compilation itself is a very important happy path unit test 🙂
  10. My code is getting cleaner as I learn more of Haskell! .. this will keep on happening. No matter how much ever you discover, there are always more ways to abstract things. Since code refactoring is very cheap, you will never think twice to refactor that age old code and make it more concise.
  11. Coming across weird blog posts, papers, talks by Haskell theory gurus.. This one should not scare you. With time, you will know what to look for and what all to skip to get your work done fast. It is fun to solve real world problems by stealing ideas and implementations done by these gurus.

Did I mention, I am not very interested in Math? People always feel, you should be a Math guru to use advanced haskell features. Yes, being a Math guru helps you pick up things quite easily. But you can survive and do better in Haskell even with lesser interest in Math

Now I am dirtying my hands with Erlang; with each passing day I feel terrible to miss the wonderful static type system of Haskell.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s