Haskell Features

Haskell is a functional programming language. So functions are the building blocks of Haskell code.

💡
A function is a special type of relation that relates values present in one set (called Domain) to values present in another set (called Range).

💡
The above diagram is a depiction of function. The arrows going from Domain to Range denote the function. Keeping this diagram in mind, we can also write,

$$f : X -> Y$$

where, f is the function and X is the set of inputs and Y is the set of outputs.

In imperative programing, we tell the computer what to do, for example we assign a value to variable then we can increment it in a loop, so that the same variable will have a different value. In contrast, in functional programming, we define things for the computer. Consequently, we cannot tell computer that there are two different values of the same variable. Either a variable is 10 or it is not. It cannot be both in the same program.
The advantage of functional programming is that it forces you to think all the possible cases and then define the logic of you functions. It also ensures that the functions don’t have side effects which is another way of saying that the function doesn’t affect other parts of you program.

Another feature of Haskell is that it is lazy. So it will not evaluate a function until it is really forced to do so in order to get the final output of the program. This allows you to have infinite data structures in Haskell such as infinite lists, which is really awesome.
Below is an infinite list of natural numbers.

list = [1, 2, 3 ..]

Haskell is a statically typed language. This means that Haskell compiler knows the type of variables at compile time. This helps avoid errors due to type mismatch at compile time. It is also strongly typed, which makes it easy to debug large codebases makes them more readable and maintainable. The Haskell compiler supports type inference that allows you to write short and concise programs.
We can write polymorphic functions in Haskell.

func : a -> a

The “a” in above code is a type variable. So, the input for the above function can be an integer, string on any other type. There aren’t any constraints on a right now. The output and input will be of same type since both have been denoted by the same type variable.

When you use more than one line to define the function or any equation, you would need to start the next line with a white space. It cannot have the same indent as the first line. If you are writing a where clause using multiple lines then the indentation of the next line should be same or after the first character that is written after the where keyword.

Haskell has automatic currying. So it converts functions with multiple arguments into a sequence of functions that take only one argument. And we can have higher order functions in Haskell.

💡
We need to beware of currying. In a non-curried language if we forget an argument to a function we get an error. But in case of automatic currying, forgetting an argument might create a partial function and the error would come at a different place where we call this function as a type error.

Garbage collection is an important aspect of Haskell. Since the data are immutable, so new references are created to store results subsequent operations. Hence lot of garbage data can be produced by the program.

Haskell supports green threading. It provides tools to communicate safely between threads and do work concurrently.

💡
Green threads (aka user lever threads) are threads that are managed by some runtime library or a VM rather than OS’s kernel. They allow simulation of multi-threading without requiring native OS support for multi-threading.

There are many features of Haskell, few of which I have discussed above. If you have reached till here, please provide feedback on the article. Thankyou.

0
Subscribe to my newsletter

Read articles from Shivendra Singh Chouhan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shivendra Singh Chouhan
Shivendra Singh Chouhan

Consultant Aspiring Developer