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#.
_______
Yellow blue soft is a proud Microsoft Bizspark partner. Tabbles (our flagship product) is developed entirely in F#, and WPF using Visual Studio 2010. |
_______
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.
[…] 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. […]
[…] 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. […]
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.
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.
Yes, a better example is required. In the meantime, I am fixing the post.
… 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.
@urza
you’re right, lately C# has been adding features from functional languages, though with some limitations (which don’t show in this example).
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.
[…] 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 http://bit.ly/bKU8pm #fsharp #dotnet #dev #visualstudio […]
How F# adapts to the way we think â?? part1…
Thank you for submitting this cool story – Trackback from DotNetShoutout…