F# Hawkeye : Functional Programming

(Harry is @ DevTeach in Vancounver with his family this week. He was hoping to still do Morning Coffee posts, but that’s turned out to be infeasible. So instead, you get a series of pre-written posts about F#.)

If you’re coming from the imperative object-oriented world of C#, Java or VB, functional programming just seems odd at first. But you might as well start getting used to it – functional aspects have been leaking into C# and VB with every version. The way LINQ chains together iterators (Where, Select, Order By, etc) is heavily influenced by FP (aka functional programming).

It takes a while to get the hang of FP, but I’ve found that some problems that are hard to solve in C# are easy in F#. For example, in my parsing code, I have a bunch of boilerplate caching and tracing code that I need to execute in each parsing function. How do I do that in C# without having to cut and paste the boilerplate code over and over again?

In F#, I wrote a function called CacheAndTrace that takes a parsing function as a parameter and returns a new function that adds the caching and tracing code. Since the new function has the same signature as the original parsing function, the caller can’t tell the difference but it saves me having to write the boilerplate code over and over again.

Actually, you can do this type of functional composition with C#. In fact, I first wrote my version of the CacheAndTrace function in C#. But F#’s syntax is much better for functional programming – probably since it was designed for FP, rather than having FP tacked on later.

My father once compared functional programming to Aspect Oriented Programming. AOP is about factoring apart cross cutting concerns, which tends to be hard to do in a straight-up OO language. But in an FP language, AOP-esque separation of concern is fairly straightforward.

F# isn’t a pure functional language – it’s actually a multi-paradigm language, so you can do imperative and OO stuff if you want to. For example, variables are typically immutable in functional programming. So how do you do something like cache the most recent result from a given function – which is what my CacheAndTrace function does? Frankly, I don’t know. But in F#, I can easily mark the cache value as mutable so I can update it on every call. Sort of the best of both worlds, though mixing and matching can get a little tricky.

F#’s OO support is really useful when you interop with other .NET languages, since they’re mostly OO themselves. For example, I’m using NUnit test cases for all my parsing code. My parsing code is F#, so I wanted to write my tests in F# as well. NUnit requires test methods to be grouped into classes known as test fixtures. Frankly, if I were designing a native F# xUnit library, I wouldn’t require all the test methods to be grouped into a class. But it’s easier to just define an test fixture object in F# rather than build my own xUnit Framework for F#

F# as a Second .NET Language

(Harry is @ DevTeach in Vancounver with his family this week. He was hoping to still do Morning Coffee posts, but that’s turned out to be infeasible. So instead, you get a series of pre-written posts about F#.)

I’ve been spending some real quality time with F# of late. I’ve been getting into parsing again, and it turns out that functional pattern matching languages like F# are really good at text processing. After claiming I’d learn F# this year, then abandoning the effort to learn Powershell, I went back to F#. Nothing against PowerShell – I’ve moved over to using it as my primary command line shell and have tricked out my startup script and everything. But I haven’t found much need to code in it lately.

If you’re a .NET ninja guru, you owe it to yourself to take a long look at F#, if for no other reason to expand your mind. It takes a while to get used to. Don Syme (aka father of F#) can attest I’ve been peppering him with questions (Thanks, Don!). I’ve also been writing code in C# and F#, and looking at the result in Reflector so I can understand what’s happening. I certainly am not an F# expert by any stretch, but I do think I’m getting the hang of it.

Since I’ve reached this first plateau of getting it, I thought I’d write out some of the things I like and don’t like about the language. This is by no means an introduction to F#. I’d recommend Robert Pickering’sFoundations of F# book as well as Don’s Expert F# book (when it comes out). You should also be reading Luke Hoban‘s and Jomo Fisher‘s blogs – they both just joined the F# team. C# MVP Tomas Petricek has written several blog posts introducing F#, which he’s brought together in a single post. For an general overview of functional programming (the primary programming paradigm of F#), check out Slava Akhmechet’s Functional Programming For The Rest of Us.

This turned into a fairly long post, so I split it out into a series that I’ll post thru the end of the week.

Update: added link to Tomas Petricek’s F# Introduction

Fans4Writers

Most of my readers get DevHawk via the RSS feed, so I wanted to explicitly call out a new addition to my flair. If you’re not aware, the Writers Guild of America is on strike. When you buy a $20 DVD, the writer makes a measly 4 cents. When you watch an episode of your favorite show online, the writer makes nothing. This video explains the situation pretty well.

Even though I’m about as liberal as you can get, I’m not a big union guy. Neither of my parents were in a union. Neither Julie nor I are in a union. My only exposure to unions growing up was negative, typically when the Washington Opera (where my mother works) was negotiating with the musicians or stagehands. I vividly remember one musician’s strike where one of their demands was to increase the minimum call size to be bigger than the size of the orchestra pit. Needless to say, that seemed like an unreasonable demand to me (though to be fair, I only heard the management side of the story).

In the recent hockey labor dispute, I was firmly on the side of the owners since day one. And while the teacher’s union is one of the strongest bastions of democratic party support, I think the modern education system is fundamentally broken. So while I am a liberal, I’ve never been a big union guy.

However, I’m firmly with the writers union on this one. I spent several hours tonight reading a bunch of strike-related blogs, like United Hollywood. Obviously, they’re coverage of the strike is pro-writer biased, but it’s hard to argue with the idea of a fair wage for Internet delivered content. I particularly like this video which is a series of clips of media CEOs bragging about how much money their companies can make online. Yet – again, according to pro-writer blogs – they refuse to even negotiate paying the writers a percentage of the money they make for using the writer’s content that way.

And to add a Media 2.0 spin to all this, there was a fascinating post wondering if Google could/would “scoop up the entire entertainment industry“. I don’t think substituting Google for AMPTP (which includes among others “big media” like Disney, Fox and Viacom) would be a good idea – new boss, same old problems. However, the idea of bypassing the studios with direct Internet distribution is a good one. One has to wonder how much this strike will accelerate that trend. This strike seems much more risky for the distribution companies & networks than for the writers – even direct distributed content needs to be written, right?

Afternoon Coffee 126

  • In a surprise to exactly nobody, the Caps let coach Glen Hanlon go yesterday. I gotta say I feel for the guy. I mean, he had to go, but still. The Caps promoted the coach of their minor league team Bruce Boudreau. Makes sense – the farm team is where you develop players, why not coaches too? The team responded by beating the Flyers in overtime, though they did blow a 3 goal lead along the way.
  • It won’t get them back in the national title hunt, but thrashing ASU may earn USC a ticket to a BCS bowl, or the Rose Bowl if the Ducks can’t win without Dennis Dixon.
  • I finally finished Dead Rising today. A sequel has been rumored and hinted at, but not confirmed even though the ending left the door wide open. I really enjoyed it, so here’s hoping. I’m going to hold off on starting anything new until I get back from Canada, but it’ll probably be R6:Vegas. Don’t really have time between now and Christmas to finish Blue Dragon and it’s 3 DVDs.
  • In more “Screw Turkey Day, we’re shipping anyway” news, p&p shipped a new version of the Web Service Software Factory. This one’s called the “Modeling Edition”. I saw some of this stuff back in August, and I like what those p&p folks are doing. It’s worth a look, just to see how they’ve integrated DSL and GAT.
  • My old team shipped a new version of their S+S demo app LitwareHR. There’s also some tools for testing multi-tenant databases.
  • Quick reminder: I’m @ DevTeach Vancouver next week, so blogging will be light. I’ve got a series of thoughts on F# ready to post, but we’ll see when I get network access to post them. Given that I took a month off from blogging a short while back, I didn’t bother asking Dale to cover for me.

Morning Coffee 125

  • So I wasn’t quite as close to the end of Dead Rising as I thought I was. Those who’ve played the game thru will understand.
  • After their promising start, the Capitals lost yet again. At the 20 game point, they’re now 6-13-1 for a league-worst 13 points. I think we’re at the point where they need to fire Glen Hanlon. Nothing personal Glen, but it’s not getting done. The only problem is who you would replace him with? Bob Hartley? Uh, no thanks. I think most Caps fans want Dale Hunter, but I think he’s too involved with the London Knights – he’s co-owner, president and head coach. But if we could get Dale, I’m guessing Glen would be gone in a heartbeat.
  • The XNA team blog announced that XNA Game Studio 2.0′s beta has released. The download is available from Creators Club Online. The big new feature in this release is network support, and they’ve shipped a new starter kit to get you started.
  • In addition to shipping VS08 & .NET FX 3.5, a new CTP of SQL 2008 shipped yesterday. I couldn’t find a good overview of what’s new, but the SQL Express team has a post on what’s new in just their corner of this release. (via Jesus Rodriguez)
  • In more “I know it’s Thanksgiving week, but we’re shipping anyway” news, the Ruby.NET folks have shipped v0.9 – the first release since transferring control to the community. Does it run Rails? Not yet, but apparently they’re “close to getting Ruby on Rails to run successfully”. One thing that caught my eye is that it includes VS integration. Nice.