Why Haskell?
My Intro into Functional Programming
It's been a long time since I’ve written a blog! I had a wonderful vacation but I just know everyone is dying to know what my next project is. It's Haskell! This week I’ve been learning the basics of functional programming and Haskell. But Sara, why are you moving away from Object-Oriented Programming? Why Haskell?
That’s a good question. The chart below ranks programming languages based on the PYPL PopularitY of Programming Language Index. This is determined by analyzing how often tutorials for a specific language are searched on Google. Where is Haskell? Oh, it's near the bottom at number 27 and showing a decrease in popularity over the last year.
So, again, why Haskell? Functional programming has been gaining in popularity over the last couple of years, with languages like Java and Python even adopting some of the concepts from functional programming. Secondly, and more important to me, it's adding some new tools. If I wanted to learn another OOP language, it would mainly consist of learning new syntax. However, by deciding to learn a new programming paradigm, I will diversify my skills much more and learn a totally new approach to solving problems. I understand this will be more of a challenge than if I wanted to learn Java next (especially given my C# experience) but hopefully, the effort will pay off.
Functional Programming
First, I had to get familiar with the concept of functional programming. I know what a function is; we all do, and we use them all the time, even in OOP. But, here's the difference. In OOP we try to build objects that represent, as closely as we can, the real world. You have a set number of functions you can use to add, modify and have objects interact with each other. In functional programming, instead of building new objects, we build new functions.
Functional programming is programming using pure functions. It doesn't use shared state or mutable variables. This leads to no side effects since for the same inputs we would expect the same outputs. Pure functions do not modify variables, but they can return new ones. Some more characteristics of FP are:
- focuses on results, not the process by using expressions and declarations rather than execution of statements
- uses conditional expressions and recursion (no loops)
- value output doesn’t depend on a local or global state, only the arguments passed to the function
Why Use Functional Programming?
The use of functional programming is growing. It's great for big databases, parallel programming, and machine learning. Functional programming leads to stable infrastructures that are reliable, fault-tolerant, and less prone to errors. The following are some of the reasons companies are starting to shift towards FP:
- use of immutability and lack of side effects leads to fewer errors, code safety, and higher reliability
- pure functions are easier to test and debug, especially unit testing
- allows parallel processing and concurrency which is good for data analysis and large sets of data
- code is usually shorter and clear, therefore easy to maintain and offers better modularity
What is Haskell?
Haskell is a purely functional programming language. It is general-purpose and statically typed. Programs in Haskell are always written as mathematical functions which have no side effects. It is mainly used in research and academia. However, it is also used in a number of companies. It has applications in a range of fields like aerospace, defense, web startups, social media, and hardware design. Recently, it has become popular in finance tech companies (due to its security).
See some example uses in the industry here.
Why Use Haskell?
Along with the benefits of functional programming, Haskell has its own benefits that promote security and stability and allows building fault-tolerant programs. Haskell’s advantages include:
- it, by default, uses lazy evaluation, which means things only get evaluated if they are needed. This can result in faster programs.
- it includes automatic memory management to make it memory safe and avoid memory leaks and overflows (a common problem in languages like C or C++).
- it is statically typed, which adds an extra layer of security to the code. It may take more time during development but makes it easier to debug.
- it facilitates clean, more reliable code which leads to shorter development time, scalability, and eases refactoring.
Read more:
- What are the advantages of Haskell over other functional programming languages?
- How is Haskell different from other functional languages?
Why Not More Haskell?
I spent a lot of time talking up Haskell and Functional Programming. Well, if it's so great, why was it so low on the list of popularity? There are a few reasons for this. Firstly, Haskell is still primarily used in academia and research. There are companies that use it (see list above) however, even they struggle to find good candidates that have experience. It can be rather complex and so not a lot of new programmers will try to learn it. Lastly, it's a matter of fit. Most companies still have a majority of their code in “traditional” languages like C++, Java, Python, or Ruby. Integrating the existing code with Haskell would be a huge and almost impossible task.
Conclusion
I’m excited to continue on this journey in Haskell. The main benefit will be the opportunity to expand my mind. Haskell and functional programming will allow me the chance to attack problems in a way I couldn’t before. It’s not about a specific new language, it's a new mindset.
References
- Why Fintech Companies Use Haskell
- What is Haskell, and Who Should Use It? | 47 Degrees
- Quora: Why is Haskell not used in the software industry even though it is a popular functional programming
- What is Haskell? A portrait of the Haskell functional programming language
- Stack Overflow: What is Haskell used for in the real world?
- Why developers are falling in love with functional programming
- What is Functional Programming? Tutorial with Example
- Haskell Tutorial: get started with functional programming