F# XNA Game Ported to Windows Phone

The previous post talks about an F# Windows Phone game. Here is the same game ported to the Windows Phone.

Continue reading


Hidden Markov Models and a ‘Reactive’ Viterbi Algorithm Implementation

Brian Beckman over at c9 has implemented a reactive version of the classic Viterbi algorithm using System.IObserver. It seems that Viterbi is natrually suited to this type of an implementation because it deals with events (observations) over time so this is a very cool innovation.

I took Brian’s implementation and ported it to F# and parallelized the main computational part along the way. The parallelized part relies on the Task Parallel Library (TPL) under the covers and uses the F# PowerPack TPL wrapper.

See http://fsviterbi.codeplex.com/

Understanding Monads Sample: F# Async Monad

In the previous post I explained, at a very high level, that monads are really about function composition with side activities.

In this post, I will use an example based on F#’s Async monad to demonstrate practical uses of monads.

Imagine that there are 3 asynchronous web service calls that we want to sequence together before doing something else. The calls have data dependencies where the data from the previous call is required by the next. The calls are asynchronous so the calling thread starts the request and then continues on. The thread will not block waiting for a response as is the case with regular synchronous calls. However the next service call cannot start unless the previous one is done, which requires special handling.

Continue reading

Understanding Monads

Update: Also see this post. It contains references to detailed explanation of monads and actual monad implementations and usage with source code.

As a late comer to functional programming (FP), I struggled a fair bit with understanding monads at first.

The ‘aha’ moment for me was that monads are really about function composition.

Function composition is somewhat of an alien concept for imperative programmers. Perhaps the concept of function composition does not sink deep enough for many newcomers to FP and thus they struggle when it comes to understanding monads later.

Continue reading

Google’s Multi-Language Benchmark in F#

Recently, Robert Hundt of Google published a paper titled “Loop Recognition in C++/Java/Go/Scala”

The paper compares various aspects (run-time performance, code size, etc.)  of the implementation of the same algorithm in different languages. See the paper for a detailed analysis. The final (best) performance results for the various languages are:

Benchmark Time [sec]
C++      : 23
Java     : 89
Scala    : 58
Go       : 126

Update: See this post for F# performance on newer hardware and .Net versions.

I ported the elegantly written Scala code by Google’s Daniel Mahler to F# and then further optimized it with the help of Visual Studio 2010 Profiler (which makes profiling almost effortless) to achieve:

F# Performance (updated)
31 sec on Core Duo 2.00 Ghz
20 sec on Core i5

Continue reading