The functional programming corresponds to the programming pattern that is based on the concept of stating the procedure of a program as a mathematical functional model rather than as explicit sequences of instructions to a processor, which is the main concept in imperative programming.
Functional language emphasizes statements and terms rather than executing statements. In this programming, the result will only depend on the parameters that are passed to a function, unlike other types that obey a local or global state.
Its name comes from the mathematical functions, which are the assignment of a set of inputs to a set of outputs. A mathematical function does not really do any work, but describes the model of a process, explaining by means of a formula what a set of inputs produces in a function.
Article index
The foundation on which functional programming was based was the lambda calculus, which was developed during the third decade of the 20th century to define and apply functions. LISP was the first programming language of its kind, designed in 1960.
Although most programming languages consist of inputs, outputs, and external variables that can be set or used from within functions, functional programming avoids this. The idea is that every time a function is called with the same parameters, it should return the same value.
Functional programming languages are called applications, because the functions are applied to their parameters, as well as declarative and non-procedural, since the definitions specify what is to be calculated and not how it is calculated.
A function is pure when it has no observable side effects, such as alteration of external variables, changes in the filesystem, etc..
These functions are considered convincing, as they will not expressly change any variables that other parts of the code might depend on at some point. It would seem awkward to code with these constraints, but these functions should be considered deterministic, predictable, and composable..
Functions are considered as values that can be assigned to variables, so they can be passed to and returned from other functions. That is, a function can be used as if it were a parameter or as a value that is being returned.
This implies that the function can be passed as such, rather than just the result of the function. For example, consider the double (x) function, which returns twice the value of its input parameter. Thus, double (2) would return 4.
As it is a first class function, the code (double (double (2)), would be the same as the double (4) code. This allows you to nest one function as a parameter of another and so on.
It refers to the fact that in this programming pattern there are no assignment statements. That is, new variables must be defined if you want to store additional values. Therefore, the state of a variable is constant at all times.
This eliminates the slightest possibility of unwanted effects, because any variable can be replaced by its real value during any point of execution of the program..
In functional programming there are no "for" and "while" loops. Instead, iteration relies on recursion. Recursion is implemented using recursive functions, which call themselves repetitively until the base case is reached.
Variables are immutable, that is, it is not possible to modify a variable once it has been initialized. Although a new variable can be created, modifying existing variables is not allowed.
With an example you can analyze the difference between these approaches, performing the same operation in both arrangements, which is to filter the odd numbers from a list while substituting 5 for even numbers less than 5.
It is the same calculation, with the same result. However, as you can see, the imperative code is verbose and not immediately clear. On the other hand, the declarative approach is readable and explicit, because it focuses on what you want to get.
What is defined as pure and impure functions can be clarified with some basic examples:
It means using functions in the same way that data is used. Therefore, they can be passed as parameters to another function. In the following example, the int function can be passed as a parameter to the map function:
>>> list (map (int, ["1", "2", "3"]))
[1, 2, 3]
They can be assigned to variables and returned. For example, in the following code you can assign the function hello_world, then execute the variable as a function.
- Focus on what you want to achieve (declarative) and not how to achieve it (imperative).
- They do not contain assignment statements, so after variables are given a value, they will no longer change. Therefore, functional programs do not contain side effects.
- The logical flow is clear, since the state is less dispersed and is not implicitly modified.
- Supports the concept of lazy evaluation, which means that the value is only evaluated and stored when needed.
- Since pure functions do not change any state and are completely dependent on input, they are easy to understand. The return value given by such functions is the same as the result produced by them.
- Due to the nature of the pure functions to avoid that the variables or any external data change, the implementation of the concurrency is effective.
- Functions are treated as values, passed to other functions as parameters. This improves the understanding and readability of the code..
- Pure functions take the parameters once, producing immutable output. Using unalterable values makes debugging and testing easier.
They are shorter and easier to understand than imperatives. Studies have shown that average programmer productivity in terms of lines of code is more or less the same for any programming language, translating into higher productivity.
Calling a function cannot have a different effect than calculating its result. This rules out an important source of errors, also making the order of execution irrelevant, since no secondary effect will be able to change the value of an expression, and it can be evaluated at any time.
The programmer is relieved of the burden of establishing a flow of control. As expressions can be evaluated at any time, variables can be replaced by their values.
This autonomy favors that functional programs are more mathematically manageable than conventional programs..
- The functional programming paradigm is not straightforward, making it difficult for a beginner to understand.
- It is difficult to maintain, as many objects evolve during encoding.
- In some cases, writing pure functions causes a reduction in the readability of the code.
- Immutable values in combination with recursion can lead to a drastic reduction in system performance.
- Reuse is very complicated and needs constant refactoring.
- Writing programs in a recursive style instead of using loops or loops can be a very daunting task..
- Objects may not represent the problem correctly.
- Although writing pure functions turns out to be easy, combining them with the rest of the application and with the input / output operations is quite difficult
Artificial intelligence programming is done in functional programming languages and artificial intelligence techniques migrate to real-world applications.
It also excels in the implementation of complex mathematical models. For this reason, one of the main uses of functional languages has traditionally been academic. It is useful for developing executable specifications and prototype implementations.
Many functional languages also excel at implementing parallel processing. This is due to its ability to take advantage of pure functions, which always return the same value regardless of the order in which they are executed..
WhatsApp uses the Erlang programming language, which follows the functional programming model, thus allowing more than a hundred of its employees to handle the data belonging to some 1,600 million people.
Another important carrier of the functional programming style is Haskell. It is used by Facebook in its antispam system. Even JavaScript, one of the most widely used programming languages, flaunts the properties of a dynamic type functional language.
It was designed after C ++, obtaining all its benefits while eliminating its observed weaknesses of having to be compatible with C.
It is highly scalable and concurrent, making it ideal for telecommunications and other applications that receive massive amounts of data in an unpredictable order..
This is a pure functional programming language, which uses Lambda calculus for this.
It is used in mathematical, scientific, financial, analytical and other applications. One of its strengths is making software to handle other programs.
It is an open source language that is based on Caml. It tends to create very lightweight programs, helping them to load and run faster than those created by other languages.
It is based on the LISP syntax and the ALGOL structure. Due to its simplicity, it is used in many computer science courses as an introduction to program design to show some of the fundamentals of computer programming.
Yet No Comments