What is Haskell?
Haskell is a purely functional programming language. C, C++, Java, Pascal, Python etc. are all examples of imperative programming languages. In imperative programming languages, you give a computer a sequence of commands and the computer executes these commands one after the other. These statements are said to change the state of the program as each one is executed in turn. For example, in imperative programming languages you can set a variable x to 10, do some stuff and then set that variable x to something else if you want. In purely functional programming you cannot do this – variables do not exist! (not ones that change their value, anyway) Instead you tell the computer what stuff is rather than what it should do.
Example:
The factorial function takes a number n and returns
n × (n − 1) × (n − 2) × · · · × 2 × 1
So factorial(5) = 5 × 4 × 3 × 2 × 1 = 120
How would you write a program to compute factorial(n)?
The imperative way:
This program is a sequence of commands that the computer will execute one after the other. The program tells the computer to:
- Declare variables
- Go around a while loop
- Do these instructions each time
The functional way:
This python program still computes factorial(n) but:
- No variables are declared
- Recursion is used instead of explicit looping
Functional programs are more mathematical
- No variables
- No loops
- Uses recursion
If you’re already proficient with imperative or object-oriented languages, then learning to think functionally can be challenging. In a lot of ways, it requires you to become a beginner all over again. You’ll have to change your habits and learn how to solve problems in a functional way.
So Why Learn a Functional Language?
In multi-paradigm languages you can incorporate different approaches the language supports. Instead, functional languages will force you to use a functional approach throughout which make them have better syntactic support for functional techniques than multi-paradigm languages.
Haskell is the language that any that any functional programmer worth his/her salt knows. However, not many people make a living coding in Haskell as adoption in industry is very limited. But is it growing more important, especially as parallel systems are becoming more prominent, e.g. multi-core GPUs. Functional programming is great for parallel systems.
Learning a functional language such as Haskell will also make you a better programmer in general. You don’t necessarily need to use a functional programming language to do functional programming but sometimes the functional style is most appropriate. Learning Haskell will force you to become proficient with a functional style of programming.
Image Source: https://en.wikipedia.org/wiki/Haskell_(programming_language)
