101

In which situations should I choose to use a functional programming language over a more verbose object-oriented language like C++, C# or Java?

I understand what functional programming is, what I don't really understand is for what types of problems is it a perfect solution?

Santosh Kumar
  • 26,475
  • 20
  • 67
  • 118
Alex Baranosky
  • 48,865
  • 44
  • 102
  • 150

9 Answers9

104

I've done some research into this myself and thought I might add CONCURRENCY to the list of reasons to use a functional language. See at some point in the near future processor speed will not be able to increase using the same cpu technology. The physics of the architecture won't allow it.

So that is where concurrent processing comes in.

Unfortunately most OOP languages cannot take advantage of multiple processors at once because of the interdependencies between data.

In pure functional programming languages the computer can run two (or many more) functions at once because those functions are not altering outside state information.

Here is an excellent article on functional programming you may enjoy.

Alex Baranosky
  • 48,865
  • 44
  • 102
  • 150
  • 42
    I wouldn't say most OO languages can't take advantage of an arbitrarily large number of processors. I would say that for a program to take advantage of many processors at the same time in an efficient and reliable manner, that program must be organized such that most compute-intensive units of code behave like pure functions. It is possible to organize a program that way in most languages. It is easier to do in functional languages than it is in imperative (including object-oriented) languages. – Zak Feb 26 '10 at 17:44
  • 2
    Thank you for linking that article. It really helped explain things for me. – abc123 Apr 09 '14 at 03:27
52

Functional languages are, in my opinion, good for mainly two things: Game AIs and mathematical computations. It's good in game AIs because of its nice list manipulations (at least in Lisp and Scheme), and for mathematical computations because of its syntax. Scheme, Lisp, and Haskell have a syntax that makes mathematical computations easy to read. The last thing I have to add is that functional languages are really fun languages. My Scheme course was one of the courses I had the most fun with.

honk
  • 9,137
  • 11
  • 75
  • 83
martiert
  • 1,636
  • 2
  • 18
  • 23
  • 2
    After I got over the fact that my brain wanted to explode in my first week of the compiler class I took in Scheme I also found it to be a lot of fun (and I got a lot better at it hehe) – Alex Baranosky Dec 29 '08 at 10:57
  • 1
    Which functional languages have you used for mathematical computations? – Jules Dec 29 '08 at 11:17
  • 9
    Also, compilers! I've heard people who've written compilers in Haskell say they'll "never write a compiler in an imperative language again". – ShreevatsaR Dec 29 '08 at 15:12
  • 1
    julesjacobs: I have used a bit of both haskell and Scheme to solve simple mathematical computations. I'm not at all good in those languages, so I have a way to go to solve more complex problems. I just can't make them fast enough by now. – martiert Jan 11 '09 at 19:10
15

A friend of mine quoted one of his college professors as saying something like the following:

Draw a grid with data types across the top and operations on those data types down the left side. If you slice the grid vertically, you're doing OO; if you slice the grid horizontally, you're doing FP.

My answer is that FP is a completely viable approach to programming with as wide a range of application as OO. As with any programming-language-choice discussion, I think the more important questions are:

  1. Do you have the time to invest in learning a new notation and a new way of thinking?
  2. Which of the languages you're considering have sufficiently rich libraries that you won't get stuck reinventing some wheel?

As to the first question, if you are doing this primarily for the learning value, I'd suggest looking at Haskell, because of the wide range of support materials (books, articles, active community, etc.)

As to the second question, Haskell has a nice range of libraries (although some are more mature than others), but Scala (a hybrid OO-functional language running on the JVM) has the advantages that you can more gradually transition into the functional style, and you have the full range of Java-accessible libraries available to you.

joel.neely
  • 30,725
  • 9
  • 56
  • 64
  • 1
    I learned Scheme in college, so I don't want to learn functional programming just for the experience. I would rather learn some functional language for practical reasons. – Alex Baranosky Dec 29 '08 at 14:46
9

Practically everything you can do in PP can be done in FP, and same thing in reverse. It's just another way to code something—another perspective on the problem and a different way to solve it.

