Haskell is a functional programming language that has been specially designed to handle symbolic computation and list processing applications
This is a beginner’s tutorial to let them understand the fundamentals of Haskell.
What Is Haskell?
Haskell is a widely used purely functional language.
Functional programs are more concurrent, and they follow parallelism in execution to provide more accurate and better performance. In typical programming languages, we instruct a compiler to do something and how to do it. — https://www.tutorialspoint.com/haskell/haskell_overview.htm
That’s not so in Haskell, where we ask what it is we do. Also, Haskell is a sluggish programming language, so the program doesn’t execute code that it thinks is not necessary.
A Haskell program is nothing more than a series of functions that execute.
Haskell is a strictly typed language. By the term strictly typed language, we mean the Haskell compiler is intelligent enough to figure out the type of the variable declared. Hence we need not explicitly mention the style of the variable used.– https://www.tutorialspoint.com/haskell/haskell_overview.htm
Getting Started
To set up a Haskell environment on your Windows computer, go to their official website https://www.haskell.org/platform/windows.html and download the installer.
To set up a Haskell environment on your MAC system, go to their official website https://www.haskell.org/platform/mac.html and download the Mac installer.
For Linux, the installation process is as follows:
$ sudo apt-get install haskell-platform
Starting Haskell
It’s effortless to start the Haskell programming language, and you enter the following command in the terminal:
$ ghci
GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude>
Now, you can enter your Haskell code, for example:
Example program
It is a simple example to demonstrate the dynamism of Haskell. Take a look at the following code:
Prelude> main = putStrLn "Hello Medium"
Output:
Hello Medium
Basic Operations
Haskell is intelligent enough to decode some number as a number. Therefore, you need not mention its type externally as we usually do in other programming languages.
An example:
Prelude> 4*4
You will receive the output:
Prelude> 16
Characters
Like numbers, Haskell can intelligently identify a character given as an input to it.
To display the type of a variable, you can enter the following line of code:
Prelude> :t "a"
This will display:
"a" :: [Char]
Strings
A string is nothing but a collection of characters. We can create the following string:
Prelude> "Hello Medium"
It will display:
"Hello Medium"
If want to know the type of this string, we can use :t
again:
Prelude> :t "Hello Medium"
The output will be:
"Hello Medium" :: [Char]
As I already said, a string is just a collection of characters, so this will be returned as a Char
data type.
Booleans
The boolean data type is pretty much as straightforward as other data types. In the example below, you see some booleans.
Prelude> True && True
Or
Prelude> True && False
Lists
Like other data types, List
is also a beneficial data type used in Haskell. As per example, [a,b,c] is a list of characters. Hence, by definition, List
is a collection of items of the same data type, separated by a comma.
Let’s create a list:
Prelude> x = [1,2,3,4,5]
Outputs:
[1,2,3,4,5]
Length
Lists also have a couple of methods. Given list x, you can use the following method to get the length:
Prelude> length x
Reverse
Or to reverse a list, you can use:
Prelude> reverse x
Add
To add an element to a list, you use the ++
operator;
Prelude> x ++ y
The code above combines list x with list y.
Delete
To delete one single element from a list, you use drop
:
Prelude>`drop n xs
Tuple
Haskell provides another way to declare multiple values in a single data type. It is known as a tuple. A tuple can be considered as a list. However, there are some technical differences between a tuple and a tist.
A tuple has a fixed amount of elements inside it. Tuples are immutable. A Tuple is created as follows:
Prelude> (8,16,'b')
Outputs:
(8,16,'b')
This tuple contains three elements, two numbers, and a character.
Like lists, tuples contain methods with them to determine things like the first or last element in the tuple.
First element
To retrieve the first element of a tuple, use the following method:
Prelude> fst (16, 8)
This will output:
16
Head and tail
When using the head
method, you also retrieve the first element of a tuple:
Prelude> head (16, 8, 24, 32)
Output:
16
But when using the tail
method, you don’t just take the last or second element of a tuple but all the elements except the first one:
Prelude> tail(16, 8, 24, 32)
Outputs:
[8, 24, 32]
Conditional Statements
Conditional statements are a feature that allows programmers to apply a condition in the code flow. The programmer can execute a set of instructions depending on a predefined condition.
Haskell has the following conditional statements:
- If-else statement
- Nested if-else statement
If-else statement
The syntax for if
expressions is:
if <condition> then <true-value> else <false-value>
Functions
Functions play a significant role in Haskell, as it is a functional programming language. Like other languages, Haskell does have its functional definition and declaration.
Let’s take a small example of an add
function to understand this concept in detail.
add :: Integer -> Integer -> Integer --function declaration
add x y = x + y --function definitionmain = do
putStrLn "The addition of the two numbers is:"
print(add 2 5) --calling a function
Output:
The addition of the two numbers is:
7
Pattern matching
Pattern matching is the process of matching a specific type of expression. It is nothing but a technique to simplify your code.
Take a look at the following code block.
fact :: Int -> Int
fact 0 = 1
fact n = n * fact ( n - 1 ) main = do
putStrLn "The factorial of 4 is:"
print (fact 4)
Here we have used the technique of pattern matching to calculate the factorial of a number.
Guards
The concept of guards is very similar to pattern matching, but we use guards to test some property of an expression. In the following code, we have modified our factorial program by using the concept of guards:
fact :: Integer -> Integer
fact n | n == 0 = 1
| n /= 0 = n * fact (n-1)
main = do
putStrLn "The factorial of 4 is:"
print (fact 4)
Recursion
Recursion is a situation where a function calls itself repeatedly. Haskell does not provide any facility of looping any expression more than once.
Haskell wants you to break your entire functionality into a collection of different functions and use the recursion technique to implement your functionality.
In the following example, we have used both pattern matching and recursion to calculate the factorial of 4.
fact :: Int -> Int
fact 0 = 1
fact n = n * fact ( n - 1 ) main = do
putStrLn "The factorial of 4 is:"
print (fact 4)
Lambda expression
A function without having a definition is called a lambda function. A lambda function is denoted by character.
main = do
putStrLn "The successor of 5 is:"
print ((x -> x + 1) 5)
Modules
If you have worked on Java, then you’ll know how all the classes are bound into a folder called a package. Similarly, Haskell can be considered as a collection of modules.
For example, the list module:
import Data.List
You are now able to use the List
functionalities.
Some other common modules are:
Char
moduleMap
moduleSet
module
Custom modules
Let us create the custom module and define a few functions in it.
module Custom (
showEven,
showBoolean
) where
showEven:: Int-> Bool
showEven x = do
if x 'rem' 2 == 0
then True
else False
showBoolean :: Bool->Int
showBoolean c = do
if c == True
then 1
else 0
To import it into the program we do:
import Custom
main = do
print(showEven 4)
print(showBoolean True)
Some Useful Resources:
Conclusion
After reading this article, I hope you can now write a simple Haskell code and have a good idea of what Haskell is about.
I think after learning Haskell, learning other functional programming languages is easier.