views:

248

answers:

3

Possible Duplicate:
Why functional languages?

I began programming with C/C++, VB, and eventually Python - all imperative languages. I took a course about programming languages and learned my first functional language - OCaml. It was terrible.

Syntax and other horrors aside, OCaml took my imperative thought process and threw it out the window. It was frustrating. I insisted that everything that could be done functionally could also be done imperatively. I thought of functional programming as imperative programming without a limb (side effects). In response to my frustration, the only benefit my professor could come up with was an FPL's ability to parallelize side-effect-free functions.

Anyways, enough talk.

  1. What are some advantages that FPLs offer above IPLs?
  2. Is there anything that can easily be done in an FPL that cannot easily be done in an IPL?
  3. Are there any real-world examples of FPLs in use, or do they mostly serve as academic exercises? (When I say real-world, I mean a project that heavily relies on the functional aspect of the language and doesn't cram an FPL into a scenario where it doesn't belong).

Thanks,
Advait

+10  A: 

First of all, almost any language in common use today is equivalent in expressive power, be it imperative or functional, so it's natural to think that anything you can do in a functional language you can probably do in an imperative one, because it's probably true.

One of the really nice things about functional languages is that their structure permits the application of Hindley-Milner type inference. This is the type system used in SML, OCaml and a bunch of other functional languages. It genuinely seems to lead to reduced rates of errors and is capable of saving you a lot of time and energy by finding bugs up-front as compile errors.

The automatic parallelisation argument is a bit over-used, especially because the promise simply hasn't eventuated. I have written explicitly parallel code in functional languages and it is nicer, IMHO, than doing something similar in Java or the like.

Anecdotally at least, I wouldn't be the first person to claim that learning a functional language makes you a better imperative programmer! That discomfort you felt in having your "imperative" thought process interrupted when using OCaml is actually a really good process to go through. It makes you question assumptions and stops you from writing code in a particular way just because you have always done it that way.

As for real-world use, you might like to look at the proceedings of the Commercial Users of Functional Programming workshops. There are also some very large projects written in various functional languages, although most of them are probably of limited interest outside fairly small communities. The theorem provers Coq and Isabelle are written in Ocaml and SML, respectively.

Whatever you do, I would persevere. I spent a long time banging my head against ML before things finally clicked. These days I'm not sure I even remember how Java or C work, because I haven't had a need for them in a long time... I just use ML!

Gian
Specifically, this video about the uses of Functional Programming at Facebook is really interesting and pretty compelling: http://cufp.org/videos/functional-programming-facebook
Gian
The 'expressive power' buys you not that much. You can row in a boat around the world or use a ship. Both have the 'expressive power' to take you around the world. Most people don't take the rowing boat.
Rainer Joswig
@Rainer, I'm not sure if you were agreeing with me or not, but that was basically the point I was making. It's trivially true that you can implement most things in any reasonable language, it's just whether you would want to or not that matters.
Gian
If you have a portable device with little memory, the a language that 'can' implement things won't help you if the runtime is larger than the available memory. Or of the application demands certain response times and the certain language needs a runtime with GC, then it might not be possible to provide these response times. There are classes of languages that just don't apply then. There are many REQUIREMENTS in technical systems that have little to do with 'expressive power' or 'wanting'. There are hard *non-functional requirements* that influence the choice of an implementation tool.
Rainer Joswig
Yes, I agree that it's certainly true that non-functional requirements should affect language choice. My reading of the original question was very much in the frame of every-day programming of standard commodity hardware, but you are correct that the situation changes somewhat if that is not the case. That said, there are functional languages well-suited to embedded programming :)
Gian
+1 for type system and "makes you a better imperative programmer". Though I would extend that: It makes you a better programmer on the whole. Neither FP nor IP is perfect, but rather different and sometimes complementary. Both shine for certain applications and suck for others (e.g.: pure FP doesn't play nice with changing state, but stateful enviroments are hard to parallelize). Knowing both you can use the one which fits your current problem best.
delnan
+6  A: 
  1. When one finally manages to silence his imperativelly (mis)trained mind, FP actually becomes easier and more fun than IP.

  2. FP tends to be safer, less bug prone, due to its declarative nature.

  3. I like to think that parallelising imperative code doubles its complexity (try yourself with a non-trivial parallel app). IMO, FP reduces the gap a lot, thanks to lack of syncronisation in many cases.

  4. Citing Gian, learning FP make you a wiser imperative programmer...

Mau
There is not that much real data about point 2. It's mostly an expectation. If you look at computers in aircrafts, they may use Ada, which is a typically imperative language. The comparable amount of code written in a FP language (OCAML, Haskell, ...) that actually runs on a flight computer is probably close to zero. The only place where FP languages are used are in verification of imperative programs, still the FP code is not running on a flight computer. Their might be exceptions, but almost all real-world safe code is not in one of these languages you expect to be safer or less bug-prone.
Rainer Joswig
Yes, it's true that most evidence for point 2 is anecdotal, but there is plenty of anecdotal evidence for it! And in fairness, there's not a lot of solid information about techniques for improving software quality in _any_ language.
Gian
There is a large amount of literature about writing safe systems for aircraft and similar systems. If FP software is safer or less buggy, why isn't it widely used in mission critical software (other than for verification)? are the runtime systems of these languages verified?
Rainer Joswig
@rainer-joswig: well they used Ada because it was approved 30 years ago, and noone wants to bother with changing the 'rules'. Performance and runtime verification are an issue too: imperative languages make for 'easier' compilers.
Mau
+2  A: 

You can read http://www.paulgraham.com/avg.html

mathk