Question

Functional programming seems to be a paradigm in computer science which has more and more echo.

I wonder which kind of problems are better solved with a functional programming approach rather than with a more traditional object oriented approach.

Thank you.

Was it helpful?

Solution

Functional programming is best suited for most kinds of problems, including anything you would normally use object-oriented programming for, except for maybe problems that require the storing of a lot of state or other side effects. Aside from that, FP handles complex problems much more gracefully than OOP, as a lot of it comes from a mathematical background (starting with the lambda calculus). You have much more flexibility as far as abstraction and composition go. An object-oriented program with a lot of design patterns could be refactored using more functional constructs which will allow you to do the same thing without the boilerplate structures that design patterns make you write. In addition to mathematics and parsing, FP has also been extensively used in artificial intelligence (particularly Lisp).

OTHER TIPS

Functional programming is great for creating programs that can undergo parallel execution since they discourage the use of global states.

Functional languages are used a lot in maths and statistics.

To further on the parallel execution point, functional languages tend to be good for graphical modelling, such as ray-tracing.

I agree with Galwegian -

"Functional languages are used a lot in maths and statistics."

However, those are not the only uses. As functional programming languages become more mainstream, youll likely starting finding business classes/libraries/software written written in these languages.

Pretty much anything that has a lot of maths.

For example the functional language ML is nice for implementing compilers and other applications manipulating trees.

If you're writing C++ templates, you're working in a pure functional language.

XSLT is an example of a functional programming language for doing transformations of data or documents represented as XML -- admittedly a very verbose and very limited one. If I remember correctly XQuery is also purely functional. The tricks is that without any concept of global state they arely on a host program to supply the data and consume their results. This helps keep XSLT programs (stylesheets) pure and reusable, but you need to add some sort of (imperative) framework to use them as part of a general-purpose processing system.

Basic spreadsheets can be seen as functional programs... :)

Data Structures. Example: Compare the imperative implementation of a Fibonacci Heap (often used as a priority queue) with the functional implementation. Often the functional code will be less than 100 lines of code whereas the imperative implementation can be many more lines of code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top