Analyzing the visual tree in F#

I finally had my first “Aha!” moment with F# today.  After reading Robert Pickering’s amazing article about Erlang style message passing in F# I tried to create a very dumbed-down version of a function he created.  I wanted to create a function which counts how many instances there are of each type of element in a Window’s visual tree.

I’m using the WPF visual tree as a data structure to program against.  Since I can easily configure a visual tree via XAML, it makes for an excellent way to create a tree structure of diverse data types.  VisualTreeHelper is used to traverse that tree of elements, by calling its GetChildrenCount and GetChild methods.  This brings me to my “Aha!” moment…

In my previous post I recursively walked the visual tree by using a for loop, just like I would in C#.  But today that felt wrong.  I forced myself to drop my old habit of manually looping, and try to think about things differently.  As it turns out, F# makes it really easy to recursively walk down trees by using something called a “list comprehension.”  I used that feature to “declare” that I want a list of every child element of a given element, then I forward that list to the List.iter function.  The callback passed to List.iter contains the recursive call!  Check out the code in the countTypes function, where the comment reads “// Process this element’s children.”

Analyzing the visual tree in F# (code)

The output of running the demo application contains the output of two functions.  The countTypes function output is listed second, as seen below:

Analyzing the visual tree in F# (output)

I’m starting to really enjoy programming in F#.

Download the demo app here: Analyzing the visual tree in F# (demo project) Be sure to change the file extension from .DOC to .ZIP and then decompress the file.  Download the F# compiler and tools here.

About these ads

4 Responses to Analyzing the visual tree in F#

  1. Sarafian says:

    Josh, although i have played with WPF very little due to my requirements at work, i beleive that WPF is the future. So i read your blog, and you latest articles about F#.
    I have to say that I really don’t like the syntax. For me a serious programming language cannot have let and other natural language based keywords inside like VB used to have. I also don’t like the =,:=

    For me F# tries to resemble a scripting language and not a robust language as C or c# and if you would like java. And i really can’t understand how .net programmers would leave the efficient strictness of C# (which is the majority’s prefered language for .NET). I can’t see my self browsing 10000 lines of code with natural language keywards inside.

    That’s my opinion of coarse.

  2. Josh Smith says:


    I also doubt that F# will become as mainstream as C#. It is a radical departure from the “normal” languages of the .NET world so far. But I don’t think F# is intended to become as popular as C#.

    I think F# is supposed to fill a gap which C# and VB.NET leave behind. It is a great language for functional programming, which is known to make it much easier to correctly implement concurrency. It is also ideal for quick scripting-like scenarios, and creating prototypes. Whether or not F# gains traction in the .NET world is yet to be known…


  3. Anon says:

    Think of let as var in c#

  4. Marlon Grech says:


    I completely agree with you. F# will not replace C#… These 2 can work really well together… I think F# will show the world the power of the CLR!!! The reason to have a multi language VM (the CLR) was not to have C# and only… These 2 languages have different syntax but more or less the same functionality.. On the other hand with F# (and even ironpython etc.. ) you can do a lot of different other things (for instance F# will be really good for async tasks)… So there you have it… one project in C# and all its power and another dll with F# with all the other power working together… sum it up….. power + power = SUPER_POWER;

    .Net is truly amazing when you think you have seen it all…BAMMM there it is another cool feature!



Get every new post delivered to your Inbox.

Join 275 other followers

%d bloggers like this: