Article by: 22.06.2022
Haskell is a highly interesting niche programming language. Its roots go back quite far - to 1990. It has never been a particularly popular language. Despite this, it has its own group of faithful users. It is also constantly improved and supported.
If we look at a handful of statistics the the following situation is evident:
- 28th on the list of most popular programming languages (2021 data);
- About 1% of the repositories on Github are code written in Haskell.
It seems small, but on the other hand there are more niche languages. Besides, such data do not show the full picture of the situation.
Haskell is a functional language. In practice, this means that you can write code using pure functions that have no so-called side effects.
Such code is usually more "expressive". It allows you to focus on the result of an operation rather than on the individual steps that lead to that result. What's interesting is that Haskell was often a founding language in this area. Functional programming, so fashionable nowadays, is therefore not particularly new. The basics of this concept have been known for years.
Haskell is sometimes used as an introductory language to the concept of functional programming. Its very nature forces us to think in a functional way.
Haskell has the ability to direct thinking into completely new directions. In a way, it forces you to discover new ways of solving programming problems.
There are a few features of the language that make it appeal to a wide range of users and numerous uses in production code.
- It handles multithreading very well.
- It has a solid set of good quality libraries.
- Haskell offers a very flexible static typing system.
- It is a relatively fast and efficient language. Provided, of course, that the code is written " smartly".
Simple example 1
A line of code is known to be worth more than a thousand words. So let's look at a simple example of Haskell syntax.
main = putStrLn "Hello, World!"
The result of the above code is trivially easy to predict:
And that's about it as far as first contact with Haskell goes. So far, there's nothing remarkable here.
Simple example 2
It gets much more interesting if we try to write and run a simple function. Let's take a look:
add :: Integer -> (Integer -> Integer) add x y = x + y main = print(add 2 4)
At first glance, this code looks a bit strange, especially if we are used to more traditional languages. So let's try to take a closer look at the individual elements.
This is the definition of a function called add. It will return a value of Integer type and take two parameters of Integer type.
add :: Integer -> (Integer -> Integer)
In the next step we declare what our function will do. We have two parameters, which we named x and y. The function is to return the sum of these parameters (x + y).
add x y = x + y
And here we call our function. We give values 2 and 4 and print the result on the screen using print().
main = print(add 2 4)
Do you see the main element? That's the function that is the main input point of the program. If you're familiar with languages like C++ or Java you're probably familiar with this concept. In Haskell it works the same way, although of course the syntax is slightly different.
And while we're on the subject of syntax, here are some interesting things specific to Haskell.
- The function parameters do not need to be separated by commas. So we can use this notation x y.
- There are no curly brackets. This is a bit similar to Python.
- You may not see it in our code, but Haskell uses indentation inside blocks. This is also a Python-like element.
In general, you will find that Haskell does many things its own way. And it doesn't make things easy if you're just learning the basics of the language. On the other hand, you can get used to everything. It's a matter of practice and experience.
Example 3 - lists
Haskell oferuje możliwość pisania zwięzłego, ładnego i bardzo ekspresyjnego kodu. Spójrz:
list1 = [1,2,3] list2 = [4,5,6] main = print(list1 ++ list2)
In the above example, we created two lists, which we then concatenate using ++ (concatenation operator in Haskell). The result will be a concatenated list [1,2,3,4,5,6]. A similar trick is available in Python, for example. As we wrote earlier, Haskell at its core is a function language. This, in turn, allows for a great deal of "expressiveness" when writing code. It allows you to focus on the result of an operation rather than on its individual steps.
Where Haskell is used
Haskell has always had a reputation as an experimental or "academic" language. It was seen as a curiosity for enthusiasts. A language that may be interesting but doesn't have many practical applications.
Except that's not entirely true. There are many examples of this language being used in manufacturing code.
- Semantic - a system for parsing, analyzing, and comparing source code used by Github.
- An anti-spam system developed and used by Facebook/Meta.
- Various systems supporting the IT infrastructure at Google
In general, Haskell is usually used to write specialized software. One that needs to be efficient, stable and secure. It is often software that processes large amounts of data. It is also worth mentioning that Haskell has wide applications in academic and scientific environments.
Haskell is also a good way to break out of the patterns and master a completely new approach to writing code. Its functional nature makes it force you to rethink your approach. So it is a good choice if you already have some idea about programming and want to expand your horizons.
What you need to get started
Haskell is a compiled language. The most commonly used compiler is GHC. Everything you need to get started can be found here:
And how do you learn the basics of a language? Start with this Codenga course. . You'll find not only the basics of Haskell. but more importantly a whole bunch of assignments and exercises. It's a very practical approach to learning programming.