35
votes

What languages are available that promote both object-oriented and functional programming? I know that any language that supports first-class functions can be considered functional, but I'm looking for a syntax that's specifically targeted for both coding styles.

Using such a language, I'm imagining isolating all state changes to a single portion of code and having the rest of the program be purely functional. Just thinking of it makes me drool (debugging heaven!).

So far I've discovered Scala although I've only just heard of it (and it looks amazing). Are there any big contenders in this "mixed style" paradigm?

12
I'm not really into languages that mix like that. It ruins the point for me. I'm learning Haskell right now, and then I'll be delving deeper into Clojure. My two favorite languages! :DRayne
I've used Clojure, it's great for an ex-Lisper! I may do it opposite from you and learn Haskell next.Kai
+1 I totally agree with the idea of isolating state change to dedicated parts of a program, and have it functional elsewhere (or the other way round; Kent Beck calls them "mathematical micro-worlds").ThomasH

12 Answers

21
votes

The best known are OCaml and F# (which can be vaguely described as OCaml for .NET).

There are many other multi-paradigm languages, like Oz, but they have mostly pedagogical value. By contrast, OCaml is very practical. It's almost as fast as C and almost as beautiful as Haskell :)

The popular scripting languages like Python and Ruby let you program in functional style as well. However, they don't provide one of the strongest facilities that "classical" functional languages (as well as OCaml) have: pattern matching (don't mistake it for regexp).

22
votes
12
votes

Also, many scripting languages such as Python, Ruby, Lua, etc. have this capability, but lack many of the nice features of functional languages such as algebraic data types and pattern matching.

10
votes

Haskell: Pure functional ,pretty much no OO, but go ahead, take the dive. :D

Scala: Beautiful mix of OO and FP, could possibly overtake java as premier language on the JVM in a decade or 2. I like it because it brings functional programming to the java platform, something that's sorely need IMHO.

C#: Awesome support for OO, as well as getting more functional (first class functions already, we'll see what improvements .net 4 brings)

F#: .net language Built to be functional specifically, as opposed to C#, which was originally conceived for OO stuff.

Python: Great for OO, but not at all suited to FP

Javascript: Supports first-class functions, but not specifically designed for FP like Scala and F#. Still slightly better than python IMHO.

Why do you want to mix OO and FP? As a stepping stone?

9
votes

OCaml and F# are the most popular languages that mix OOP and FP, as far as I know.

Most languages, like Ruby, mix functional programming in, but a lot of people don't even realize it. I find languages like that leave a lot to be desired on the syntax front and such.

7
votes

C#. It's imperative, which can be handy, but also has a lot of functional features. Lambdas, iterators, and LINQ are all functional.

It probably won't appeal much to purists, but it works for me.

7
votes

JavaScript, Python, and Ruby could be used that way, but Scala bumps up a notch by typing the function statically and working under JVM.

4
votes

You're really asking the wrong question. Your question is premised on there being a distinction between "OO" and "functional" programming. This distinction isn't interesting or relevant. In fact, according to the "supports first-class function" criteria, even Java is functional.

But you hit the nail on the head with "purely functional". That's the interesting bit. What languages offer you referential transparency and purity like that? Well, most of them, if you're very careful. But not many of them actually guarantee that your functions are pure. I can only think of a couple of languages that offer you that.

One of them is Haskell. In Haskell, you write programs that are purely functional from beginning to end. Side-effects are delegated to a data structure called IO, and state is handled by passing it through pure functions or encapsulating it in monads. So you have your "debugging heaven" where only a small portion of your code interacts with global state and the rest of your program is pure, and purity is enforced by the language.

3
votes

As long as you don't insist on "purity", Common Lisp supports all your needs.

1
votes

Python, javascript, common lisp, ruby, smalltalk, haskell, and ocaml, off the top of my head. It's not really an exotic combination.

0
votes

I'm wondering why you are looking for a language that specifically encourages mixing it up rather than just doing it anyway with a language that does functional programming and OO programming well? It could be easily brought into effect with Python, Ruby, Perl or similar interpreted languages. In addition C based OO languages tend to mix pure C with OO features, for example Objective C could easily be written in such a way should you choose.

EDIT: I have been made aware I'm incorrect, I've left this answer here incase someone can learn from my mistake - see comments.

-1
votes

Ruby! In Ruby, everything is an object (even literals), and it also has full functional programming support.