Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are some problems best/worst addressed by functional programming?

I've often heard that functional programming solves a lot of problems that are difficult in procedural/imperative programming. But I've also heard that it isn't great at some other problems that procedural programming is just naturally great at.

Before I crack open my book on Haskell and dive into functional programming, I'd like at least a basic idea of what I can really use it for (outside the examples in the book). So, what are those things that functional programming excels at? What are the problems that it is not well suited for?

Update

I've got some good answers about this so far. I can't wait to start learning Haskell now--I just have to wait until I master C :)

Reasons why functional programming is great:

  • Very concise and succinct -- it can express complex ideas in short, unobfuscated statements.
  • Is easier to verify than imperative languages -- good where safety in a system is critical.
  • Purity of functions and immutability of data makes concurrent programming more plausible.
  • Well suited for scripting and writing compilers (I would appreciate to know why though).
  • Math related problems are solved simply and beautifully.

Areas where functional programming struggles:

  • Debatable: web applications (though I guess this would depend on the application).
  • Desktop applications (although it depends on the language probably, F# would be good at this wouldn't it?).
  • Anything where performance is critical, such as game engines.
  • Anything involving lots of program state.
like image 905
Carson Myers Avatar asked Jun 15 '09 21:06

Carson Myers


People also ask

What problems does functional programming solve?

Advantages Of Functional Programming It improves modularity. It allows us to implement lambda calculus in our program to solve complex problems. Some programming languages support nested functions which improve maintainability of the code. It reduces complex problems into simple pieces.

Why is functional programming less popular?

Functional programming is less popular because no major successful platform has adopted a functional language as the preferred language. I don't buy that functional languages are unpopular because they are unintuitive. Losts of stuff in JavaScript is highly unintuitive.

What is functional programming good for?

Functional programming seeks to take advantage of language support in using functions as variables, arguments, and return values to create elegant code. Because first class functions are so flexible and useful, even strongly OOP languages like Java and C# have moved to incorporate first class function support.


3 Answers

Functional programming excels at succinctness, owing to the existence of higher level functions (map, lfold, grep) and type inference.

It is also excellent at generic programming, for the same reasons, and that further increases the ability to express complex ideas in a short statement without obfuscation.

I appreciate these properties since they make interactive programming plausible. (e.g. R, SML).

I suspect that functional programming can also be verified more readily that other programming approaches, which is advantageous in safety critical systems (Nuclear Power Stations and Medical Devices).

like image 141
Alex Brown Avatar answered Oct 19 '22 19:10

Alex Brown


I would say that functional programming are suited for problem solving e.g. AI problems, math problems (this is far too easy), game engine but not too well suited for GUI and custom controls development or desktop application that requires fancy UI. I find it intuitive to think the following way (though it may be generalizing too much):

            Back-end   Front-end
Low-level   C          C++
High-level  FP         VB, C#
like image 11
Hao Wooi Lim Avatar answered Oct 19 '22 17:10

Hao Wooi Lim


Functional programming would be good for parallel programming. The fact that you're not relying on state changes with functional programming means that the various processors/cores won't step on eachother. So the types of algorithms that take well to parallelism like compression, graphical effects, and some complex mathematical tasks would also typically be good candidates for functional programming. And the fact that multi-core CPU's and GPU's are only growing in popularity also means that the demand for this type of thing will grow.

like image 6
Steve Wortham Avatar answered Oct 19 '22 19:10

Steve Wortham