What Is Functional Programming?
Functional programming is a programming approach that treats computer operations as calculations of functions. The most important foundation of a functional programming language is the lambda calculus, and the functions of the lambda calculus can accept functions as input (parameters) and outputs (return values). [1]
- Although the calculus was not designed to be executed on a computer, it can be considered the first functional programming language. In the late 1980s, Haskell published an attempt to gather many ideas from functional programming research. [2]
- Simply put, "functional programming" is a "programming paradigm", which is the methodology of how to write programs.
- It belongs to a kind of "structured programming". The main idea is to write the operation process as a series of nested function calls as much as possible. [2]
- The oldest example of functional programming is the LISP created in 1958. Through LISP, you can use a streamlined manpower. More modern examples include Haskell, Clean, Erlang, and Miranda. [3]
- Closures and higher-order functions
- Functional programming supports functions as first-class objects, sometimes called closures or
- 1. Simple code and fast development
- Functional programming uses functions extensively, reducing code duplication, so programs are short and development speed is fast.
- Paul Graham wrote in "The Hacker and the Painter": In the case of programs with the same functions, in extreme cases, the length of the Lisp code may be one-twentieth of the C code.
- If programmers write the same number of lines of code each day, this means, "C takes one year to develop a feature, and Lisp takes less than three weeks. Conversely, if a new feature, It takes three months to complete the development of the Lisp language and five years to write the C language. "Of course, this comparison deliberately exaggerates the differences, but" in a highly competitive market, even if the development speed differs only two or three times, it is enough to make you Always behind. "
- 2. Close to natural language and easy to understand
- Functional programming has a high degree of freedom to write code that is close to natural language.
- I wrote the expression (1 + 2) * 3-4 as a functional language:
- subtract (multiply (add (1,2), 3), 4)
- It is not difficult to get another wording by deforming it:
- add (1,2) .multiply (3) .subtract (4)
- This is basically the expression of natural language. Look at the following code, you should understand what it means at a glance:
- merge ([1,2], [3,4]). sort (). search ("2")
- Therefore, functional programming code is easier to understand.
- 3. Easier code management
- Functional programming does not depend on and does not change the state of the outside world. As long as the input parameters are given, the results returned must be the same. Therefore, each function can be viewed as an independent unit, which is very useful for unit testing and debugging, and modular combination.
- 4. Easy to "concurrent programming"
- Functional programming does not need to consider "deadlock", because it does not modify variables, so there is no problem with "locking" threads. There is no need to worry about the data of one thread being modified by another thread, so you can safely spread the work among multiple threads and deploy "concurrency".
- Look at the following code:
- var s1 = Op1 ();
- var s2 = Op2 ();
- var s3 = concat (s1, s2);
- Since s1 and s2 do not interfere with each other and do not modify variables, it does not matter who executes first, so you can safely add threads and assign them to two threads to complete. Other types of languages cannot do this, because s1 may modify the system state, and s2 may use these states, so you must ensure that s2 runs after s1, and naturally cannot be deployed to other threads.
- Multi-core CPUs are the trend of the future, so this feature of functional programming is very important.
- 5. Hot code upgrade
- Functional programming has no side effects. As long as the interface is not changed, the internal implementation is externally independent. Therefore, the code can be directly upgraded in the running state, without restarting or stopping.
- Functional programming is often considered to be a serious drain on CPU and memory resources. There are two main reasons:
- Early functional programming languages were not implemented with efficiency in mind.
- Some non-functional programming languages do not provide features such as automatic boundary checking or automatic garbage collection for speed.
- Lazy evaluation also adds extra management work to languages such as Haskell.