views:

907

answers:

8

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?

+6  A: 

Functional languages are, in my opinion, good for mainly two things. GamingAI and mathematical computations. It's good in GamingAI because of its nice list manipulations (atleast in lisp and scheme), and for mathematical computations because of its syntax. Both scheme, lisp and haskell have a syntax that makes mathematical coputations 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.

martiert
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
Which functional languages have you used for mathematical computations?
Jules
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
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
+1  A: 

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
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
Chris Jester-Young
+4  A: 

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
+1  A: 

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
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
+4  A: 

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 view at the problem and a different way to solve it.

However, because not much people use FP, the problem are more about the lack of good library, the portability/maintenability (since the maintener has chance to understand something written in C++ than Scheme), 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.

Since this is more a subjective post, this is more what I think about FP. It is not a rigoreous statement.

*"Practically everything you can do in PP can be done in FP"* - Actually, it is *exactly* everything.
BlueRaja - Danny Pflughoeft
A: 

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.
A: 

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
+2  A: 

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
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