The Haskell program is just a big feature

Haskell tutorial: learning Haskell made easy

Haskell plays a special role among the functional programming languages: Since the definition of the first version in 1990, Haskell has been counted as a Quasi-standard for a language that follows the functional programming paradigm. It is no coincidence that many other functional languages ​​are based on Haskell. Learning the programming language can quickly lead to frustrating experiences at the beginning - especially since the Functional programming concepts take a lot of getting used to for inexperienced users. However, as soon as the first entry hurdles have been overcome, newcomers can also successfully immerse themselves in the world of Haskell programming.

In the following Haskell tutorial, we will provide you with the best possible easy entry Step by step, how to install the functional language and how exactly programming with Haskell works.

Install and start Haskell: This is how it works

As with many other programming languages, there are a number of ways you can prepare your system to work with Haskell. The most common solution for everyone who wants to learn Haskell or conveniently program with the language is GHCi. This is a ready-to-use, interactive development environmentwhich is available bundled with the Glasgow Haskell Compiler. In addition to the compiler, the Haskell libraries are required, for which you have a Building software like Cabal or Stack.

The overall structure of the Haskell compiler and the appropriate building application is also called Haskell platform designated.

To get started as easy as possible, install a complete Haskell platform. Ready to use Installation files for Windows, macOS and various Linux-Distributions for free download (including installation instructions) can be found e.g. B. on the official website of the Haskell project.

In the Linux systems you can also use the installation path via the Package manager choose. For example, if you are an Ubuntu or Debian user, type the following command into the terminal:

After the installation is complete, you should use the interactive development environment at any time with the command "ghci“Via the respective command line of your system start - in Windows, for example, via PowerShell, which is also recommended for installation.

Note that the Haskell platform versions in the package management are not always up to date. So it may be necessary to update the installation files!

Learn Haskell: Understand the Basic Data Types

Haskell is a purely functional language, which makes it much more interactive and intelligent than other programming languages. Data types like numbers, character or logical values (Boolean) are already predefined in Haskell or are at least intelligently broken down by the computer memory.

Numbers in Haskell

For example, Haskell understands natural numbers as natural numbers. Specifically, this means that you do not have to define in the Haskell code that a input around a number acts. Once the development environment has started, you can easily perform calculations with the usual operators such as plus (+) or minus (-). Enter "2 + 2" as an example and confirm the entry with the enter key:

Haskell then presented that Result of this mathematical calculation: 4.

Letters in Haskell

Haskell can also intelligently capture letters, which we can easily demonstrate at this point in the Haskell tutorial using an example: First, we set the ": t" option in the command. This ensures that the command line shows which data type is involved after the command has been confirmed. After a trailing space, add any letter in single or double quotes and send the input with Enter:

In this case, the output result is correctly "[Char]", Which stands for the English word" character "- in German: letter.

Strings in Haskell

Character strings, which are better known in programming languages ​​under the English term "strings", can also be recognized as such by Haskell without any problems. In this case, too, you do not need a specific syntax - it is sufficient if you use the respective Enclose the string in double quotes. An example provides the following input, which consists of numbers and letters:

By setting the option “: t” again at this point, Haskell tells us again in the output what type of data it is. As with a single letter, the command line displays "[Char]".

Learn Haskell: Introducing the most important operators

In the section on numbers, we have already seen that they are the typical mathematical operators - Addition, subtraction, division and multiplication - can be used in Haskell without much trouble. The sequence or range operator also provides an operator for the simple listing of consecutive values.

The online Haskell Compiler web development environment was used to execute and display the results of the following code snippets in this Haskell tutorial.

Addition in Haskell

The addition operator for adding up two or more values ​​is also classically used in Haskell by the Plus sign (+) reproduced. In the following code example that we add to the file main.hs insert, we bind using the expression "let"Two variables (" var1 "and" var2 ") that are to be added. The command line should then display the result in the form of a Text message present in the expression "putStrLn"Is defined:

See in the above online tool Input (left window) and output (right window) looks like this:

Subtraction in Haskell

If you want to instruct Haskell to subtract one numerical value from another, use the classic subtraction operator - expressed by the minus sign (-). In the previous Haskell tutorial example for addition, we simply replace the operator and adjust the output message slightly:

Also for the operators of the division and multiplication Haskell uses the typical characters: With slash or slash (/) you can divide values, with the asterisk (*) you instruct Haskell to multiply values.

Collections of values ​​in Haskell

The "sequence" operator is a special Haskell operator. It enables you to have one in the simplest possible way Declare collection with a sequence of values. It is characterized by "[..]". For example, if you want Haskell to display all numbers from 1 to 10, you can specify the input "[1..10]" for this number spectrum. The same applies to letters - with "[a..z]", for example, you can present the alphabet in the Haskell code. We also have a simple example for the "sequence" operator at this point:

Haskell: This is how you declare and define functions

Haskell is a purely functional language. So it comes as no great surprise that functions play an important role in Haskell programming. Like other languages, Haskell follows his own path to Declare functions and to define.

The declaration a function tells the compiler that a certain function exists. It also indicates which parameters are to be expected and what the output should look like. As definition a function is the actual integration of the function in the code.

To give you an understanding of how Haskell works in this tutorial, let's look again at a specific code example:

The function is declared in the first line of code - all three values ​​of the function (input and output) are whole numbers (integers). The function is defined in line 2: The two arguments "x" and "y" are added. With the already known "Main" method the code is compiled and the result of the function for the two input values ​​"3" and "5" is output.

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

With another click you load the video from YouTube. In this case, YouTube can set cookies over which we have no influence.

Additional tips on Haskell learning and programming

Haskell is a very popular language, which is why the range of help in the form of online manuals, tutorials and support forums is very large. For example, the YouTube tutorial series"Haskell for Imperative Programmers" by Philipp Hagenlocher, which describes the most important components of the functional language in over 30 videos with different topics summarizes. The community area of ​​the official Haskell homepage provides a comprehensive overview of other help available on the web. Friends of printed literature come, among others. by doing book"Haskell from the very beginning" by John Whitington at their expense.

Incidentally, if you use the interactive development environment ghci, you can always get a complete Show an overview of the available commands to let. To do this, simply type the following command into the command line and confirm your entry with the Enter key:

Similar articles

Create a native mobile app yourself - Part 2: Programming or app construction kit?

If you want to create a native mobile app yourself, you can do that with a development environment. However, since knowledge of software development is required for this, this variant is usually not an option for beginners. Inexperienced app developers can, however, fall back on special app makers. These do not require any programming knowledge and sometimes score points with a surprising number of ...

Haskell: A portrait of the functional programming language

Hardly any other language presents the range of functional programming languages ​​as clearly as Haskell. Since the 1990s, the language based on the lambda calculus has been a quasi-standard in terms of the functional programming paradigm. We explain what Haskell is, how it differs from other programming languages, and what projects it is suitable for.

DART tutorial - first steps very practical

The DART tutorial makes it easy for you to take the first steps to learn how to program with DART. If you are already familiar with other programming languages, you will notice that DART strives for simple and clear syntax in order to overcome some of the basic weaknesses of JavaScript. Go through the examples in our DART-Lang tutorial and try your hand at your own ...

Flutter Tutorial: How to Develop Apps Using the Google SDK

In recent years, Google has not only published numerous useful services such as Gmail, Maps or Google Workspace, but has also launched various tools for programming software on the market. One of these tools is the Flutter SDK, which is of particular interest to mobile app developers. In the Flutter tutorial below, we show you how to use a simple app ...