How F# adapts to the way we think – part1

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 d.name = 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 D.name = jerry, then print “hello”.

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

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

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

if
exists
dogs
(fun d
->
d.name = "jerry") then print "hello"
if there exists a dog d such that d.name = “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.

_______

Comments
  1. GiUmaTo

    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. 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.

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

    if dogs |> Seq.exists (fun d-> d.name = “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 d.name==”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.

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

  5. … 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.

  6. @urza

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

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

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

    You have to come up with a better example.

Reply To: How F# adapts to the way we think – part1

Download Area

Tabbles 4.0

(~ 9MB) Compatible with: Vista, 7, 8, 8.1, 10. For LAN mode, it requires Sql Server Express 2008 or above, or LocalDB

Write us!

Do you have questions about Tabbles, or just want to say hi? Drop us a line and we'll get back to you:

Your Name (required)

Your Email (required)

Your Message (required)

Type the 4 characters below and press Send
captcha

By continuing to use the site, you agree to the use of cookies. more information

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.

Close