views:

1477

answers:

8

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

What are the greatest differences between functional programming and OOP?

+3  A: 

It doesn't have to be one or the other: using a language like C#3.0 allows you to mix the best elements of each. OO can be used for the large scale structure at class level and above, Functional style for the small scale structure at method level.

Using the Functional style allows code to be written that declares its intent clearly, without being mixed up with control flow statements, etc. Because of the principles like side-effect free programming, it is much easier to reason about code, and check its correctness.

Samuel Jack
+19  A: 

The style of functional programming is to describe what you want, rather than how to get it. ie: instead of creating a for-loop with an iterator variable and marching through an array doing something to each cell, you'd say the equivalent of "this label refers to a version of this array where this function has been done on all the elements."

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

The biggest benefit of Functional programming is brevity, because code can be more concise. A functional program doesn't create an iterator variable to be the center of a loop, so this and other kinds of overhead are eliminated from your code.

The other major benefit is concurrency, which is easier to do with functional programming because the compiler is taking care of most of the operations which used to require manually setting up state variables (like the iterator in a loop).

Some performance benefits can be seen in the context of a single-processor as well, depending on the way the program is written, because most functional languages and extensions support lazy evaluation. In Haskell you can say "this label represents an array containing all the even numbers". Such an array is infinitely large, but you can ask for the 100,000th element of that array at any moment without having to know--at array initialization time--just what the largest value is you're going to need. The value will be calculated only when you need it, and no further.

C. Lawrence Wenham
I feel your first paragraph is closer to describing declarative relational programming like Prolog than functional programming.
McPherrinM
@McPherrinM: functional languages are declarative, instead of imperative.
Lie Ryan
+5  A: 

The biggest benefit is that it's not what you're used to. Pick a language like Scheme and learn to solve problems with it, and you'll become a better programmer in languages you already know. It's like learning a second human language. You assume that others are basically a variation on your own because you have nothing to compare it with. Exposure to others, particular ones that aren't related to what you already know, is instructive.

Just Some Guy
that's a benefit of learning it, not a benefit of the paradigm itself
Moe
But are they really separate? From the standpoint of the original questioner, I would say not - they are most likely looking for benefits in total of spending the effort to learn a functional language.
Kendall Helmstetter Gelner
"that's a benefit of learning it, not a benefit of the paradigm itself". The paradigm will leak over into your other OOP work and can help simplify your development there. You can approach problems from a "compute this output from this input" and "compose these two functions that compute new data" instead of "wait---what was the state of some shared variable over there?" and "did I get these procedures to execute in the correct order?". Seriously, you get these benefits (from understand the FP paradigm) in Python, C#, C++, Java, you name it.
Jared Updike
+2  A: 

If you don't already know functional programming then learning it gives you more ways to solve problems.

FP is a simple generalization that promotes functions to first class values whereas OOP is for large-scale structuring of code. There is some overlap, however, where OOP design patterns can be represented directly and much more succinctly using first-class functions.

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

Cheers, Jon Harrop.

Jon Harrop
A: 

I think the most practical example of the need for functional programming is concurrency - functional programs are naturally thread safe and given the rise of multi core hardware this is of uttermost importance.

Functional programming also increases the modularity - you can often see methods/functions in imperative that are far too long - you'll almost never see a function more than a couple of lines long. And since everything is decoupled - re-usability is much improved and unit testing is very very easy.

Bozhidar Batsov
+3  A: 

Why Functional Programming Matters
http://www.cs.chalmers.se/~rjmh/Papers/whyfp.pdf

Robert Harvey
Why have you posted the same answer twice?
missingfaktor
@Rahul: Probably one of those days when I haven't had my cup of coffee yet. I deleted the other answer.
Robert Harvey
+1  A: 

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

If you're talking about computability, there is of course nothing that is possible in functional but not imperative programming (or vice versa).

The point of different programming paradigms isn't to make things possible that weren't possible before, it's to make things easy that were hard before.

Functional programming aims to let you more easily write programs that are concise, bug-free and parallelizable.

sepp2k
A: 

Do not think of functional programming in terms of a "need". Instead, think of it as another programming technique that will open up your mind just as OOP, templates, assembly language, etc may have completely changed your way of thinking when (if) you learned them. Ultimately, learning functional programming will make you a better programmer.

Justin Ethier
Its needed for Math =)
never_had_a_name