Posts Tagged: Professional F# 2.0

Dec 10

In Retrospect: The F# in Education Workshop

I was taking the elevator down after getting settled in my hotel room and as the doors opened I was awestruck by the sight of Don Syme sitting on a couch, typing away on his laptop. With a bit of trepidation I walked up to him and introduced myself.  It was immediately obvious that he was both a very friendly fellow and very excited about something in particular.

Don turned his laptop to me and said “look at this”. It was a blog post detailing the release of F# under the Apache 2.0 license. Needless to say, my mind was blown. I was dumbstruck.

Don Syme announcing to the world that F# would forever be under the Apache 2.0 license. (Photo courtesy of Migel de Icaza)

We then proceeded to dinner in the hotel restaurant. Miguel de Icaza was there, and if you know Miguel you know he’s the life of the party. He became even more animated when told about F# becoming open source and pledged to get the source integrated into Mono as soon as possible.

Later that night the Hotel turned into some kind of giant dance party and fashion show.  Thinking of the day ahead we all returned to our rooms.  I did sneak back down later though to grab a celebratory book-publishing nightcap with Ted Neward.  It’s old hat for him, but I’m still reeling from it.

Announcing Professional F# 2.0! (Click for Video)

To be honest, I was a bit intimidated by the idea of speaking to a room full of PhDs. Because of this, I put far more thought and practice into this 30-minute presentation than any of the much longer talks I had given before. Having my material down pat, I was able to turn that anxiety into gusto with what I feel was great success.

While presenting I noticed that the entire room was laughing at my jokes at first but by the end most seemed rather serious.  I took this to mean that many were offended by my critique of current teaching techniques. Had I been too heavy handed with my language?

Afterward my fears were allayed by the enthusiasm of those who came and spoke with me. Later, a swath of positive reviews confirmed that, while I may have been skirting the edge, I hadn’t gone too far.  Even so, I think I’ll try to be a bit softer in my approach in the future.  As they say, you attract more flies with honey than with vinegar.

Judith Bishop
Queen of Microsoft Research

Miguel de Icaza
Lord of all things Mono

Tomas Petricek
F# Jedi

Joe Pamer
Tamer of F# Compiler Lions

Photos Courtesy of Microsoft Research

That night I had dinner and drinks with some of the most brilliant people. Conversation ranged from type systems to the future of F# and Mono. I ended up staying up quite late talking with Tomas about his future plans. It looks as though he has a ton of great stuff for the F# community right on the horizon.

Howard Mansell
The F# Prophet of Credit Suisse

Richard Minerich
Who invited that guy anyway?

Photos Courtesy of Microsoft Research

The next day Howard and I took the train back to New York and discussed our plans for NYC-wide F# domination. Howard is almost single-handedly responsible for Credit Suisse’s rise as one of the biggest F# using companies. They now number over 100 F# users strong and continue to grow. Combined, we might just be unstoppable.

All-in-all I’d say that the event was a great success both personally and for F#. In a couple of years we’ll see some of the first big batches of graduates educated in statically typed functional programming. At that point, those the imperative object oriented camp will need to start playing catch-up.

Nov 10

An F# Whirlwind

Just under two weeks ago I was packing up my things at Atalasoft and enjoying my last Friday Beer:30 with coworkers and friends.

Moments before I left Atalasoft to seek fame and fortune.

My last Beer-30 at Atalasoft

A lot has changed in these past two weeks. I’ve moved into my Hoboken apartment and (partially) assembled a whole set of IKEA furniture, I’ve gotten started as the first employee at a brand new company called Bayard Rock, and I’ve seen quite a few exciting things happen in my own little F# world.

First, I’ve managed to get a new site up for my F# Discoveries This Week blog series. It’s called F# Central and I hope to do big things with it far beyond the scope of my weekly blog post. Stay tuned.

Second, after a year of blood, sweat and tears Professional F# 2.0 has been released.  I wrote the entirety of Part 3 and worked hard to convey every moment of functional programming epiphany that I experienced while learning F#.  I hope you’ll pick up a copy and let me know what you think.

