Haskell plays a special role among func­tion­al pro­gram­ming languages. Since the release of the first version in 1990, Haskell has been con­sid­ered a quasi-standard for languages that follow the func­tion­al pro­gram­ming paradigm. It is no co­in­ci­dence that many other func­tion­al languages model them­selves after Haskell. Initially, learning this pro­gram­ming language can quickly lead to frus­tra­tion, mainly because the concepts behind func­tion­al pro­gram­ming take a lot of getting used to for in­ex­pe­ri­enced users. However, once the initial obstacles have been overcome, even beginners can suc­cess­ful­ly dive into the world of Haskell pro­gram­ming.

In the following Haskell tutorial, we will provide you with simple step-by-step in­struc­tions on how to install this func­tion­al pro­gram­ming language and how pro­gram­ming with Haskell works.

How to install and start using Haskell

As is the case with many other pro­gram­ming languages, you have a variety of options for preparing your system to work with Haskell. GHCi is the most common solution for those looking to learn Haskell and to be able to easily program with the language. It is a ready-to-use in­ter­ac­tive de­vel­op­ment en­vi­ron­ment that comes bundled together in a package with the Glasgow Haskell Compiler. In addition to the compiler, you also need the Haskell libraries, which in turn require building software such as Cabal or Stack.

Note

The com­bi­na­tion of the Haskell Compiler and cor­re­spond­ing de­vel­op­ment software is also referred to as the Haskell Platform.

To ensure as smooth a start as possible, install the complete Haskell Platform. Ready-to-use in­stal­la­tion files for Windows, macOS and various Linux dis­tri­b­u­tions are available to download (with in­stal­la­tion in­struc­tions) free of charge. You can find these on the official Haskell project website.

In Linux systems, you can also choose the in­stal­la­tion path via the package manager. If you are an Ubuntu or Debian user, type the following command in the console:

sudo apt-get install haskell-platform

Once the in­stal­la­tion is complete, you should be able to launch the in­ter­ac­tive de­vel­op­ment en­vi­ron­ment at any time by entering the command “ghci” in your system’s command line window. In Windows, this is done via Pow­er­Shell which is also rec­om­mend­ed for in­stal­la­tion.

Note

You should keep in mind that the Haskell Platform versions in the package managers are not always up to date. In some cases, it may be necessary to update the in­stal­la­tion files.

Learning Haskell: un­der­stand­ing the basic data types

Haskell is a purely func­tion­al pro­gram­ming language which makes it much more in­ter­ac­tive and in­tel­li­gent than other pro­gram­ming languages. Data types such as numbers, strings (char­ac­ters), and Boolean values (logical values) are already pre­de­fined in Haskell or are at least in­tel­li­gent­ly broken down using computer memory.

Numbers in Haskell

Haskell is capable of rec­og­niz­ing natural numbers. This means that you do not have to define in the Haskell code when an input is a number. Once the de­vel­op­ment en­vi­ron­ment has been launched, you can simply perform cal­cu­la­tions using the usual operators, such as plus (+) and minus (-) signs. As an example, type in “2+2” and confirm the input by hitting the Enter key:

Prelude> 2+2

Haskell will then display the result of this math­e­mat­i­cal cal­cu­la­tion: 4.

Letters in Haskell

Haskell can also recognize letters which we demon­strate below using an example. First, we enter the option “:t” in the command line. This tells the command line to output what data type it is after con­firm­ing the command. This option needs to be followed by a space. Then, enter any letter in single or double quotation marks and confirm the input by hitting Enter:

Prelude>:t "a"

In this case, the resulting output should be “[Char]” which stands for character.

Character strings in Haskell

Haskell can also recognize character strings without problems. These are better known as simply “strings” in pro­gram­ming speak. Once again, no specific syntax is required. All you need to do is enter the string in double quotation marks. The following is an example of what the code would look like and consists of a string with both letters and numbers:

Prelude>:t "12345abcde"

By using the option “:t”, Haskell will output what the data type is. Just like the example using the single letter, the command line will display “[Char]”.

Learning Haskell: in­tro­duc­tion to the most important operators

