FSharp : Welcome into the Functional Programming

FSharp logo
FSharp logo

I am a CSharp (C#) developer for several years now and before working with this programming language I made a few projects in C++ : I am an Object Oriented Programmer (OOP). But OOP is not the only way to develop software and sometimes it is not the easiest way to create a specific functionality.

As a software journeyman I want to sharpen my skills and learn new ones. Given that I am familiar with the OOP paradigm it could be interesting to learn a new programming paradigm. So I decided to look toward Functional Programming with FSharp (F#).

Why FSharp ?

Short answer : pressure from my peers !

Joke aside, I was hesitating between Python and FSharp to learn functional programming. Some of my colleagues advise me to pick FSharp and I also had the chance to participate to a BBL (Brown Bag Lunch) on FSharp (summary of this event here). But this is not the only reason I chose this language.

FSharp is not only a functional language, it is also possible to do OOP with it : it is a multi-paradigm language. FSharp has been designed by Microsoft Research thus it can be used with the .NET framework which is perfect for a C# developer like me. And of course it can be used inside Visual Studio.

What does it look like ?

Enough with the talking ! Let’s see some code !

For this first FSharp example, I resolved a simple problem of the Project Euler in C# and F#. This problem is called : “Multiples of 3 and 5”.

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.

Solution in C# (one of many) :

using System;
 
namespace CSharp.Euler
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("The result is : {0}", Multiple());
        }
 
        static int Multiple()
        {
            var result = 0;
            for (int i = 0; i <= 999; i++)
            {
                if (i % 3 == 0 || i % 5 == 0)
                    result += i;
            }
            return result;
        }
    }
}

Solution in F# (one of many) :

let multiple =
    [0..999]
    |> List.filter (fun x -> x % 3 = 0 || x % 5 = 0)
    |> List.sum

printfn "The result is : %d" multiple

The purpose of this example is not to compare these two languages, it is to show a bit of the FSharp syntax.

If you want to learn more or even try FSharp, I highly recommend visiting www.tryfsharp.org to check their well-crafted and interactive tutorials.

I’m just starting learning FSharp and for now I really like it and I wanted to share this enjoyment with a quick presentation of this multi-paradigm functional focused programming language. I think that FSharp will come back in future articles shortly.

See you next time !

3 thoughts on “FSharp : Welcome into the Functional Programming

  1. Hi.
    “FSharp has been designed by Microsoft Research…” and its made on the basis of OCaml, if you know f# u know 90% of OCaml.
    Good luck on function way.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s