Third, this Friday I’ll have the great honor of speaking alongside giants such as Don Syme, Tomas Petricek, Judith Bishop, Joe Pamer, and Howard Mansell at the F# in Education workshop. In my talk entitled F# in the classroom and the lab, I’ll be drawing on my own past experiences in order to paint a picture of how F# can be used to improve all aspects of academic life.  Even if you can’t make it in person be sure to watch it via the internet simulcast.

Finally, with the help of my good friends Rachel Appel and Howard Mansell I’ve got everything in place for the first meeting of the New York F# User Group.  I’m getting the feeling that the NYC F# community is about to explode and I hope you’ll be part of it.  Come join us if you’re in the area and help spread the word if you aren’t.

Apr 10

The Ted Neward F# Folding Challenge

My friend, and fellow Professional F# 2.0 author, Ted Neward recently challenged me to a bit of a Code Kata.   Take a list of numbers and compress it in a particular simple way but without any mutable state.  What makes this problem interesting is that a tech interviewer mentioned that that he hadn’t seen a functional solution to this problem.  I also wanted to share this because I think it’s a great example of how to convert an imperative loop into a functional fold.

So, on to the problem.

Given a set of numbers like [4; 5; 5; 5; 3; 3], compress it to be an alternating list of  counts and numbers.

For example, the solution for [4; 5; 5; 5; 3; 3] would be [1; 4; 3; 5; 2; 3], as the list consists of one four, then three fives and finally two threes.  Ordering counts as the initial list must be reconstructable from the compressed list.  The answer to [4; 5; 4] would be [1; 4; 1; 5; 1; 4]

The imperative solution is rather simple, we use three variables outside of a loop:  a last value, a count and an accumulator list.

let clImp list =
    let mutable value = 0
    let mutable count = 0
    let output = new System.Collections.Generic.List<_>()
    for element in list do
        if element <> value && count > 0 then
            count <- 0
        value <- element
        count <- count + 1
    if count > 0 then

Using the normal .NET mutable list type, this version works efficiently and produces the output we expect.

> let compressed = clImp numbers
   Seq.iter (fun e -> printf "%i " e) compressed;;

1 4 3 5 2 3

How might we convert this to a functional style?  In this example, the general type of operation could be thought of as the gradual building of a data structure while walking over a list.  F# just happens to have a list processing function designed just for this task.  This function is named fold and it is one of the most useful constructs in any functional programmer’s tool chest.

let clFold input =
    let (count, value, output) =
            (fun (count, value, output) element ->
                if element <> value && count > 0 then
                    1, element, output @ [count; value]
                    count + 1, element, output)
            (0 , 0, [])
    output @ [count; value]

Here, we are doing almost exactly the same thing as in the imperative version but with a fold instead of a loop.   The secret is that instead of putting variables outside of our loop and changing them with mutation, we have added them as elements in our accumulator tuple.  In this way, the values are updated when each element is visited with no mutation.

However, there is one serious problem with this example.  Appending to the end of a linked list requires recreating every node in that list.  This will make our algorithm grow exponentially slower approximately in proportion to the length of the input list.  To correct this we have two choices: do a head append with a normal fold and reverse the list when we are done, or use foldBack.  The foldBack version is a rather small step from here and looks much nicer, so let’s go in that direction.

let clFoldBack input =
    let (count, value, output) =
            (fun element (count, value, output) ->
                if element <> value && count > 0 then
                    1, element, [count; value] @ output
                    count + 1, element, output)
            (0, 0, [])
    [count; value] @ output

There are only two real changes here.  First, we are using foldBack instead of fold.  This change causes some argument reordering.  Second, we are appending to the head of the output list instead of the tail.  It works well, is rather fast and is easy to understand if you are comfortable with folds.

However, there is a bit of a dirty secret here.  Under the hood foldBack converts its input list into an array when the size is large.  As arrays have linear element look up time, they can be walked through backwards very quickly.  Does this make the solution not functional?  You’d never know unless you looked at the underlying implementation.  Anyway, however  you want to label it,  it sure works well.

If you liked this example and want to see more check out our book, Professional F# 2.0.   It’s just about to be done.  In fact, I better get back to editing.