Phil Trelford's Array
POKE 36879, 255

My First Xamarin Mobile App

July 21, 2014 00:09 by phil

Following on from F# week, Xamarin have been running a contest to build your first F# mobile app which ends today. If you missed it you can still enter, run an F# app and get a free F# T-shirt.

Over the weekend I’ve put together a simple calculator app that includes units of measure support:

units calculator

Getting started

I installed the latest version of Xamarin on my Mac. The happy path seems to be the stable release channel these days, as F# is now baked into the IDE. Once it’s installed you can start straight away developing Android apps. For iOS you need to install Xcode from the Apple store too. The iOS emulator seemed to run the faster, taking just a few seconds to build and run, so I settled on that.

The Xamarin IDE includes plenty of project templates for building Android and iOS applications with F#. I used the universal single view application template for iOS as a start point, and the Creating iOS Applications in Code tutorial as a guide. I’d also recommend checking out Rachel Reese’s excellent Introduction to F# with Xamarin article.

Note: F# is a mature, open source, cross-platform language so you get the same compiler in Xamarin as you do in Visual Studio.

Units of measure

F# has built-in support for units of measure so you can write:

let speed = 100<m> / 10<s>

This unit information is used for compile time checking, it has no cost at runtime, and subsequently no meta data is available at runtime.

For the units calculator an expression parser and units implementation is required. This was a case of here’s one I wrote earlier, see:

The implementation uses a couple of F# discriminated unions for defining unit types and a simple recursive decent parser using F# active patterns for parsing expressions. No libraries were imported and the code is just over 200 lines.

You can also play with the units implementation in F# interactive, simply highlight the source in Units.fs and then right-click and execute selected in F# interactive.

Note: Google and Bing both provide unit calculators from their respective search boxes.

Source code

All the source code is available on BitBucket:

Categories: F# | Mono | Mobile
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Loan calculator

July 2, 2014 00:17 by phil

Yesterday I came across a handy loan payment calculator in C# by Jonathon Wood via Alvin Ashcraft’s Morning Dew links. The implementation appears to be idiomatic C# using a class, mutable properties and wrapped in a host console application to display the results.

I thought it’d be fun to spend a few moments re-implementing it in F# so it can be executed in F# interactive as a script or a console application.

Rather than use a class, I’ve plumped for a record type that captures all the required fields:

/// Loan record
type Loan = {
   /// The total purchase price of the item being paid for.
   PurchasePrice : decimal
   /// The total down payment towards the item being purchased.
   DownPayment : decimal
   /// The annual interest rate to be charged on the loan
   InterestRate : double
   /// The term of the loan in months. This is the number of months
   /// that payments will be made.
   TermMonths : int


And for the calculation simply a function:

/// Calculates montly payment amount
let calculateMonthlyPayment (loan:Loan) =
   let monthsPerYear = 12
   let rate = (loan.InterestRate / double monthsPerYear) / 100.0
   let factor = rate + (rate / (Math.Pow(rate+1.,double loan.TermMonths) 1.))
   let amount = loan.PurchasePrice - loan.DownPayment
   let payment = amount * decimal factor


We can test the function immediately in F# interactive

let loan = {
   PurchasePrice = 50000M
   DownPayment = 0M
   InterestRate = 6.0
   TermMonths = 5 * 12

calculateMonthlyPayment loan


Then a test run (which produces the same results as the original code):

let displayLoanInformation (loan:Loan) =
   printfn "Purchase Price: %M" loan.PurchasePrice
   printfn "Down Payment: %M" loan.DownPayment
   printfn "Loan Amount: %M" (loan.PurchasePrice - loan.DownPayment)
   printfn "Annual Interest Rate: %f%%" loan.InterestRate
   printfn "Term: %d months" loan.TermMonths
   printfn "Monthly Payment: %f" (calculateMonthlyPayment loan)
   printfn ""

for i in 0M .. 1000M .. 10000M do
   let loan = { loan with DownPayment = i }
   displayLoanInformation loan


Another option is to simply skip the record and use arguments:

/// Calculates montly payment amount
let calculateMonthlyPayment(purchasePrice,downPayment,interestRate,months) =
   let monthsPerYear = 12
   let rate = (interestRate / double monthsPerYear) / 100.0
   let factor = rate + (rate / (Math.Pow(rate + 1.0, double months) - 1.0))
   let amount = purchasePrice - downPayment
   let payment = amount * decimal factor

Categories: F# | C# | .Net
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

F# Summer of Code 2014

May 27, 2014 00:04 by phil

In the northern hemisphere summer is nearly open us, and to celebrate the F#unctional Londoners are putting on a series of 3 free hands on coding sessions.

The sessions are open to all levels of experience (and operating systems), and I believe will be a great opportunity to pick up new skills or extend existing ones. So bring your friends, your partners, your kids and your laptops for some coding fun.


F#unctional Londoners


Fractal Forest Dojo – Thursday June 26th

In this hands on session, we'll have some fun, dig into some recursion and fractals, and create beautiful tree drawings, along the lines of this one:


This session was the brainchild of Mathias Brandewinder, first showing in San Francisco at the beginning of the year. Since then it’s spread to groups in Minsk, Washington DC and now London.

Build a 2048 bot Thursday July 10th

2048 is a fun (and very addictive) game that can be played on the Web:

Canopy is an awesome F# UI web testing framework, built on top of Selenium:

Let's put them together, and build a simple bot that plays 2048, using Canopy!

This is another session from Mathias, check out his video above.

F#ore! A pleasant round of Code Golf – Thursday July 24th

In this hands-on session with Grant Crofton, we'll be having a game of Code Golf, where the objective is to complete your program in as few (key)strokes as possible.

Throw caution and good programming practice to the wind in your quest for an ever-decreasing character count!  Well-named variables?  Not today my friend.  Sensibly modularised code structure?  Hell no!  Comments?  You must be kidding..

Although the main aim is to have fun, it's also a great way to improve your F# language knowledge.  Do you *really* need those parentheses? Is pattern matching more concise than an If? Isn't there an operator that does this?! 




This summer sees the return of the GameCraft London at Skills Matter on Saturday June 7th! Last year’s event was huge, expect this year to be even bigger.

Although not specifically an F# event, expect to see a number of teams using F#.

Check out the great GameCraft resources page to get you started, and for F# game development I’d recommend also looking at:

With F# and Mono you can target iOS, Android, Mac, PC and Linux.


fsharpWorks |> Paris


fsharpWorks/Paris '14 on Friday June 27th is a one-day conference for developers who want to take the fast track to F#. The program will feature a morning of talks by world-class experts, demonstrating F# in action on a wide range of practical applications, and an afternoon of hands-on workshops, to learn first-hand how to use it productively.

Why go?

F# is an open-source, cross-platform, functional first programming language.

F# makes it easy to model complex problems simply. It offers great benefits in terms of reliability and safety. Its lightweight syntax make it a fantastic language for scripting, rapid prototyping, and interactive data exploration, with all the performance benefits of a statically typed language. And it integrates smoothly with existing .NET code bases. And... it makes coding fun again!

F# has seen an explosive growth in 2013, and is not slowing down. So if you want to take your development skills to the next level, come and join us for a fast track to F#!

Speakers include Tomas Petricek, Scott Wlaschin, Steffen Forkmann, Mathias Brandewinder, Robert Pickering and Jeremie Chassaing.

Categories: F# | Mono | GameCraft
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed