Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Many people ask me to explain to them why is F# (or other functional languages) better (or different) than other languages they know (like Java, C#, C++).

Until today, I either tried showing few examples or using longs scary words or lending Tomas Petricek's book, but I find those ways either time consuming or unclear.

Is there a simple(r) way to explain functional programming to imperative programmers?

share|improve this question
I think this one belongs on programmers.stackexchange.com – Slomojo Feb 1 '11 at 21:26
I just do not get it. – Job Feb 3 '11 at 3:03
1  
It's not a functional programming that makes F# a better choice in certain domains. Killer features are algebraic data types and pattern matching. – SK-logic Feb 3 '11 at 11:19

migrated from stackoverflow.com Feb 3 '11 at 1:59

9 Answers

I would use the example of Red-Black trees, show how a two-line type definition defines the shape of all trees without having to write any constructors or access methods, and go on to show how useful pattern-matching can be when writing sophisticated symbolic manipulation functions such as the re-balancing one.

I would try to emphasize that it's a good language to write these kinds of computations in, making them concise and clear, but that the advantages may be less obvious for other categories of programs. Getting to the point where functional programmers can be considered as rational people who deliberately chose the best tool for the job they had to do would be an improvement with respect to the current situation, where they are mostly considered as lunatics by the rest of the programming community.

If that's not already clear, I wouldn't push too hard for the adoption of F# (or any other functional language). We've tried that, it doesn't work, it's time to move on. Let interested programmers come, but do not preach.

EDIT: Example taken from this nice page with a lot more detail:

type color = R | B    
type 'a tree = E | T of color * 'a * 'a tree * 'a tree

let balance = function                     
    | B, T(R, T(R, a, x, b), y, c), z, d        
    | B, T(R, a, x, T(R, b, y, c)), z, d       
    | B, a, x, T(R, T(R, b, y, c), z, d)         
    | B, a, x, T(R, b, y, T(R, c, z, d))         
        -> T(R, T(B, a, x, b), y, T(B, c, z, d))
    | c, l, x, r 
        -> T(c, l, x, r)

In function balance, the lines that start with | are patterns. Inside a pattern, uppercase letters (or generally names that start with an uppercase letter) are constructors. For instance B is one of the constructors of type color. Lowercase letter are variables. Variables always match and can be used on the right-hand-side of -> to represent the value that was matched on the left-hand-side.

When the shape of the function's four arguments is of one of the first four patterns, T(R, T(B, a, x, b), y, T(B, c, z, d)) is returned. Otherwise, the fifth pattern is tried, and since it contains only variables, it always matches. In this case T(c, l, x, r) is returned.

share|improve this answer
@Pascal - Could you add example code to help illustrate your above points? – Enigmativity Feb 1 '11 at 23:45
@Enigmativity Example added. I realize that it assumes already a bit of familiarity with the syntax, but this is not the place for a full-blown tutorial. Any online F# tutorial should get to pattern-matching early on anyway, as it is one of the central features of this family of languages. – Complicated see bio Feb 2 '11 at 5:58
Why don't I get it??? – Job Feb 3 '11 at 2:22
@Job, I trully believe you don't get it because your mindset is not "configured" do get it. If you really want to learn it, you can. By the way, I don't get it either. Yet. – Machado Feb 3 '11 at 12:42
1  
@Job - I had the same mindset by the way. You simply have to fight the instinct to dismiss what we don't understand. – ChaosPandion Feb 3 '11 at 15:04
show 4 more comments

I've given dozens of introductory talks on F# and seen dozens more. The mistake people usually make is starting with syntax or functional programming concepts. If you want to sell F# to traditional imperative/object-oriented programmers you need to explain the context in which you would use F#, because it is not a 100% replacement for any current programming language.

There are domains where imperative programming sucks

Think about writing a compiler or a physical simulation engine in C#. Certainly you could represent mathematical equations and interactions using classes and interfaces, but note that those aren't in the domain. In F# you deal with functions and data transformation, so there is less of a mismatch between how you conceptualize your ideas and the code that you write.

See other answers about how concisely you can write advanced algorithms or data structures in F#. This is because F# excels in those specific domains. F# would be a poor choice for writing forms-over-data applications, but it is a great language for writing a search engine.

There are ways in which imperative programming sucks

Have you ever heard someone say that debugging multi-threaded applications is easy? You haven't because it isn't. Imperative programs focus on telling the computer explicitly what to do. This approach is fine in most situations, but fails miserably when you get into parallel and asynchronous programming because as soon as you have two threads modifying the same data you open up a whole slew of new problems.

F# doesn't 'solve' parallel programming, but it does make it easier to integrate into your applications. Functional programming focuses on immutable data, so there is less reliance on global, mutable state. Quote "You cannot screw up what you don't change."

There are many other more subtle ways in which F# is a great language as well. There is the existing .NET stack, Visual Studio integration, the REPL, and so on. But if you want to focus on convincing standard imperative programmers, you need to focus on how F#/functional programming reframes how you think about programming.

share|improve this answer
"You cannot screw up what you don't change." -- great quote! – Paul Nathan Feb 16 '11 at 16:36
"F# would be a poor choice for writing forms-over-data applications". I actually use F# for that too. – Jon Harrop Feb 25 '12 at 20:03

Here's something that I run into a lot in C# where I know it's easier in F#:

class MyClass
{
    private readonly string param1;
    private readonly int param2;
    private readonly MyService param3;

    public MyClass(string param1, int param2, MyService param3)
    {
        this.param1 = param1;
        this.param2 = param2;
        this.param3 = param3;
    }

