views:

648

answers:

5

I know of several functional languages - F#, Lisp and its dialects, R, and more. However, as I've never used any of them (although the three I mentioned are on my "to-learn" list), I was wondering about the pros/cons of the various functional languages out there. Are there significant pros/cons, both in learning the language and in any real-world applications of said language?

A: 

Lisp has a gentle learning curve. You can learn the basics in an hour, though of course it takes longer to learn idioms etc. On the down side, there are many dialects of Lisp, and it's difficult to interact with mainstream environments like Java or .NET.

I would not recommend R unless you need to do statistics. It's a strange language, and not exactly functional. You can do functional programming in R, but most people don't.

If you're familiar with the Microsoft tool stack, F# might be easy to get into. And it has a huge, well-tested library behind it, i.e. the CLR.

You can use a functional programming style in any language, though some make it easier than others. As far as that goes, you might try Python.

John D. Cook
+2  A: 

Haskell is "extreme" (lazy, pure), has active users, lots of documentation, and makes runnable applications.

SML is "less extreme" (strict, impure), has active users, formal specification, many implementations (SML/NJ, Mlton, Moscow ML, etc.). Implementations vary on how applications are deployed wrt the runtimes.

OCaml is ML with attitude. It has an object orientation, active users, documentation, add ons, and makes runnable applications.

Erlang is concurrent, strict, pure (mostly), and supports distributed apps. It needs a runtime installed separately, so deployment is different from the languages that make runnable binaries.

F# is similar to OCaml with Microsoft backing and .NET libraries.

Scala runs on the JVM and can be used as a functional language with advanced features, or as simply a souped-up Java, or both. The flexibility is cited as a drawback for learning a functional language because it's easy to slip back into imperative Java ways. Of course it is also an advantage if you want to use existing JVM libraries.

Doug Currie
Where does scala fit in with this categorization?
Eddie
Good question, Eddie. I'll add that.
Doug Currie
A: 

ML family (SML/OCaml/F#):

Pros:

  • Fairly simple
  • Have effective implementations (on the level with Java/C#)
  • Easily predictable resource consumption (compared to lazy languages)
  • Readable syntax
  • Strong module system
  • (For F#): large .Net library available
  • Has mutable variables

Cons:

  • Sometimes too simple (no typeclasses => problems with overloading)
  • (Except F#): standard libraries are missing some useful things
  • Has mutable variables :)
  • Cannot have infinite data structures (not lazy language)

I haven't mentioned features common to most static-typed functional languages: type inference, parametric polymorphism, higher-order functions, algrebraic data types & pattern matching.

Can you please expand on "lazy"? In F#, sequence expressions can be infinite data structures, and the code to generate each element is not run until the element is requested. If that doesn't qualify as lazy, I'd like to know the definition.
Joel Mueller
Of course you can emulate laziness in a strict language, but it requires some extra coding. I should have mentioned that "cannot have by default, compared to Haskell"
+4  A: 

I'm not sure if your question is to functional languages in general, or differences between them. For general info on why functional:

http://paulspontifications.blogspot.com/2007/08/no-silver-bullet-and-functional.html

Why Functional Programming Matters

As far as differences between functional languages:

http://stackoverflow.com/questions/485418/distinctive-traits-of-the-functional-languages

The awesome thing about functional languages is that base themselves off of the lambda calculus and other math. This results in being able to use similar algorithms and thoughts across languages more easily.

As far as which one you should learn: Pick one that will have a comfortable environment for you. For example, if you're using .NET and Visual Studio, F# is an excellent fit. (Actually, the VS integration makes F# a strong contender, period.) The book "How to Design Programs" (full text, free, online) with PLT Scheme is also a good choice.

I'm biased, but F# looks to have the biggest "real-world" potential. This is mainly because of the nice IDE/.NET integration, allowing you to fully tap .NET and OO, while keeping a lot of functional power (and extending it in ways too). Scala might be possible contender, but it's more of an OO language that has some functional features; hence Scala won't be as big a productivity gain.

Edit: Just to note JavaScript and Ruby, before someone comments on that :). Ruby is something else you could take a look at if you're doing that type of web dev, as it has a lot of functional concepts in, although not as polished as other languages.

The biggest downside is that once you see the power you can have, you won't be happy using lesser languages. This becomes a problem if you're forced to deal with people who haven't yet understood.

One final note, the only "con" is that "it's so complicated". This isn't actually true -- functional languages are often simpler -- but if you have years of C or whatnot in your brain, it can be a significant hurdle to "get" the functional concept. After it clicks, it should be relatively smooth sailing.

MichaelGG
A: 

I have learnt Haskell at the university like a pure functional languaje and I can say that's really powerful, but also I couldn't find a practical use.

However, i found this: Haskell in practice . Check it, is amazing.

The characteristics of functional paradigms sometimes are pros, and sometimes cons, depending on the situation / context. Some of them are:

  • high level
  • lambda functions
  • lazy evaluation
  • Higher-order functions
  • recursion
  • type inference

Cite from wikipedia:

Efficiency issues

Functional programming languages have been perceived as less efficient in their use of CPU and memory than imperative languages such as C and Pascal.[26] However, for programs that perform intensive numerical computations, functional languages such as OCaml and Clean are similar in speed to C. For programs that handle large matrices and multidimensional databases, array functional languages (such as J and K) were designed with speed optimization in mind.

Purely functional languages have a reputation for being slower than imperative languages. However, immutability of data can, in many cases, lead to execution efficiency in allowing the compiler to make assumptions that are unsafe in an imperative language, vastly increasing opportunities for inlining.

Lazy evaluation may also speed up the program, even asymptotically, whereas it may slow it down at most by a constant factor (however, it may introduce memory leaks when used improperly).

Matias