preface

The author has spent time learning Haskell (the so-called pure Functional programming language), and has studied Functional reactive programing (Functional Reactive programing) for some time, which has intuitive feeling in general. However, in the process of learning Kotlin recently, Suddenly, functional programming has aroused new thinking, so I took the time to research what is functional programming, and summarize it in the hope that it can help you understand.

Some interpretations are biased

Functional programming is often mentioned in various fields, but many articles on functional programming are biased, and do not capture the essence. Or the common way to write functional programming, but not exactly what is functional programming. Only by understanding this essential problem can we be smooth sailing in the follow-up study.

(In order to understand functional programming, some of the nouns in this article are directly used in English, because the translation will increase the difficulty of understanding)

What is a Function?

As simple as this seems, it’s not.

The term function comes from mathematics, where a function takes a given value and computes another value, that is, f(x), which is common in school.

In common sense, f1, f2, f3 are usually called functions in the following code:

// The pseudocode function has no input parameter and returns 2
def f1(): return 2
// The function takes an argument x and returns x+1
def f2(int x): return x+1
// This function has no input parameter, no return value, and prints hello world
def f3(): print("hello world")
Copy the code

However, there is a difference between a function and a procedure: a function returns a value through an operation, while a procedure executes a piece of code and returns no value. This is very helpful to understand later, the first is to distinguish between the two.

Returning to the code above, f1, f2, is function and f3 is PROCEDURE.

What is Pure Function

Pure. A simple; Pure; We call functions that satisfy the following two conditions Pure Function:

  1. Function has no side effect(no side effect)
  2. The function satisfies the referential transparency condition.

Side effect

The sum function has no side effect on the external state.

def sum(a,b): return a+b
Copy the code

What does the function that produces side effect look like? In fact, we often write functions like this:

int sum = 0
def plus(a,b){
  sum = a + b
  return sum
}
Copy the code

Besides calculating the sum of parameters, the plus function also changes the value of the external variable sum. Side effect is generated by the plus function.

Common Side effect

  • Change the value of the external variable (plus function in the above example)
  • Like writing data to disk
  • Make a button on the page clickable or unclickable

As mentioned above, the difference between function and procedure is that pure funcion does not produce side effect, while procedure usually produces side effect.

Referential transparency

Referential transparency means that given a function and an input value, you will always receive the same output. That is to say there is no external state used in the function.

Referential transparency means that you can replace anyexpression in the program with the result of evaluating that expression (or vice versa) without changing the meaning of the program.

  • Functions that satisfy Referential Transparency can replace the expression itself with the result calculated by the function without affecting the logic of the program.
  • Given the specified argument, the value returned is the same at all times. Not affected by other external conditions.

They say the same thing, but from different angles

Let’s take an example that satisfies RT

def f(): return 2

print(f() + f())
print(2)
Copy the code

F () in the following code is a function satisfying RT. According to the above explanation, we can replace f() with 2, which is the result of f(), without affecting the logic of the program itself:

def f(): return 2

print(2 + f())
print(2)
Copy the code

Or replace it like this:

def f(): return 2

print(f() + 2)
print(2)
Copy the code

On the other hand, f() returns the same value whenever it is called (no external conditions affect it).

Let’s take an example that doesn’t satisfy RT

int counter = 0

def f(x){
  counter += 1
  return x + counter
}
Copy the code

In this example, the function f(x) does not satisfy the code below RT, and the program logic is wrong when we replace f(1) itself with the calculation result of f(1) :

// The original code execution result is: 3
f(1) + f(1)

// Replace the result 1 of f(1) with the result 2
f(1) + 1

// Again, I get 2
1 + f(1)

//εΎ—εˆ°2
1 + 1
Copy the code

We can’t replace the function itself with the result of its execution,

On the other hand, the next two lines of code execute differently

f(1) + f(1)
2 * f(1)
Copy the code

Although the input parameter is 1, f(1) is called at different times to get different results, so f does not meet the condition RT

Back to the pure function

Now that we understand side effect and Referential Transparency, let’s revisit the definition of pure Function.

  • No side effect
  • Referential transparency

A function that satisfies these two conditions is called pure function

What is functional programming

After understanding the pure function, we come back to what is functional programing, and we will feel suddenly enlightened:

Functional programming is about writing pure functions

Functional programming maximizes pure function to minimize side effects and guarantees the same result whenever the function passes the same argument.

What are the benefits?

When we write programs with functional programming in mind, the program has the nature of a module because the functions implemented are pure functions, and you can combine those pure functions as you like.

Pure function does not produce side effects and does not need to worry about external states.

Other benefits have been mentioned in many articles, and the author will analyze them later. πŸ˜€

conclusion

I hope you can understand the essence of functional programming by explaining the previous core concepts. If there is any deviation in the article, welcome to discuss, πŸ‘