When to use a functional programming language?

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.

– Jules Dec 29 '08 at 11:17 6 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 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 12 jul1 at 19:10.

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.

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

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

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.

1 "Practically everything you can do in PP can be done in FP" - Actually, it is exactly everything. – BlueRaja - Danny Pflughoeft Feb 2 '10 at 20:20 @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 at 19:04 @Luigi Plinge I'd have to agree with BlueRaja. Haskellers discussing some Linux drivers written in Haskell.

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.

Also, keep in mind that most of my links are Haskell, a PURELY functional language. – PardonMyRhetoric Jun 28 at 8: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 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. – PardonMyRhetoric Jun 29 at 4:40.

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.

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 Dec 29 '08 at 10:46.

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: Do you have the time to invest in learning a new notation and a new way of thinking?

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.

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.

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.

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 jmock.org/yoga.html) Another FP-like syntax in otherwise OO languages is the use of closures, such as in Ruby.

Use a functional programming language when (without quotes):.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions