๐Ÿ“˜
HPM Education - Haskell
  • Introduction to Haskell
  • Introduction
    • Functions
    • Functional Programming vs Imperative Programming
    • Installing Haskell
    • Haskell Modules
    • Loading Modules into GHCi
    • Expressions
    • Laziness
    • Immutability
  • Types in Haskell
    • Introduction
    • Basic Types
    • Static Type Check
    • Polymorphic and Overloaded Types
    • Data Structure Types
      • Lists
        • List Functions
      • Tuples
    • Function Types
      • Curried Functions
      • Partial Application
  • Defining Functions / Working with Functions
    • The Layout Rule
    • Local Definitions
    • The Infix Operator
    • Conditionals
      • If-then-else Statements
      • MultiWayIf
      • Guarded Equations
      • Case-of Statements
    • Pattern Matching
      • Tuple Patterns
      • List Patterns
    • Lambda functions
    • Function Operators
  • List Comprehensions
    • List Comprehensions
  • Higher-order Functions
    • Introduction
    • The map Function
    • The filter Function
  • Recursion
    • Introduction
    • 4 Steps to Defining Recursive Functions
    • Recursion Practice
    • Folds
      • Fold Right (foldr)
      • Fold Left (foldl)
  • Cutom Types
    • Declaring Types
      • Type Synonyms
      • Data Declarations
      • Newtype declarations
  • Type Classes
    • Introduction
    • Basic Classes
      • Eq โ€“ Equality Types
      • Ord โ€“ ordered types
      • Show โ€“ Showable Types
      • Read โ€“ readable types
      • Num โ€“ Numeric Types
      • Integral โ€“ Integral Types
      • Fractional โ€“ Fractional Types
      • Enum โ€“ Enumeration Types
    • Derived Instances
    • Exercise โ€“ Making a Card Deck Type
  • Interactive Programming
    • Introduction
    • Input / Output Actions
    • Sequencing Actions
    • Exercise - Numbers Guessing Game
  • Functors, Applicatives and Monads
    • Introduction
    • Functors
    • Applicative Functors
    • Monads
      • Maybe Monad
      • List Monad
      • Monad Laws
  • References / Further Reading
Powered by GitBook
On this page

Was this helpful?

  1. Introduction

Functional Programming vs Imperative Programming

Now that we have taken 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 programming style, 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:

int sum = 0;
for (i = 1; i <= n; i++) {
    sum = sum + i;
}

The above program first initialises a variable sum to zero, and then loops (repeats the same action) through all the numbers from 1 to n, updating the stored value of the sum variable on each iteration. In the case of n = 3:

sum = 0;
{ first iteration }
i = 1;
sum = 1;
{ second iteration }
i = 2;
sum = 3;
{ third iteration }
i = 3;
sum = 6;

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 already written by people to solve various problems, and we can use them without having to rewrite existing solutions ourselves.

sum [1..3]
= { applying [..] }
sum [1, 2, 3]
= { applying sum }
1 + 2 + 3
= { applying + }
6

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 โ€“ Haskell has no variable assignments. The equality sign = in Haskell is not the assignment operator as in imperative languages, but instead the equivalent of the mathematical equal sign.

PreviousFunctionsNextInstalling Haskell

Last updated 2 years ago

Was this helpful?