15 Replies - 9200 Views - Last Post: 02 April 2011 - 01:12 PM

What're the advantages of functional languages?

I'm a C#/Java/C++ programmer. I've been in these languages for a while, but I've always in the back of my mind had a nagging curiosity about functional languages. I have a few questions about them.

What's the general advantages of a functional language over a procedural one?

Functional languages seem strange to me, is there a performance hit due to their different structure (is it harder to translate to assembly/machine code)?

What are the 3 most common functional languages in industry, what applications are they used for, and what different advantages/disadvantages to the 3 have comparatively to each other?

I tried Prolog back in college, but I couldn't even get hello world to print, lol. I assume there was something I was totally missing about the language back then. Is Prolog a functional language, or a completely different beast?

Replies To: What're the advantages of functional languages?

Re: What're the advantages of functional languages?

Posted 25 March 2011 - 12:38 PM

POPULAR

Functional programming languages are based around precisely what it sounds like: functions. Pure functions, to be exact. Pure functions are functions that do not have side effects. They take arguments and they return a value. They do not check the state or modify the state of variables. They do not print to the screen. They do not launch nuclear missiles at China. They are very dumb functions. They know nothing of the outside world. A call to a pure function can be replaced with the value it returns in a program and that program will run as normal.

As simple as it is, it is very important. When most of your application is made up of pure functions, you have a much lower amount of complexity. You end up with less bugs because there are less stateful things happening.

Building upon this, the limitation of state is important. Where OOP and such are built around the concept of state and mutation, functional languages try to isolate and limit that state. Most functional languages have immutable data structures. These structures are never actually changed. When you modify them, new structures are created and the old structures are never modified, keeping things immutable. Clojure, for example, has persistent data structures with structural sharing so these changes are blazing fast because, while you might assume that making a change would result in a whole new data structure being copied from the old one, that isn't actually the case.

Mutable state is a huge source of complex bugs and corner cases. Functional languages, for the most part, eliminate or isolate state as much as possible.

One thing is very clear: concurrency is hard. Functional programming makes it a lot easier. In OOP languages, you have to deal with managing state across multiple threads without ruining that state. In functional languages, when the majority of the work being done is being done by pure functions, you don't have to worry about it.

Clojure in particular is a non-purely functional language. One of Clojure's primary goals is to make the management of state across threads sane and relatively simple compared to OOP counterparts. It does a very good job at it.

I'm not a real performance jockey, but being functional doesn't mean being slow, and the structure of functional languages has little to do with their speed. The syntax of functional languages vary greatly (see Clojure vs Haskell). OCaml is known for being around C speed. Clojure is very fast as-is, and can reach (and sometimes exceed) the speeds of Java with after-the-fact optimization.

The top functional languages in the industry? That's a weird question. I think you're asking "which 3 functional languages am I most likely to get a job doing?". If so, that's slightly easier to answer.

I'd probably have to say Clojure, Scala, and Haskell. I'm not really basing these on statistics as much as experience. There is not a big demand for any of these languages in the industry yet. However, I feel that the former two, Clojure and Scala, are the ones that are growing the fastest. Clojure is 3 years old and a *lot* of companies are already using it. Some companies began using it in it's first year! There hasn't been a language with growth like Clojure in quite a while.

As for a comparison of the three, that's even harder. Let's start with Haskell. Haskell is a purely functional programming language. It's the only purely functional language out of all of them. This also makes it possibly the most difficult to grok of all of them. Being pure means that there is *no* mutable state without resorting to magic tricks. Furthermore, all functions are pure (without resorting to magic tricks). I think Haskell is a very good language for *anybody* to learn, because of the fact that it's pure. It'll take functional programming, turn it sideways, and shove it up your ass with the force of a case of dynamite. However, it can be the most difficult of the three to learn when coming from procedural/OOP languages. Also, not all problems are pure problems. Sometimes you just need state. Haskell's purity makes some things difficult, and it leads some people to question it's practicality.

Scala is a different beast entirely. Not only is it not purely functional, it's also OOP. Scala is a mostly OOP language with a lot of functional features. It, of course, encourages less mutable state than, say, Java, but not nearly to the extent of other functional languages. Scala is definitely a step up from Java, but as far as functional programming goes, it isn't the best option. I'd recommend Scala to someone who wants something better than Java but doesn't want to go very far. I would not recommend it to someone interested in functional programming and it's benefits/capabilities.

Clojure is in between. It is not a purely functional language, and it doesn't support OOP in the traditional sense. Clojure gives you the best of both worlds. It takes the good from OOP and the good from functional programming and presents them in a way unique to Clojure. Clojure is built for concurrency and has a software transactional memory system built right into the language. It's focused on the sane management of state in a multi-threaded environment, and that is one of the most important things about it. It supports OOPish things like polymorphism in multimethods (that are more flexible than the polymorphism one might find in Java), protocols, and records. Clojure has a rich set of extremely fast immutable and persistent data structures. Furthermore, Clojure is a dialect of Lisp. Depending on where you come from, Lisp may either seem like a good or bad thing to you. It is good. Being a Lisp and following the code-as-data philosophy means Clojure has a powerful macro system. This is metaprogramming the likes you have never seen, and it'll blow your mind. The drawback of being a Lisp is that a lot of people don't understand the purpose of the parenthesized syntax and run screaming from it. It may seem bizarre and foreign at first, but it becomes second nature in time. Eventually you come to actually enjoy and value the simplicity of the syntax. Besides that, any decent editor can be set up to match parentheses and highlight them for you to make it easier to see depth and such.

All of these are excellent languages and I encourage you to investigate all of them. I obviously have a bias towards Clojure, and I'm aware that I didn't do the other languages as much justice as I did Clojure. Anyone more familiar with Scala or Haskell is welcome to step in and give their own elevator speech for them. This is just how I see things from where I sit. Other perspectives are always nice.

