HPM Education - Haskell

Searchβ¦

Introduction

Types in Haskell

Defining Functions / Working with Functions

List Comprehensions

Higher-order Functions

Cutom Types

Interactive Programming

Functors, Applicatives and Monads

Functional Programming vs Imperative Programming

Now that we took a look at functions in Haskell, letβs explore the concept of functional programming. In functional programming, the basic method of computation is the application of functions to arguments. Therefore, functional programming is best described as a style of programming, and functional programming languages support and encourage this style.

On the other hand, in imperative programming the basic method of computation is changing stored values, i.e. functions in imperative programming languages are not purely mathematical functions, but rather a sequence of instructions that the program should follow to get to the result. To better understand this, letβs see how a task of calculating the sum of natural numbers between

`1`

and `n`

would normally be handled by an imperative language, C:1

int sum = 0;

2

for (i = 1; i <= n; i++) {

3

sum = sum + i;

4

}

Copied!

The above program first initialises a variable sum to zero, and then loops (repeats the same action) for all numbers from

`1`

to `n`

, updating the stored value of the `sum`

variable on each iteration. In the case of `n = 3`

:1

sum = 0;

2

{ first iteration }

3

i = 1;

4

sum = 1;

5

{ second iteration }

6

i = 2;

7

sum = 3;

8

{ third iteration }

9

i = 3;

10

sum = 6;

Copied!

Now let's take a look at how that task could be handled by Haskell β we can achieve this with a combination of two functions:

- 1.
`[n .. m]`

β which produces a list of numbers from`n`

to`m`

, e.g.`[1..3] => [1, 2, 3]`

- 2.
`sum`

β which produces the sum of a list

`sum`

is a predefined function in the Haskell standard library, called Prelude, which is imported by default into all Haskell modules. Libraries are collections of functions written by other people to solve various problems that we can use without having to write them ourselves.1

sum [1..3]

2

= { applying [..] }

3

sum [1, 2, 3]

4

= { applying sum }

5

1 + 2 + 3

6

= { applying + }

7

6

Copied!

The above example shows that executing Haskell programs triggers a sequence of function applications β the basic method of computation in functional programming. In functional programming, the code tells the program what to calculate, but not explicitly how to get to the end result following a sequence of steps.

That brings us to another important thing β there are no variable assignments in Haskell. The equality sign

`=`

in Haskell is not the assignment operator as in imperative languages, but the equivalent of the mathematical equal sign.Last modified 1yr ago

Copy link