When you start learning about functional programming, one of the first things to know is that it's a different way of programming compared to what you might have seen before, like object-oriented or procedural programming.Read prevoius FP Series posts
In functional programming, the focus is on describing what things are, rather than giving step-by-step instructions on how to do something. Let's take the example of describing a house.
In a functional way, we'd say a house has a foundation, four walls, and a roof. In an imperative way, we'd explain how to build a house, like pouring the foundation and building walls.
|Set X equal to Zero.
|Add the first number to x.
|X is the sum of all the numbers in the list,
divided by the length of the list.
|Repeat the step 2 for the rest of number in the list.
|Divide X by the length of the list.
Now, let's apply this to a programming problem. Suppose you want to find the average of a list of numbers. In an imperative approach, you'd outline each step, like setting a variable to zero, adding numbers one by one, and then dividing. This is how most people are used to writing code – telling the computer how to do things.
In functional programming, you simply state that the average is the sum of all numbers divided by how many there are. It's like writing a math formula. This way of thinking about problems is a big part of functional programming, and it's inspired by the precision of mathematical functions.
Functional programming is built on three main ideas:
In the upcoming posts, we'll explore these concepts in detail, showing how they work and why they're useful.