+5 votes
207 views
Introduction to the Haskell language

in Other languages by (552k points)
reopened | 207 views

1 Answer

+3 votes
Best answer

We are going to see an introduction to the Haskell programming language , it is a functional language . In this type of languages ​​the functions will perform a calculation and will return something, it does not have any side effects. In addition, the variables can only take one value, that is, if we have a variable called n that we assign the value of 10, n will be 10, we can not make it count as 14, or 3..

Haskell is a statically typed language , another Java language example is Java , this means that when we compile a code the compiler will know what type of data belongs to a part of memory (integer, string, character, boolean, etc.), unlike what happens in dynamic languages, such as Python or Ruby .

This language makes lazy evaluations , it will not calculate until a value is completely necessary, it will also prevent that evaluation from being repeated if we need the calculation later. For exponential functions it is something interesting, since it will help to reduce the execution time..

After an introduction to some of the characteristics of the language, we will start with the action, the tutorial will be completely practical, the first thing we will need is to install Haskell in our system, for this tutorial we will use Ubuntu, therefore I will execute the following command in the terminal:

 sudo apt-get install haskell-platform 
If you use another operating system then we leave a button to the official Haskell page, where you will find how to proceed with the installation, whether it is Windows, Mac or another Linux distribution:
DOWNLOAD HASKELL

Let's start with the practice, we will divide the tutorial into examples, starting with the simplest ones.

Example 1

Let's start by launching the Haskell console, for this in a terminal we will write the following command:
 ghci 
As you can see, it loads us:

image

The promp is Prelude, we can change it, in this case we are going to put it in hkl, for this we will execute:

 : set prompt "hskl>" 
We see how it has changed:

image

To finish with the first example, we will see how to do some operations in this console, below is the capture of the examples, we can see a sum, a division (we see that it shows us decimals, in other languages ​​we would have returned the whole part ), and an operation with parentheses, to appreciate its function..

image

Note
If we want to leave the Haskell console, we write:
 : q 
And we give enter.
Example 2

In the following example we will work with Boolean data types, you can see the following image to understand its use:

image

It does not require further explanation, we use and ( && ) and or ( || ), in addition to the denial ( not ). At the moment everything is easy, as we can see.

Example 3

We can compare strings, numbers, as we do in the rest of languages, let's see how it works:

image

The comparison between different types gives us an error, as expected.

Example 4

In this example we will see functions that we have in Haskell, that we will probably use often:

image

We explain the functions seen in the previous capture:

  • succ x : Returns the successor of x, if x is 3, decuelve 4.
  • min xy : Returns the minimum number between x and y, if y is 3 and x is 2, returns x.
  • max xy : Returns the maximum of x and y, in the example of 3 and 2, returns 3.
Example 5

In this example we are going to create some "function":

image

We see that we have created the exp function, what it does is squaring a number, and a function called double, which is going to return twice the number we passed to it, you can also appreciate that you can combine what a function returns with other operations such as addition.

Example 6

We will create our own maximum function, but for 3 numbers, and we will use it, then capture it:

image

Example 7

We are going to see other functions with which we can work in Haskell, they are simple, we are going to apply them to lists.

image

We see how init what it does is return the list without the last element, tail does the opposite, returns the list without the first element. If we see head, it returns the first element of the list and of course last returns the last element. And the last two functions, length gives us the length of the list and reverse returns it back to us. There are more functions, but it is not the objective of the tutorial to see all, just give an approximation so you can start working with Haskell.

Example 8

Let's see the last example that will calculate the factorial of a number, that will try to create a file, compile it and execute it, we will create a file called test.hs and in it we will add the following code:
 fac n = (if n == 0 then 1 else n * fac (n-1)) main = print (fac 3) 
To compile we use the following line:
 ghc -o test test.hs 
And to execute it we put:
 ./test 
Then we leave the output:

image

Well up to here comes the tutorial on the Haskell programming language , if you already have experience programming fast you will get the hang of it, although if you are not used to functional languages ​​at the beginning your programming may be strange.


by (3.5m points)
edited

Related questions

+5 votes
1 answer
asked Jun 4, 2020 in Other languages by backtothefuture (552k points) | 574 views
+4 votes
1 answer
Sponsored articles cost $40 per post. You can contact us via Feedback

Most popular questions within the last 30 days

10,659 questions
10,791 answers
510 comments
3 users