Finally, what are they used for? Everything. Simply everything. While functional programming makes them especially good for certain things, they are just as good at doing anything as other languages are. They are general-purpose to the extreme, just like any given OOP language, and you will find projects written in them in every domain imaginable.

I hope this was helpful and I hope it was enough to encourage you to look into functional programming more. You wont regret it.

Re: What're the advantages of functional languages?

Hah! I could have swore that I mentioned that. It was the first thing I wrote, but then I deleted it because I wanted it to be the last thing I addressed. I forgot somewhere after page 20.

Something else I'd like to add.

I've only scratched the surface here. There is more to functional programming than I can easily explain in a single sitting. For example, functional code is often more concise than the imperative/OOP alternative. Higher order functions often abstract over things that you'd repeat over and over again in other languages. There is a lot more to it than meets the eye.

Re: What're the advantages of functional languages?

I decided I wanted to start learning functional programming in a pure functional language; I, therefore, chose Haskell. I'm really enjoying it! Thanks for the input here in the thread.

As an additional concern for me, is there an easy plugin to integrate Haskell into Eclipse or Visual Studio 2008 (I'm running Windows 7)? I tried last night to install EclipseFP 2, but I kept hitting dependency issues.

Looking at Visual Studio, it seems Microsoft is promoting F#, which, as I understand it, isn't purely functional. It could be worth learning later, though.

Prolog has peaked my interest, too. I read up on it, turns out its a logic language and good for AI. I think that's gonna be my project after Haskell. Where would I go here to post questions about Prolog (if there is a place to go)?

Re: What're the advantages of functional languages?

Posted 29 March 2011 - 08:52 AM

I think Prolog is going to be in "Other Languages." Prolog isn't necessarily good for AI, but as a subject within AI. Different areas are better accomplished with different paradigms. Logic programming is pretty cool, but I haven't seen much about it here yet.

Re: What're the advantages of functional languages?

I decided I wanted to start learning functional programming in a pure functional language; I, therefore, chose Haskell. I'm really enjoying it! Thanks for the input here in the thread.

As an additional concern for me, is there an easy plugin to integrate Haskell into Eclipse or Visual Studio 2008 (I'm running Windows 7)? I tried last night to install EclipseFP 2, but I kept hitting dependency issues.

Looking at Visual Studio, it seems Microsoft is promoting F#, which, as I understand it, isn't purely functional. It could be worth learning later, though.

Prolog has peaked my interest, too. I read up on it, turns out its a logic language and good for AI. I think that's gonna be my project after Haskell. Where would I go here to post questions about Prolog (if there is a place to go)?

xclite got it right. Prolog goes in Other Languages. As for Haskell IDEs, your best bet is Leksah. I'm not sure how actively maintained EclipseFP is these days. There used to be a Visual Studio plugin, but hasn't been updated in many years.

Re: What're the advantages of functional languages?

I decided I wanted to start learning functional programming in a pure functional language; I, therefore, chose Haskell. I'm really enjoying it! Thanks for the input here in the thread.

As an additional concern for me, is there an easy plugin to integrate Haskell into Eclipse or Visual Studio 2008 (I'm running Windows 7)? I tried last night to install EclipseFP 2, but I kept hitting dependency issues.

Looking at Visual Studio, it seems Microsoft is promoting F#, which, as I understand it, isn't purely functional. It could be worth learning later, though.

Prolog has peaked my interest, too. I read up on it, turns out its a logic language and good for AI. I think that's gonna be my project after Haskell. Where would I go here to post questions about Prolog (if there is a place to go)?

xclite got it right. Prolog goes in Other Languages. As for Haskell IDEs, your best bet is Leksah. I'm not sure how actively maintained EclipseFP is these days. There used to be a Visual Studio plugin, but hasn't been updated in many years.

Re: What're the advantages of functional languages?

Posted 29 March 2011 - 08:33 PM

here is a really good read from a site that Dogstopper turned me on to. i have been trying to push my self out of my imperative shell lately. im attempting to learn lisp right now, it's proving to be pretty interesting so far.

Re: What're the advantages of functional languages?

Posted 29 March 2011 - 08:44 PM

ishkabible, on 29 March 2011 - 11:33 PM, said:

here is a really good read from a site that Dogstopper turned me on to. i have been trying to push my self out of my imperative shell lately. im attempting to learn lisp right now, it's proving to be pretty interesting so far.

And it was Raynes that gave me link...like...5 months ago? I've dabbled with functional since then. When I get less busy and I don't have an job that demands PHP or Java, I definitely want to continue farther.

Re: What're the advantages of functional languages?

Posted 31 March 2011 - 01:05 PM

im 1/4 of the way though Land of Lisp. i have gotten to where i can understand Lisp code but i have some difficulty coming up with a solution on my own. my favorite thing that i learned so far is eval. executing arbitrary code at run time is very interesting.

Re: What're the advantages of functional languages?

Posted 31 March 2011 - 04:02 PM

Lua has it to, in fact most interestedinterpreted language do. whats different about Lisp is how it stores the data that's evaluated. it stores data and code the same way (in lists). lists are easy to manipulate in Lisp so you can do some very complex things that would require some complex string operations otherwise. Lisp has really started to catch my attention.

Re: What're the advantages of functional languages?

Posted 31 March 2011 - 04:10 PM

I think you mean interpreted languages, not "interested" languages.

That's the cool thing. Theoretically, output (technically not "output") from your code could be code in itself, which is not as easy to do in languages like Python. Don't get me wrong, I like Python, but to have data be code would be nearly impossible (and it would be hacked together). I mean, we can output stuff that can be interpreted sure, but not in the same way that languages like Clojure do.