What are the benefits of functional programming?

What do you think about the benefits of functional programming? And how do they apply to programmers today?

What are the biggest differences between functional programming and OOP?

+90
functional-programming
Sep 24 '08 at 16:03
source share
9 answers

A functional programming style is a description of what you want, not how to get it. i.e.: instead of creating a for loop with an iterator variable and going through an array that does something in each cell, you should say that the equivalent of “this label refers to the version of this array where this function was executed on all elements”.

Functional programming moves more basic programming ideas into the compiler, such as list comprehension and caching.

The biggest advantage of functional programming is brevity, because code can be shorter. A functional program does not create an iterator variable as the center of the loop, so this and other types of overhead are excluded from your code.

Another important advantage is concurrency, which is easier to do with functional programming, because the compiler performs most of the operations that required manual configuration of state variables (for example, an iterator in a loop).

Some performance benefits can also be seen in the context of a single processor, depending on how the program is written, since most functional languages ​​and extensions support lazy evaluation. In Haskell, you can say that "this label represents an array containing all even numbers." Such an array is infinitely large, but you can request the 100,000th element of this array at any time without knowing - when you initialize the array - exactly what you need is the biggest value. The value will be calculated only when you need it, and no further.

+70
Sep 24 '08 at 16:14
source share

The biggest advantage is that this is not what you are used to. Choose a language, such as Scheme, and learn how to solve problems with it, and you will become the best programmer in the languages ​​you already know. It is like learning a second human language. You assume that the others are mostly variations on their own, because you have nothing to compare with. Being susceptible to others, especially those that are not related to what you already know, is instructive.

+28
Sep 24 '08 at 16:16
source share

Why functional programming matters
http://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf

Abstract

As software becomes more and more complex, it becomes more and more important to structure it well. Well-structured software is easy to write and debug, and provides a set of modules that can be reused to reduce future programming costs.

In this article, we show that two features of functional languages, in particular, higher-order functions and lazy evaluation, can make a significant contribution to modularity. As examples, we manipulate lists and trees, the program several numerical algorithms and implement heuristic alpha-beta (an algorithm from artificial intelligence used in the game program). We conclude that since modularity is the key to successful programming, functional programming offers important benefits for software development.

+11
May 23 '10 at 18:29
source share

A good starting point would be to try to understand some things that are impossible in imperative languages, but possible in functional languages.

If you are talking about computability, then, of course, nothing is possible in functional, but not imperative programming (or vice versa).

The point of the various programming paradigms is not to make things possible that were previously impossible in order to facilitate a task that was difficult.

Functional programming makes it easier for you to write programs that are concise, error-free, and parallelizable.

+10
May 23 '10 at 18:31
source share

It does not have to be one or the other: using a language such as C # 3.0 allows you to mix the best elements of each. OO can be used for large-scale structure at the class level and above. Functional style for small-scale structure at the method level.

Using the functional style allows you to write code that clearly states its intention without mixing with the instructions of the control flow, etc. Due to principles such as free programming with a side effect, it’s much easier to talk about code and verify that it is correct.

+5
Sep 24 '08 at 16:10
source share

I think the most practical example of the need for functional programming is concurrency - functional programs are naturally reliable in streaming mode, and given the growth of multi-core hardware, this is of utmost importance.

Functional programming also increases modularity - you can often see that methods / functions in the imperative are too long - you almost never see a function more than a couple lines. And since everything is untied - reuse has improved a lot, and unit testing is very simple.

+5
May 23 '10 at 18:25
source share

As soon as the program grows, the number of commands in our dictionary will be too high, which will make its use very difficult. This is where object-oriented programming makes our life easier, as it allows us to better organize our teams. We can associate all the commands that are associated with the client with some client object (class), which makes the description more understandable. However, the program is still a sequence of commands that determines how it should act.

Functional programming provides a completely different way to expand vocabulary. Not limited to adding new primitive commands; we can also add new control structures - primitives that determine how we can create teams to create programs. In imperative languages, we could compose commands in sequence or using a limited number of built-in constructs such as loops, but if you look at typical programs, you will still see many repeating structures; common ways to combine teams

+3
Aug 10 '11 at 20:40
source share

Do not think about functional programming in terms of "necessity." Instead, think of it as another programming method that will open your mind in the same way as OOP, templates, assembly language, etc. Perhaps you completely changed your way of thinking when (if) you recognized them. Ultimately, learning functional programming will make you a better programmer.

+1
May 23 '10 at 18:37
source share

If you do not already know functional programming, then studying it will give you more opportunities to solve problems.

FP is a simple generalization that advances functions to first-class values, while OOP is for large-scale code structuring. However, there is some overlap where OOP design patterns can be represented directly and much more succinctly using first-class features.

Many languages ​​provide both FP and OOP, including OCaml, C # 3.0, and F #.

Cheers, John Harrop.

0
Oct 19 '08 at 4:59
source share



All Articles