This is the first episode of a series meant to show how easily F# adapts to the way we think. I assume no knowledge of functional languages, but I do assume knowledge of an imperative language such as C#.


Suppose in your application you want to do the following:

if there is a dog whose name is jerry, print “hello”.

This is how we think, i.e. how the original thought forms in our mind. But, in order to implement this thought in ordinary imperative languages (such as C++ or C# before .NET 3), we’d have to translate it more or less like that:

for each d in dogs
   if = jerry then
      print "hello"

which is not close to the original sentence. As the conditions to write become more complicated, this kind of code tends to become unreadable and unmantainable.

Let us now see how much more natural it is to express the same idea in F#, and how much closer to the original sentence the F# code is.

In order to translate this to F#, it is useful to slightly rephrase our original thought like that:

if there is a dog D whose name is jerry, then print “hello”.

This is closer to F#, but let us rephrase it again slightly:

if there exists a dog D such that = jerry, then print “hello”.

now the above is practically F#. In fact, the real F# code is:

if exists dogs (fun d -> = "jerry") then print "hello"

as you can see, it reads almost the way you think:

(fun d
-> = "jerry") then print "hello"
if there exists a dog d such that = “jerry”) then print “hello”

End note:

In the above, the exists function is defined as follows:

let exists x y = List.exists y x

In the next episode I’ll deal with more complicated (but still very common) sentences and show how naturally they translate to F#.

Microsoft Bizspark logo Yellow blue soft is a proud Microsoft Bizspark partner. Tabbles (our flagship product) is developed entirely in F#, and WPF using Visual Studio 2010.


By | 2010-07-29T15:39:12+00:00 July 29th, 2010|English|11 Comments

About the Author:


  1. GiUmaTo August 26, 2010 at 1:50 pm

    I think I’ll stick with Maurizio on this one:
    the whole point of this (very first) post of this serie is to briefly introduce people to functional programming, F# functional programming specifically.
    Of course you can do the same very simple example with any language that has some functional programming baked into it … as the latest C#.
    But then I think you’re missing the point.

  2. […] This is the second episode of a series meant to illustrate how functional languages, and in particular F#, allow us to write code in a more natural way with respect to imperative languages. Here is the previous episode. […]

  3. […] This is the second episode of a series meant to illustrate how functional languages, and in particular F#, allow us to write code in a more natural way with respect to imperative languages. Here is the previous episode. […]

  4. Maurizio Colucci August 2, 2010 at 8:30 am

    MBR, thanks for the contribution.

    I did not use the |> because then I’d have had to explain it. I was trying to keep things simple.

    ps: the very purpose of the post was to explain how you can write code that reads like english.

  5. MBR August 2, 2010 at 4:31 am

    Why take a perfectly good fn like List.exists and create a helper fn with the args reversed?

    if dogs |> Seq.exists (fun d-> = “jerry”) then…

    Would be the preferred method of doing it in F#. It’s counter-productive to try and “English”-ify a synthetic language counter to its usage patterns – just confuses everyone else and breaks down when things become moderately complex.

    Of course, we can also do this in C#:

    if ((from d in dogs where”jerry”).Any()) …

    C# has both LINQ and a lighter-weight lambda syntax than F#, though F# has currying and more higher-order fns that act as sugar like |> — it’s pretty much a wash for this sort of thing.

  6. Maurizio Colucci July 31, 2010 at 12:37 am

    Yes, a better example is required. In the meantime, I am fixing the post.

  7. Martin R-L July 30, 2010 at 11:14 pm

    … which is why a better example is required, just as urza says…

    A correct C# example just shows that C# is equal to F# in this scenario.

  8. Maurizio Colucci July 30, 2010 at 2:56 pm


    you’re right, lately C# has been adding features from functional languages, though with some limitations (which don’t show in this example).

  9. urza July 30, 2010 at 1:51 pm

    That “ordinary imperative language C#” can also do this:

    if (dogs.Any(dog => == “Jerry”)) Console.write(“hello”);

    You have to come up with a better example.

  10. […] This post was mentioned on Twitter by Daniel Mohl, Patrick Veverka, Tabbles team, Vagif Abilov, Tabbles team and others. Tabbles team said: The first of a series of articles about F#: how F# adapts to the way we think #fsharp #dotnet #dev #visualstudio […]

  11. DotNetShoutout July 29, 2010 at 5:19 pm

    How F# adapts to the way we think â?? part1…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

Leave A Comment

We use technology (including cookies) to collect, use and transfer information to support this Site, including for data analytics purposes, as described in this Site's updated Privacy Policy. Your continued use of the Site signifies your acceptance of such cookies. To learn more about how to manage your cookie settings and how to exercise your rights under GDPR please see our Privacy Policy

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.