Functional programming. Part I:
The essence of functional programming: Theoretical basis
When writing about functional programming, it is better to start from recalling programming types in general. There are two broad categories: imperative and declarative.
- In general, imperative language describes how to perform some task, while declarative specifies what exactly should be in the result.
- The main objects in the imperative paradigm are statements, as opposed to the statements in the declarative.
- In imperative programming language, the state of the variable can be changed during the program execution, while in declarative it is impossible.
- Overall, imperative languages have a lot of side effects, and declarative ones do not have any.
In conclusion of this comparison, it is necessary to say that imperative programming languages are widely used (C++, Java, C#), though they have drawbacks. Declarative languages are more complicated than imperative, and they are mainly used in academic purposes (like R), but they avoid all the side effects that are present in imperative. So, if imagining this as a battle, there is no clear winner.
Functional programming is primarily about eliminating possible side effects that are typical to imperative programming. Side effects are cases when the behavior of a function depends on something else, except what is written in it. It has an influence from outside and can change the result of the function execution, which is not desirable when developing huge, complicated software.
For the function to be without side effects, it is necessary to specify explicitly its inputs and outputs. In this way, for the first time looking at the method, it is clear what the function needs and what it will produce as a result. This is also the main rule for any programming language when trying to make it of the functional style.
Main concepts of functional programming are the following (Krauss, 2016):
- Pure functions: these were already mentioned as functions that have explicitly defined inputs and outputs. They return the same values for the same inputs, and in such a way, no side effects occur because there is no influence from outside the area of the program.
- First class or higher order functions: the function has to return some value in such a way that the entire function could be assigned to some variable. This explanation is about first class functions. As for higher order functions, they can return another function or accept one as a parameter.
- Immutable variables: defined variables cannot be changed under any circumstances. This is a strict rule in functional programming. This provides the ability to avoid other side effects that come out of variables of which state can be changed.
- Referential transparency: this concerns the ability to use the function’s output later in the program without changing the state of the function (Martinez, 2017).
- Lambda calculus: this is the base for functional programming and it consists of two more concepts. These functions are lambda functions and currying. Lambda functions are functions that are written without specifying a name for them. Currying is the process of functions executing in such a way that if there are several parameters to the function, it is executed setting only one variable from the parameters at a time. This means that the function will be executed exactly the same amount of times as the number of parameters. This occurs because of the variable immutability principle: variables cannot be changed once defined, thus it is possible to avoid dependency from outside.
Pros and Cons
In this section, the main advantages and disadvantages are listed.
Functional programming is commonly accepted to be more complicated than usual object-oriented programming (Alvin, n.d.). Though defining pure functions is easy once you get the concept, if there is a fully developed program, it is considered to be harder to combine all these into purity. And since functional programming is not just about pure functions, it requires deep knowledge in mathematics, especially lambda calculus, and thus it is more difficult in the beginning than imperative style.
Functional programming is about providing concurrency, which is not easy to reach in imperative programming. This is the result of all the previously mentioned concepts; the essence of functional programming in general is to provide code that is easier to read and maintain. For instance, the concept of the function’s order makes such code more readable compared to the object-oriented approach.
All in all, functional programming is a hot topic today and is worth studying because more readable code can be produced, and it allows for concurrency and many other advantages described in this report. Thus, it is time to think over this paradigm and perhaps make a decision of switching the programming style.
Alvin, A. (n.d.). Benefits of Functional Programming. Retrieved from https://alvinalexander.com/scala/fp-book/benefits-of-functional-programming
Krauss, A. (2016, December 1). Core Functional Programming Concepts. Retrieved from https://thesocietea.org/2016/12/core-functional-programming-concepts/
Martinez, C. P. (2017, November 2). Side effects. Retrieved from https://codurance.com/2017/11/02/side-effects/
All practical skills require some theoretical basis. Functional programming is no exception. This discipline has both theoretical and practical sides, and a good programmer should be good at each of these fields. One of our technical writers has provided this short guide about languages, advantages, and concepts of functional programming.
Programming isn’t the only discipline that our service deals with. AssignmentShark is ready to manage your homework in various disciplines: architecture, design, IT, chemistry, etc. We work day and night to help students from different countries who struggle with their assignments. We all have difficult periods in life when we need support. Our experts will be glad to lend you a helping hand at any time! Just click the order button and get high-quality assignment help online.