As pre­vi­ous­ly mentioned in the section on numbers, you can use the typical math­e­mat­i­cal operators (e.g. addition, sub­trac­tion, division, and mul­ti­pli­ca­tion) in Haskell without trouble. You can also use the sequence or range operator to easily list sequences of values.

Note

The web de­vel­op­ment en­vi­ron­ment Online Haskell Compiler was used to execute and display the following code snippets in this Haskell tutorial.

Addition in Haskell

The addition operator for adding two or more values together is also expressed in Haskell using the tra­di­tion­al plus sign (+). In the following example of code, which we will place in the file main.hs, we will use the ex­pres­sion “let” to define two variables (“var1” and “var2”) which are to be added together. Then, we will use the ex­pres­sion “putStrLn” to tell the command line to present the result in the form of a written message:

main = do 
let var1 = 2 
let var2 = 3 
putStrLn "The two numbers add up to:" 
print(var1 + var2)

In the above-mentioned online tool, the input (left-hand window) and output (right-hand window) will look like this:

Sub­trac­tion in Haskell

If you want to tell Haskell to subtract one number from another, use the tra­di­tion­al sub­trac­tion operator which is expressed using the minus sign (-). The following example of code is basically the same as the previous example used for the section on addition. We just have to replace the operator and slightly modify the output message:

main = do 
let var1 = 2 
let var2 = 3 
putStrLn "The result of subtracting one number from the other is:" 
print(var1 - var2)
Note

Haskell also uses the typical char­ac­ters for its division and mul­ti­pli­ca­tion operators. You can tell Haskell to divide numbers using a slash (/) or to multiply them using an asterisk (*).

Listing values in Haskell

The sequence operator is a special operator in Haskell. It allows you to easily declare a list with a sequence of values. It is expressed by “[..]”. For example, if you want Haskell to display all numbers from 1 to 10, you can use the input “[1..10]” to define this number range. The same applies to letters. For example, you can enter “[a..z]” to tell Haskell to output the full alphabet. The following is a simple example demon­strat­ing how to use the sequence operator:

main :: IO()
main = do
print [1..10]

How to declare and define functions in Haskell

Haskell is a purely func­tion­al pro­gram­ming language. So, it is no big surprise that functions play an important role when pro­gram­ming in Haskell. As is the case with other languages, Haskell has its own way of declaring and defining functions.

Note

Declaring a function tells the compiler that a specific function exists. This also indicates which pa­ra­me­ters are to be expected and what the output should look like. When you define a function, this is the actual inclusion of the function in the code.

The following example of code will show you how functions work in Haskell:

add :: Integer -> Integer -> Integer   --function declaration
add x y =  x + y                       --function definition
main = do 
putStrLn "The sum of the two numbers is:"
print(add 3 5)    --calling a function

In the first line of code, the function is declared – all three values of the function (input and output) are integers. In the second line, the function is defined: the two arguments “x” and “y” are to be added together. Using the pre­vi­ous­ly mentioned “main” method, the code is compiled and the result of the function is output for the two input values “3” and “5”.

The following YouTube tutorial for beginners provides a deeper look into working with functions in Haskell:

AtsPtAf2avQ.jpg To display this video, third-party cookies are required. You can access and change your cookie settings here.

Ad­di­tion­al tips for learning and pro­gram­ming with Haskell

Haskell is a very popular language which is why a wide range of support is available for it including online manuals, tutorials, and support forums. We recommend checking out the YouTube tutorial series “Haskell for Im­per­a­tive Pro­gram­mers” by Philipp Ha­gen­locher which covers the most important com­po­nents of this func­tion­al pro­gram­ming language in over 30 videos on a variety of subjects. The community section on the official Haskell homepage provides a com­pre­hen­sive overview of ad­di­tion­al support resources you can find online. If you prefer print materials, you will certainly get your money’s worth from John Whit­ing­ton’s book “Haskell from the very beginning”.

If you use the in­ter­ac­tive de­vel­op­ment en­vi­ron­ment ghci, you can also view a complete list of the commands available at any time while learning Haskell. All you need to do is type the following command in the command line and confirm the input by hitting the Enter key:

Prelude>:?
Go to Main Menu