    public string Result
    {
        get
        {
            return param3.Calculate(param1, param2);
        }
    }
}

In F#:

type MyClass(param1:string, param2:int, param3:MyService) =
    member x.Result with get() = param3.Calculate(param1, param2)

Excuse me if my F# is incorrect, but I believe this is valid syntax, and would do the same thing.

share|improve this answer
Couldn't you do this just as easy with C# lambdas? – ronag Feb 3 '11 at 10:33
@ronag - this is obviously a simplified example. The idea is that you have a case where you don't want to expose the inner workings to the outside world. A common case is where I pass in two services, a getter and a putter, and the class gets from the getter, does a transformation, and sends to the putter service. References to the services are typically immutable, and the class encapsulates the translation logic. – Scott Whitlock Feb 3 '11 at 12:57

C# and F# follow two different programming paradigms.

C# is primarily an object oriented program. Lately, functional programming concepts have been added to C#, namely lambda expressions. But if your programming relies heavily on functional programming, F# definitely is more streamlined.

F# is primarily a functional programming language. You can do object oriented programming, but if your programming relies heavily on object oriented programming, then C# is more streamlined (IMHO)

The object oriented programming paradigm is highly suitable for implementing your software based on a domain model, and is therefore a very effective programming paradigm for writing line of business applications.

The functional programming paradigm is highly suitable for manipulating data, math, multi-threaded programming.

So if you want to explain what is good about F#, then you should point out that there are areas of programming where F# is more flexible.

One thing that I have noticed is the amount of re-usability that you can place into F# programs. In object oriented programs, the smallest reusable unit is a class. In functional programming programs, the smallest reusable unit is a function.

share|improve this answer

SERadio had an episode on F# which I was actually listening too on my way in to work today. Very interesting stuff.

share|improve this answer
  1. Show them, how Excel is the most popular functional programming language
  2. Ask them whether they would prefer to replace their Excel spreadsheets with C++/C# program
  3. Assuming they say no, they will understand the benefits of powerful functional programming languages
  4. Show them that F# is a powerful functional programming language
share|improve this answer
That's actually quite a good way... I always thought of Excel as a good place to begin with in a functional programming course, but never as a "teaser". – Ramon Snir Feb 3 '11 at 14:04
  1. F# Has Better Performance in Math
  2. F# is really good for complex algorithmic programming, financial and scientific applications
  3. F# logically is really good for the parallel execution (it is easier to make F# code execute on parallel cores)
  4. You could use F# projects in the same solution with C# (and call from one to another)

there are a few cases where F# might not be the best choice:

  1. Interop: There are plenty of libraries that just aren't going to be too comfortable from F#.
  2. Design tools. F# doesn't have any. Does not mean it couldn't have any, but just right now you can't whip up a WinForms app with F# codebehind.
share|improve this answer
Speaking of design tools, I did few nice things with F# & XAML. Even though Visual Studio doesn't integrate the XAML and the code-behind for you, the combination is very powerful (and much easier than WinForms). – Ramon Snir Feb 3 '11 at 14:03
4  
F# does NOT have better performance in any domain. It runs on the same runtime as C# and short of a few optimizations (like tail calls) it has the EXACT performance profile. – Chris Smith Feb 3 '11 at 17:04
1  
"right now you can't whip up a WinForms app with F# codebehind". But you can whip up WinForms/WPF apps in F# without the design tools. I do it all the time... – Jon Harrop Feb 25 '12 at 20:18

Show them this excellent video with Luca Bolognese: http://channel9.msdn.com/blogs/pdc2008/tl11

But I must add I don't believe F# has a bright future: it is too complicated for most corporate application programmers, and too high-level for most system programmers.

share|improve this answer
I disagree, F# may have a longer period before you hit critical mass but once that occurs... – ChaosPandion Feb 3 '11 at 3:21
4  
Pro tip: F# isn't meant for most system programmers. Just like C++ isn't meant for most web programmers. – Chris Smith Feb 3 '11 at 7:12
@Chris Smith: That's what I say - it is not meant for system programming, and it is too complicated for most application programmers. – Nemanja Trifunovic Feb 3 '11 at 16:30
i'd say its probably mostly a match for numerical modelling rather than system or apps programming. so its main competition is probably matlab – jk. Feb 16 '11 at 16:37
-1: "too complicated for most corporate application programmers". I spend much of my time teaching F# to corporate application programmers and, as I had expected, they pick it up really quickly. For example, the UK's largest insurance company (Aviva) started a 1-man project using F# 3 months ago with no prior experience of it at all. They built 10,000 lines of F# code and, with a couple of weeks help from me, are now putting it into production with four developers behind it and plan to modernize a further 1,000,000 lines of code by rewriting it in F#. – Jon Harrop Feb 25 '12 at 20:12
show 1 more comment

Do you really have to convince people a language is better than Java or C++? C# on the other hand has a lot of good things going for it so you might have a tougher sell there since the latest version of C# has a lot of functional features. One of the cool things about F# is that it has a REPL so once you get the fundamentals down growing an application by jumping back and forth between an editor and the REPL is a really enjoyable workflow. Plus, type inference is a really hard thing to give up once you get used to it but of course Java programmers don't know that because they have to type String s = "hahahah"; and they have no idea how painful that really is.

share|improve this answer
1  
First, String s = "hahahah"; is easy. Try doing IEnumerable<Tuple<int,IEnumerable<Tuple<string,double>>>> without the lovely var keyword. Second, there are usually people who know C# and will interop C# and F#. And I'm not sure if F# can be compared to C++ (I know I wrote it in the original post). It is just different and for different purposes. – Ramon Snir Feb 3 '11 at 14:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.