Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benefits and uses of a functional programming language [duplicate]

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

like image 812
advait Avatar asked Jul 23 '10 08:07

advait


3 Answers

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!

like image 144
Gian Avatar answered Sep 20 '22 13:09

Gian


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

like image 24
Mau Avatar answered Sep 19 '22 13:09

Mau


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

like image 40
mathk Avatar answered Sep 19 '22 13:09

mathk