F# by example – part 2
August 10, 2010 by Maurizio Colucci · 5 Comments
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.
Let us deal with a slightly more complicated condition:
| if there are two dogs with the same name, print “hello” |
Again, this sentence reflects the way we think. I am now going to show you how to gradually and naturally translate it to F#.
Let us rephrase the sentence as
| if there exist two dogs D1 and D2 such that D1 and D2 have the same name, then print “hello” |
As you can see, the word “exist” highlights two different dogs at once, i.e. a pair of dogs (taken from the set of all pairs of dogs). Translating this sentence in F# would require us to generate all pairs of dogs, in order to pick a pair. Since we do not want to have to do that for now, it is easier to use the logical rule:
| ∃x,y: P ⇔ ∃x: ∃y: P |
or, in english,
| There exists x and y such that P is true ⇔ There exists x such that there exists y such that P is true. |
This logical rule allows us to rephrase the sentence in such a way that “exists” highlights a single dog. Here is the rewritten sentence:
| if there exists a dog D1 such that there exists another dog D2 such that D1 and D2 have the same name, then print “hello” |
This sentence is easier to translate to F#. However, we are not done: we have to add a condition expressing something we tend to give for granted, namely the fact that D1 and D2 are different dogs. (Otherwise the above condition will always evaluate to true.) So our sentence becomes:
| If there exists a dog D1 such that there exists a dog D2 such that D1 and D2 have the same name and D1 is different from D2, then print “hello” |
And we are done. The F# code is almost identical:
| if exists dogs (fun d1 -> exists dogs (fun d2 -> d1.name = d2.name && d1 <> d2 )) then print “hello”. |
(where the exists function is defined as in the previous episode)
See you in the next episode!
_______
![]() |
Yellow blue soft is a proud Microsoft Bizspark partner. Tabbles (our flagship product) is developed entirely in F#, and WPF using Visual Studio 2010. |
_______
How F# adapts to the way we think – part1
July 29, 2010 by Maurizio Colucci · 11 Comments
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. |
_______
FileSystemWatcher (.net wrapper): doesn’t work on FAT32 (removable or Samba)
July 26, 2010 by Andrea D'Intino · 1 Comment
HELLO WORLD,
We’ve always perceived the FileSystemWatcher as “moody”, meaning that it would work on some disks and not on some other… but we couldn’t quite understand the reasons behind it. It would work on fixed HD but not always on removable drives…
A few days ago we bought 2 of those fantastic mini NAS-adapters, which work indeed fine even if they:
1) have an embedded Linux using Samba to manage the sharing.
2) support only FAT32 partitions.
Of course we started testing Tabbles furiously and we immediately started yelling “OMG, the FileSystemWatcher doesn’t work on Network drives!”. The reason of such a panic attack is that our auto-tagging rules and one click-tagging are based on the FileSystemWatcher and Tabbles looses much of its appeal without those.
After a bit of yelling and hair-tearing we googled “FileSystemWatcher Samba” until we stumble on this article suggesting to use the “Win9xWatcherStrategy class”… deeply moved in the heart, Maurizio tries immediately to implement this but soon enough he finds out that the method “Win9xWatcherStrategy” doesn’t exist in the FileSystemWatcherClass. This means that we’ll probably bin the .net FileSystemWatcher wrapper, and move to the lower-level Win32 function.
For any of you using Tabbles Portable: be aware that for the moment the auto-tagging rules work only if your USB drive is formatted as NTFS… sorry for that but we’ll try to fix this soon!
——
But we also have some good news here: totally out of the blue, Mr. Leandro “O Dragão” posted on our forum a solution to the annoying problem of Tabbles not being able to track files moved with Explorer, have a look at this thread. This is among the most exciting thing that happened to us in the past months…we don’t really know a lot about it yet, but so far it looks really promising – Kudos to Leandro!
(Not sure why, but I believe Mr. Burns would be proud of us!)
Goodbye FileSystemWatcher – welcome full file-manager solution!
March 1, 2010 by Andrea D'Intino · 9 Comments
<
After some intense research about technologies we came to the conclusion that there is no easy way to "listen" to a filesystem and to understand when files are moved…are we missing something (our thread on MSDN forum)?
>
The (final?) solution: only files pasted using Tabbles will be tracked. Tabbles already had all of the file-management functions built-in, therefore it was pretty easy for us to listen to the file-movement initiated by Tabbles itself and then have the tagging to "follow" the files. This means that you can do cut&paste or drag&drop inside Tabbles, or you can do cut from explorer and paste in Tabbles, as well as drag&drop from explorer to Tabbles (but not the other way around!) and it will still work.
Are we missing something? If anyone out there could think of a better solution to this problem, we're all ears! Please write us using the contact form on tabbles.net Hint – how to rearrange your files thematically, like you never could before: keep in mind that Tabbles allows you to group files according to "concepts" but you can at the same time browse your disks as you do with a normal file manager. Therefore, let's say that you have a bunch of scattered files related to France and you feel like having them in the same folder. You can open the for example the tabble "France", cut all the files in there, and paste them in a brand new folder, all inside Tabbles. This way you can get files rearranged in no-time and all the tagging will be preserved. Also keep in mind that, should you want to backup your files on a disk, you can do drag'n'drop files from Tabbles to any application, including your favourite cd/dvd-burner app!
Comments are welcome! And here is the related forum post! peace and love, Andrea + Maurizio



