WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Haskell · Basic · question 2 of 100

What is functional programming? How does it differ from imperative programming?

📕 Buy this interview preparation book: 100 Haskell questions & answers — PDF + EPUB for $5

Functional programming is a programming paradigm that centers around the concept of treating computation as the evaluation of mathematical functions and the use of immutable values instead of mutable variables. It is a declarative programming paradigm, in which the focus is on defining _what_ is to be computed, rather than _how_ it is to be computed, like in imperative programming.

In contrast, imperative programming deals with the explicit manipulation of the program’s state, using statements to change the state and mutate variables. This programming style focuses on using algorithms to describe _how_ a computation should be carried out, and gives step-by-step instructions for the computer to execute.

Let’s compare functional programming and imperative programming using several key differences:

1. **Immutable data vs mutable data**

In functional programming, data is usually immutable, meaning that once a value is assigned, it cannot be altered.

-- Haskell, functional programming

-- Define a function addOne that takes an integer `x` and returns `x+1`
addOne :: Int -> Int
addOne x = x + 1

In imperative programming, mutable data is common, and variables can be modified in-place.

# Python, imperative programming

# Define a function add_one that takes an integer `x`, adds 1, and returns the result
def add_one(x):
    x = x + 1
    return x

2. **Higher-order functions**

Functional programming languages like Haskell frequently utilize higher-order functions, which are functions that take other functions as arguments or return functions as their result.

For example, the higher-order function ‘map‘ in Haskell:

-- Haskell, functional programming

-- Define a function square that squares an integer `x`
square :: Int -> Int
square x = x * x

-- Apply the square function to a list of integers, [1..5]
squaredList = map square [1..5]

In imperative programming, higher-order functions are less commonly used, and loops are often preferred for iterating over sequences.

# Python, imperative programming

# Define a function square that squares an integer `x`
def square(x):
    return x * x

# Apply the square function to a list of integers between 1 and 5
squared_list = [square(x) for x in range(1, 6)]

3. **Statelessness and side effects**

Functional programming embraces statelessness and aims to minimize side effects. Functions often rely solely on their input arguments to produce a result, without modifying any global state.

In contrast, imperative programming is not averse to maintaining state and often uses variables to manage state throughout the program. This can lead to a higher cognitive load and unforeseen consequences, as variables can be modified by functions in many different parts of the program.

4. **Recursion vs loop**

In functional programming, recursion is a common technique for repetition, as it relies on function calls and immutable data. For example, calculating the factorial of a number in Haskell:

-- Haskell, functional programming

factorial :: Integer -> Integer
factorial 0 = 1
factorial n = n * factorial (n - 1)

Imperative programming often favors loops (e.g., for, while) for repeated operations, which can be more intuitive for some programmers. Here’s the same factorial computed in Python:

# Python, imperative programming

def factorial(n):
    result = 1
    for i in range(1, n + 1):
        result *= i
    return result

In conclusion, functional programming and imperative programming are two distinct paradigms with different approaches to solving problems. Functional programming emphasizes statelessness, immutability, and using functions that depend only on their input arguments. Imperative programming focuses on mutable state, side effects, and step-by-step instructions for carrying out a computation. Each approach has its merits and can be more suited to particular types of problems or programmer preferences.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Haskell interview — then scores it.
📞 Practice Haskell — free 15 min
📕 Buy this interview preparation book: 100 Haskell questions & answers — PDF + EPUB for $5

All 100 Haskell questions · All topics