However, because not many people use FP, the problem is more about the lack of good libraries, portability/maintainability (since the maintainer has a better chance to understand something written in C++ than Scheme), and the lack of documentation and community. I know there ARE some docs, however, compare that to C++, C# or Java and you will understand what I mean.

However, if you really want to do FP, you can use this style even in C++ and C#. Of course, it won't be a 100% FP if you use the standard library. Unfortunately, I don't think C# is optimized to be used with functional programming and it will probably create lot of performance issues.

This is more a subjective post, what I think about FP. It is not a rigorous declaration.

ErikE
  • 48,881
  • 23
  • 151
  • 196
user35978
  • 2,302
  • 1
  • 13
  • 14
  • 11
    *"Practically everything you can do in PP can be done in FP"* - Actually, it is *exactly* everything. – BlueRaja - Danny Pflughoeft Feb 02 '10 at 20:20
  • 3
    @BlueRaja - are you referring to Turing Completeness? If so, what you say does not follow. For example, Conway's Game of Life is Turing Complete, but you can't use it to write a video card driver as you can in C. Likewise I don't see any of the latest games written in Haskell. Can do in FP != Could theoretically do in a theoretical FP. – Luigi Plinge May 30 '11 at 19:04
  • @Luigi Plinge I'd have to agree with BlueRaja. [Haskellers discussing some Linux drivers written in Haskell.](http://www.haskell.org/pipermail/haskell-cafe/2005-March/009480.html) I don't have a link but I've also heard an OS has been written in Haskell (possible other functional languages too). As far as games go I don't see the latest games being written in C# or Java, are you implying they can't be used for such? [Anyway, an example of a game being written in FP.](http://joyridelabs.de/game/) Also, keep in mind that most of my links are Haskell, a PURELY functional language. – austinprete Jun 28 '11 at 08:02
  • @PardonMyRhetoric You obviously can write games in Haskell and do any computation, but the speed will be several times slower and memory use higher. So a counterexample to BlueRaja's statement would be that you cannot write "a game that is as fast and uses as much memory is in C" in FP. This is a real practical issue, not a pedantic trick (and I'm not out to bash FP at all - I'm coding Scala right now!). – Luigi Plinge Jun 28 '11 at 16:02
  • @Luigi Plinge Alright, sorry for misinterpreting what you were trying to say. Some FP languages are definitely slower, but Haskell has often been shown to be nearly as fast as C, so if you know what you are doing you could probably get similar performance. That said, it probably is true that the theoretical performance similarities aren't as real as the practical ones. I'd assume it is harder to get high performance out of Haskell than C. Anyway, happy Scala coding to you. – austinprete Jun 29 '11 at 04:40
  • 1
    @PardonMyRhetoric It also needs to be idiomatic FP. The benchmarks you see that give Haskell performance approaching that of C are written in a "low level" unidiomatic style that most Haskell programmers could not write. For example you could achive similar performance by simply making your Haskell code output and compile C code. Pointless though. A related practical issue is of what "you can do" vs what "can/could be done". If something is just too difficult, it doesn't really matter much if it's possible in theory (and "possible" for whom?). – Luigi Plinge Jun 29 '11 at 11:08
  • *I understand what functional programming is, what I don't really understand is for what types of problems is it a perfect solution?* In other words, the OP wants to know when is functional coding **more advantageous** than procedural coding, i.e. easier to code and/or faster in execution. Your answer does not help the OP at all. – Trunk Aug 22 '19 at 17:37
2

Although this is quite the subjective question, I'd just like to add that functional languages are used a lot to parse domain specific languages; the functional nature lends well to parsing grammars.

csl
  • 10,937
  • 5
  • 57
  • 89
  • 2
    Parsing DSLs? I thought with Lisp macros and the like, DSLs are implemented _as_ macros, not something to be parsed by Lisp programs. YMMV with non-Lisp languages. :-P – C. K. Young Dec 29 '08 at 10:46
2

