111

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?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Rayne
  • 31,473
  • 17
  • 86
  • 101

9 Answers9

87

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.

Chris Wenham
  • 23,679
  • 13
  • 59
  • 69
  • 14
    I feel your first paragraph is closer to describing declarative relational programming like Prolog than functional programming. – McPherrinM May 23 '10 at 18:54
  • 6
    @McPherrinM: functional languages are declarative, instead of imperative. – Lie Ryan Sep 03 '10 at 05:26
  • 2
    Seems you are [confusing DP vs. IP, with procedural vs. FP](http://stackoverflow.com/questions/602444/what-is-functional-declarative-and-imperative-programming/8357604#8357604). FP is providing for separation-of-concerns by emphasizing function composition, i.e. separating the dependencies among the subcomputations of a deterministic computation. – Shelby Moore III Dec 08 '11 at 01:32
  • 2
    @LieRyan incorrect. Please see the link in my prior comment – Shelby Moore III Dec 08 '11 at 01:33
  • Concurrency is having multiple threads which may interact with each other, which is imperative. Doing multiple independent computations at the same time is called *parallelism*. See http://en.wikipedia.org/wiki/Concurrency_(computer_science) – Lambda Fairy Dec 09 '11 at 01:28
33

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.

Kirk Strauser
  • 30,189
  • 5
  • 49
  • 65
  • 32
    that's a benefit of learning it, not a benefit of the paradigm itself – Moe Sep 24 '08 at 16:42
  • 2
    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 Jan 30 '09 at 07:16
  • 3
    "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 Nov 09 '09 at 19:35
14

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 is more and more important to structure it well. Well-structured software is easy to write and to debug, and provides a collection of modules that can be reused to reduce future programming costs.

In this paper we show that two features of functional languages in particular, higher-order functions and lazy evaluation, can contribute significantly to modularity. As examples, we manipulate lists and trees, program several numerical algorithms, and implement the alpha-beta heuristic (an algorithm from Artificial Intelligence used in game-playing programs). We conclude that since modularity is the key to successful programming, functional programming offers important advantages for software development.

Community
  • 1
  • 1
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
12

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
  • 363,768
  • 54
  • 674
  • 675
8

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
  • 55,802
  • 13
  • 100
  • 117
6

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
  • 32,712
  • 16
  • 118
  • 155
4

Once the program grows, the number of commands in our vocabulary becomes too high, making it very difficult to use. This is where object-oriented programming makes our life easier, because it allows us to organize our commands in a better way. We can associate all commands that involve customer with some customer entity (a class), which makes the description a lot clearer. However, the program is still a sequence of commands specifying how it should proceed.

Functional programming provides a completely different way of extending the vocabulary. Not limited to adding new primitive commands; we can also add new control structures–primitives that specify how we can put commands together to create a program. In imperative languages, we were able to compose commands in a sequence or using a limited number of built in constructs such as loops, but if you look at typical programs, you'll still see many recurring structures; common ways of combining commands

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
2

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
  • 131,333
  • 52
  • 229
  • 284
0

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.

J D
  • 48,105
  • 13
  • 171
  • 274