F#: Perth .NET User’s Group

Thanks to Mitch Wheat for inviting me to talk, and letting me choose the topic: F#.

Also a major thanks to both Joel Pobar (for callvirt, around all things dynamic and functional spin) and SecretGeek (for slides and some helper starter scripts)

There was also a callout to Paul Stovell: “F# is Overhyped” as I was demonstrating a sum-of-squares example. Thankfully, there are better demos of F# over and above Fib sequences and other simple 17th century maths problems.

I used Luca Bolognese’s F# demonstration from PDC2008; watching his video intently and re-typing his code. A great way to learn a new language, and for many new programming paradigm.

There were some questions from the group, including a couple from the pub a little later on.

  1. Parallel across multiple machines

    Parallel/Multicore/Concurrency for the masses, not just for rarefied scientific endeavour is in vogue.

    In Luca’s F# PDC demo, the Seq.pmap and Seq.psum methods are using System.Linq.ParallelEnumerable added via System.Threading which is a part of the Microsoft Parallel Extensions Jun08 CTP.  The Parallel extensions are a part of the .NET Framework 4.0 CTP (and a part of mscorlib.dll, System.dll and System.Core.dll with .NET 4.0)

    Across multiple machines, we are in the bounds of Windows HPC. From a Q&A on the Parallel team blog:

    Question: What is the relationship between HPC Server and Parallel Extensions?  Does one replace the other?

    Neither replaces the other, and in fact they're useful in combination.  HPC Server is all about scaling out to nodes in a cluster of machines, and Parallel Extensions is all about scaling up on an individual machine.  Just as you can use Parallel Extensions to parallelize your desktop applications, you can use Parallel Extensions to parallelize the .NET applications running on nodes in your cluster.  This can be quite powerful when used in conjunction with HPC Server functionality like the new WCF-based SOA Service Broker.

    More from Lynn Langit and others on Parallel Programming in F#

    Matthew Podwysocki has an interesting post on using F# Async in C#. Cheeky, but nice. Also some notes about how the F# compiler differences with closures.

  2. F# vs. C# numerics and compiler optimisation

    There has been some recent discussions on differences between C# and F# IL. A good place to start reading about the small things.

  3. UI threads, F#, use for UI vs data manipulation

    You can write UI in F#; Windows Forms, Web Forms or WPF: the question remains, is F# the most productive UI language right now?

    As F# executables are running on the CLR, the same threading/UI considerations as C# apply. It is more likely, however, that you are doing async/parallel work in F# – but never “behind your back”. That is, you will call a method asynchronously or run a set of tasks in Parallel: both deliberately.

    In other words: with Windows Forms, Control.Invoke/BeginInvoke apply; and with WPF Dispatcher.Invoke/BeginInvoke apply as marshalling mechanisms.

  4. Assembly inclusion when creating app (eq: LINQ prior to .NET 3.5)

    The current September 2008 CTP F# is supplied under the Microsoft Research Shared Source License agreement. In (ii), you can redistribute 3 DLLs. In my experience, you need to include these DLLs with your application/dll when bundling with other code (I was slightly wrong last week, sorry)

    It may be a little early to speculate how this will appear in the final VS2010/F#. I will ask the product management guys. Maybe like the Parallel Extensions, it winds up in mscorlib?

  5. Casting types, syntax

    To step ahead of the automatic type inference in the F# compiler, you can do the following:

     let (stringlist : list<string>) = [“one”,”two”,”three”] // list of strings
    

    In F#, there is a concept of upcasting:

     let myObject = (“this is a string” :> obj) // its not a string, but an object
    

    downcasting uses a similar syntax, but it prone to runtime InvalidCastExceptions

     let myString = myObject :?> string // cast myObject down as a string
    

    And to do type testing, use a syntax such as

     if (myObject :? string) ... // test for type prior to doing something nasty
    

I subscribe to SecretGeek’s mantra from his presentation F# Eye for the C# Guy“learn one new language every year (or two)”