Functional languages are good in a lot of situations. It depends on the libraries of a particular functional language.

How would you answer the question "When to use an object oriented programming language?"?

Jules
  • 6,318
  • 2
  • 29
  • 40
  • 2
    "When to use an object oriented programming language?" When you want to computationally model anything, on the other hand I have been looking for a problem that fp would be the intuitive asnswer, I still looking, that is how I found this post. – jimjim Sep 01 '19 at 12:07
  • @Arjang i guess functional programming would find tremendous use in calculation applications that only need input to generate output. No mutability and no iteration – EdgeDev Nov 28 '19 at 11:23
2

From what I've seen, it's more a matter of taste than functionality (no pun intended). There really isn't anything about either style of language that makes it inherently better or worse at specific tasks.

T.E.D.
  • 44,016
  • 10
  • 73
  • 134
  • 2
    @JonHarrop - Care to elaborate on that? I don't find it so obvious, and it would be a shame if you left the impression you were voting people down out of meer boosterisim. – T.E.D. Jan 25 '13 at 22:04
  • 1
    The ML family of languages were specifically bred for metaprogramming with the addition of sophisticated language features like pattern matching for manipulating trees (e.g. expression trees). Consequently, this family of languages are inherently a *lot* better at that specific task. – J D Jan 25 '13 at 22:41
  • 1
    @JonHarrop - I'll accept that. However, you are talking about ML, not functional languages in general. I'm a big believer in DSL's so I certianly won't argue against using a particular language for the exact task it was designed for. – T.E.D. Jan 25 '13 at 22:46
  • Today, pattern matching is found in almost all functional languages including OCaml, F#, Scala, Haskell, Clojure and Erlang. For example, I recently built a bespoke business rules engine for a client with a focus on mathematics. That is 1,000 lines of F# and is predominantly an interpreter. Lexing, parsing, type checking and interpreting input are all much easier thanks to pattern matching. – J D Jan 26 '13 at 10:41
  • @JonHarrop ..but not all. Its not an inherent feature of functional languages, which is what the question was asking about. – T.E.D. Jan 26 '13 at 16:48
  • There is no universal definition of "functional languages" so your requirement can never be satisfied. For example, I consider tail call elimination to be essential because idiomatic functional code will stack overflow without it. Clojure and Scala are regarded by many as functional languages but they do not do tail call elimination. – J D Jan 27 '13 at 16:31
2

I think previous posts are correct here is a short list, functional programming is a perfection solution for the followings:

  • stateless processing
  • mathematical equations including ML (Machine Learning)
  • immutable processes
  • when you want scalable concurrency
grepit
  • 21,260
  • 6
  • 105
  • 81
1

In general, use the language in which it's easiest to express the solution to a problem. For functional programming, this is when the solution to a problem is easily expressed in terms of functions, hence the name. Generally it's good for mathematical operations, AI, pattern matching; in general anything that can be broken down into a set of rules that must be applied to get an answer. You can only really determine the "best" language to use after you've analyzed your problem sufficiently. This is where pseudo-code comes in handy. If you find yourself writing pseudo-code that looks like FP, use FP.

Of course, all complete programming languages are functionally equivalent, so it doesn't matter really which one you choose in terms of what problems you can solve. The main effects will be in terms of coding efficiency and accuracy, and ease of maintenance.

Note also that it's possible to mimic FP within OO languages through cleverly designed APIs. For instance, I've seen numerous Java libraries (JMock is one example) that use method chaining to simulate an FP DSL. Then you'll see constructs such as:

logger.expects(once()).method("error") .with( and(stringContains(action),stringContains(cause)) );

This essentially builds a function that is evaluated to determine whether some calling sequence on a mock object is correct. (example stolen from http://www.jmock.org/yoga.html)

Another FP-like syntax in otherwise OO languages is the use of closures, such as in Ruby.

Phil
  • 815
  • 5
  • 13
  • 2
    "...it doesn't matter really which one you choose in terms of what problems you can solve". Only if you have infinite time and money to solve your problem. – J D Jan 25 '13 at 21:01