I have seen some questions related to functional programming on stackexchange sites which suggests it has significant popularity. I have some experience with it from many years ago in Lisp/Scheme and have seen commentary on the difference between functional and imperative/mutable approaches eg [4].

The wikipedia page on the subject says "Pure functional programming disallows side effects completely and so provides referential transparency, which makes it easier to verify, optimize, and parallelize programs, and easier to write automated tools to perform those tasks." But it is known to have a cost of up to a factor of $O(\log(n))$ slowdown factor [2] due to lack of mutable data structures, leading to a lot of research in functional programming datastructures that have a much different flavor than mutable approaches [3].

Along these lines,

What is an example for which the purely functional programming approach leads to better overall results than the imperative approach?

(Also it seems the word "mutable" here is a bit subtle. functional languages appear to allow one to "store" data eg via balanced trees, but not later "change" it. This is a little different than mutability eg in java, where initial storage and later changing are nearly identical operations eg with "setter" methods on the Java objects.)

[1] functional programming, wikipedia

[2] stackexchange, efficiency of purely functional programming

[3] tcs.se, What's new in purely functional data structures since Okasaki?

[4] Structure and interpretation of computer programs, by Abelson & Sussman

  • 2
    $\begingroup$ What kind of "better results" are you after? Better performance? Smaller code? $\endgroup$ – Dave Clarke Oct 3 '12 at 17:51
  • $\begingroup$ xodarap says in his answer this question is subjective. =( on 2nd thought maybe it should be closed by rigorous stackexchange stds. $\endgroup$ – vzn Oct 3 '12 at 17:58
  • 1
    $\begingroup$ If you fail to modify the question to make it clearer and less subjective, then perhaps it should be closed. The reason we have standards and request that posters comply to them is to maintain the level of quality of the site. $\endgroup$ – Dave Clarke Oct 3 '12 at 18:10
  • $\begingroup$ I have removed a discussion about language issues. Please restrict meta comments to a minimum and open a thread on Computer Science Meta if need be. $\endgroup$ – Raphael Oct 4 '12 at 7:15
  • $\begingroup$ I think this question (in improved form) may be better off on Software Engineering; please flag the question if you want us to be migrated there. It is also a good example of "not a real question" as it is subjective and it is not clear what is being asked. If you specified what "good" means (runtime or space efficiency, safety, security, ...) it would be a good question, imho. $\endgroup$ – Raphael Oct 4 '12 at 7:18

"Better" is obviously subjective but Evan Farrer did his master's thesis on rewriting python programs into Haskell (a purely functional language) and seeing how many bugs it caught. You can read his paper, but the Haskell compiler caught many non-trivial bugs.

A less controversial example is that pure languages are more easily parallelized.

| cite | improve this answer | |
  • 1
    $\begingroup$ He found Haskell caught bugs because it had 'powerful' type checking, not because it was a functional language. This answer is misleading. $\endgroup$ – Zac Apr 29 '19 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.