Functional Programming in F#

Over the last year I have heard more and more about F#, a functional language for the .Net platform. A great resource for getting to know the language is F# for Fun and Profit

What is Functional Programming?

Imperative and Object-Oriented Programming (OOP) treat computation as a series of statements that change the state of a program, changes to the state will then determine the program flow.

Functional Programming (FP) takes an alternate approach, treating computation as an evaluation of mathematical functions and transformations. It eschews the ideas of state and side effects. A method will return an output based only on a given input.

Mathematically speaking, OOP is generally concerned with set theory, where objects (sets) are used as properties of other objects and arranged in namespaces (supersets) whereas FP is based more on Lambda Calculus which is defined as

a formal system in mathematical logic and computer science for expressing computation based on function abstraction and application using variable binding and substitution

Or, in plain English, functional programming avoids changing state and produces no side effects. A function will recieve input, process it to create output and will not change anything else while doing so.

Some examples of how it can be used

For the purposes of these examples, I will use C# and F# as example languages. The C# examples will come in two falvours; with and without Linq which is itself an implementation of functional ideas in C#.

An example of the power of Functional Programming is that it can be used in a declarative fashion, where a statement describes what it wants to do as opposed to how to do it.

For example, if I want to create a list of every number between 1 and 100 that is divisible by 3;

// Old fashioned C#
List<int> numbersDivisibleByThree = new List<int>();

for (int i = 1; i <= 100; i++)
{
    if (i % 3 == 0)
    {
        numbersDivisibleByThree.Add(i);
    }
}
// Using Linq
IEnumerable<int> numbersDivisibleByThree =
Enumerable.Range(1, 100).Where(i => i % 3 == 0);
// F#
let numbersDivisibleByThree = [1..100] |> Seq.filter (fun i -> i%3 = 0)

A function written in F# can be a lot more succint than the corresponding function in C#. A good example is a function to lazily evaluate the Fibonacci sequence.

// C#

// generate an infinite Fibonacci sequence
static IEnumerable<long> Fibonacci() {
long n = 0, m = 1;

yield return 0;
yield return 1;
    while (true) {
      long tmp = n + m;
      n = m;
      m = tmp;
      yield return m;
    }
}

static void Main() {
    // get first 10 members as a list
    var firstTenTerms = Fibonacci().Take(10).ToList();
}
// F#
// generate an infinite Fibonacci sequence
let fibSeq = Seq.unfold (fun (a,b) -> Some (a+b, (b, a+b))) (0,1)
// get first 10 members as a list
let fibList = fibSeq |> Seq.take (10) |> Seq.toList

As you can see, despite some unfamilar constructs such as Seq.unfold, the F# method is a lot more concise.

I hope this has helped show the power and usefulness of F# and further posts will show how F# can be used in other contexts and to explain some of the functional concepts in more depth.