Wednesday, 27 October 2010

F# video interviews with Don Syme (parts 1-4)

David Gristwood has posted a fascinating four-part series of video interviews with the creator of the F# programming language, Don Syme:

  1. An introduction to F#.

  2. An F# tutorial.

  3. F# and Windows Azure.

  4. F# in use at Microsoft Research.

In addition to our old F# for Scientists book that Don mentions in the interview, you may also appreciate our new Visual F# 2010 for Technical Computing book.

Tuesday, 19 October 2010

Graph algorithms: topological sort and all-pairs shortest paths

The F#.NET Journal just published an article about graph theory:

"Following on from our previous article about depth- and breadth-first searches of graphs, this article examines some more involved algorithms that also have many practical applications. A topological sort of a directed acyclic graph produces the vertices in a sequence where every vertex reachable from another appears after it in the sequence. All-pairs shortest paths finds the shortest path from any vertex to any other vertex..."

To read this article and more, subscribe to The F#.NET Journal today!

Memory leaks in asynchronous workflows

During the development of a concurrent distributed application for a client, we repeated a common mistake of tail recursing in an asynchronous workflow using the do! construct. For example:

let agent execute =
   new MailboxProcessor<_>(fun inbox ->
      let rec loop() = async {
         let! msg = inbox.Receive()
         execute msg
         do! loop()
         }
      loop())

This is problematic because using do! in this context leaks stack frames every time the workflows recurses (which is typically every time a message is processed). Moreover, F# uses a trampoline to implement this stack so the consequence is not a stack overflow but, rather, a gradual memory leak.

So if your concurrent applications appear to be leaking a few kilobytes for each message they process, search for instances of do! in tail position and replace them with return! like this:

let agent execute =
   new MailboxProcessor<_>(fun inbox ->
      let rec loop() = async {
         let! msg = inbox.Receive()
         execute msg
         return! loop()
         }
      loop())

Sunday, 3 October 2010

Representing and searching graphs

The F#.NET Journal just published an article about graph theory:

"Graph theory is an important branch of mathematics in the context of programming because important problems can often be phrased in terms of graphs. This articles takes a look at the representation of graphs in F# and introduces the basic operations of depth-first and breadth-first searches..."

To read this article and more, subscribe to The F#.NET Journal today!