Phillip Trelford's Array

POKE 36879,255

Progressive F# Tutorials November 2011

At first glance it sounds a little like a set of rock music tutorials, based on a genre from the late 60s and early 70s performed and in a specific key. However on closer inspection the Progressive F# Tutorials seem to have a lot more to do with learning F#, an exciting new multi-paradigm programming language that’s part of Visual Studio 2010.

progfsharpbanner

Could there still be a tenuous link with Progressive Rock?

Jon Covach, once described in Contemporary Music Review, Progressive Rock as:

"a mostly British attempt to elevate rock music to new levels of artistic credibility."

Phil Trelford described in this very article, the Progressive F# Tutorials as:

“a mostly British attempt to elevate F# to new levels of developer understanding.”

Here’s How!

First, assemble of some of the big names in the F# world:

Second, mix in 2 tracks of F# goodness:

  • Track 1: Practical F# from the beginning
    suitable for programmers with C# or VB.Net experience
  • Track 2: F# in the Enterprise
    suitable those with F# experience or advanced C# and VB.Net developers
  • Each tutorial will be approximately 3 hours long and will have a very practical focus (i.e. bring your laptop or netbook). The aim of the conference is to have you programming F# confidently by the time you leave.

Third, help you shift into a Functional mind set with a Programming with the Stars session, where developers explain how they solve problems, featuring:

Finally, kick the whole event of with a look at what’s new in F# vNext!

Hope to see you on November 3rd..4th.

Tetris

Playable Silverlight mini-game implemented with a couple of hundred lines of F#!

Keys: Left Arrow = Left, Right Arrow = Right, Up Arrow = Rotate, Down Arrow = Drop

 

Can also be run as an editable script inside TryFSharp.org.

The main game loop:

let rec gameLoop () =  async {
    do! prompt "Click To Start" awaitingClick                                    
    do! inGameLoop ()         
    do! prompt "Game Over" paused        
    well.Clear()
    return! gameLoop ()
    }

The game makes use of F#’s Asynchronous Workflows to simplify the asynchronous flow. The game loop above looks as if it is run synchronously. But by simply wrapping the code in an async block it can be run asynchronously!

Resources

C# Light Syntax

Java and C# have somewhat reduced the ceremony over C++ by not requiring header files, but they are still both quite verbose. What would it look like if C# went one step further and adopted a light syntax like Python, where white space is significant?

Let’s start with an immutable Person class implemented in idiomatic C#:

namespace MyCompany.MyProduct
{
    public class Person
    {
        public Person(string name, int age)
        {
            _name = name;
            _age = age;
        }

        private readonly string _name;
        private readonly int _age;

        /// <summary>
        /// Full name
        /// </summary>
        public string Name
        {
            get { return _name; }
        }

        /// <summary>
        /// Age in years
        /// </summary>
        public int Age
        {
            get { return _age; }
        }
    }
}

Now lets extract the namespace, public modifiers, return statements and curly braces:

class Person

    Person(string name, int age)    
        _name = name;
        _age = age;    

    private readonly string _name;
    private readonly int _age;

    /// <summary>
    /// Full name
    /// </summary>
    string Name 
        get _name;    

    /// <summary>
    /// Age in years
    /// </summary>
    int Age 
        get _age;

Not too bad 30+ lines reduced to 20! But we could do more, what if triple slash comments were assumed to be summary text by default:

class Person

    Person(string name, int age)    
        _name = name;
        _age = age;    

    private readonly string _name;
    private readonly int _age;

    /// Full name
    string Name 
        get _name;    

    /// Age in years   
    int Age 
        get _age;

16 lines and dare I say it, no less readable!

Perhaps we could merge the class declaration and constructor to define a closure over the class members?

class Person(string name, int age)           

    /// Full name
    string Name 
        get name;    

    /// Age in years   
    int Age 
        get age;

9 lines and some might say the intent is actually clearer now!

Which incidentally isn’t a million miles away from what we can do with an F# class today:

type Person(name:string,age:int) =

    /// Full name
    member person.Name = name

    /// Age in years
    member person.Age = age
 

Or we could go one step further and take it down to just 1 line with an F# record:

type Person = { Name:string; Age:int }

It’s probably worth mentioning that both the F# class and record types can be consumed as classes from C# in exactly the same way as the original C# example.

So, if like me you’re a little bored of reading and writing verbose C# class declarations then F# might just be your thing!