Functional Programming with MapPoint and F#: Part 1
The first of two articles by Richard Marsden on MapPoint and F#. Recently released as a full product by Microsoft, F# can be used as a functional language or as an imperative or object-oriented language, and is useful for multi-threaded programming.
F# is functional language for the .NET framework that is based on OCaml. Originally developed by Microsoft Research (Cambridge), it has recently been released as a full product by Microsoft. Functional programming has been favored by academia for a long time, but it has never expanded beyond niche applications in the commercial world. Although functional programming promises to be safer, the lack of supporting libraries and standard inter-language interfaces has been a major hurdle. F# might prove to be a major force in the adoption of functional programming. F# programs can easily access the entire .NET framework, and are about as fast as comparable C# programs. F# is also multi-paradigm. It can be used in a functional manner, but it can also be used as an imperative or objective oriented language. This allows you to easily switch to the most appropriate paradigm for the problem at hand.
Another force in F#'s favor is that of multi-threaded programming. F#'s objects and values are naturally immutable - making it much easier to write thread-safe code.
That is enough of an introduction to F#. If you would like to learn F#, there are already a number of very good books and websites. A good book that I recommend is "Expert F#" by Don Syme, Adam Granicz, Antonio Cisternino; published by Apress.
The following examples were typed into the F# command line environment (FSI.exe), but they can also be entered into a file and compiled using Visual Studio. Both are included in the F# download from Microsoft. When using the command line environment, each line is evaluated when it is completed with a ';;' token. F#s responses are not displayed for clarity. F# uses // for one line comments.
Using Late Binding
Following on from my previous article about late binding and C#, we shall first look at calling MapPoint from F# using late binding. This uses a traditional imperative approach. It also does not require a COM interop file to be created, but it is more verbose and is weakly typed.
In the fsi command line environment, type:
And that is all there is to it. It is very similar to the C# version, although the casting syntax is a little strange.
Using Early Binding
Next we shall look at early binding. This is more convenient because it allows compile-time type checking. It is also more natural and less verbose.
First you should create a MapPoint COM Interop called "Interop.MapPoint.dll", if you do not already have one. Normally you would use visual Studio to create this for you when you add a COM reference, but F# Projects in Visual Studio 2008 do not currently have this option. Therefore you should create it manually using the tlbimp.exe utility. You will need to change the paths, but an example usage is as follows:
Tlbimp.exe MPNA82.tlb /out:"Interop.MapPoint.dll" /namespace:Interop.MapPoint /sysarray /transform:dispret
We are now ready to use MapPoint in F#. First we need to reference the COM Interop that we have just created, and any .NET namespaces that we need:
Next we create a new MapPoint instance. This time we use a functional approach to also set the Visible property in the same line of code. This uses an intermediate object app which is "returned" to mappoint.
Next we will do something useful and create a pushpin. Although it is essentially an assignment, the assignment performs a number of operations to create a temporary pushpin object p. The balloon state is then set, and MapPoint's view is moved to the pushpin. After all these operations are complete, the pushpin object p is returned as the value for oPin:
And that is it! I have only touched on functional programming and not done it justice. Readers interested in functional programming should read more about F#, or learn a pure functional language such as OCaml or Standard ML.
In part 2, I shall use F# to work through an existing pushpin set, classifying pushpins according to their existing data fields and assigning pushpin symbols accordingly.
Prior to Winwaed, Richard worked as a software developer working on
seismic processing algorithms for the oil exploration industry. He holds
geology and geophysics degrees from the University of Cambridge
(Churchill College), and the University of Durham; and an
interdisciplinary MBA from the University of Dallas.