In a short rant, Roger declares that he doesn’t “get” F#. There is no business case, no ROI, what can’t I do in C# that is in F#. Etc.
I think that Microsoft are trying to sell F# to us as something new and awesome, but I’m having serious problems seeing the benefits over C#.
Roger wants us to prove to him that we actually all need F#. Well Roger, you are right, you probably don’t need F#.
It can be hard to see the benefits over C#. If you aren’t excited about new ways to tackle the concurrency problem, or new approaches to handling generic and mathematical problems, and if the thought of breaking out the shiny new lexer and parser don’t give you a tingle, then maybe F# isn’t actually marketed at you at all. Perhaps, just maybe, its for a group of programmers that are gagging to get their hands on a managed language to break new ground in their medical, pharmaceutical, mathematical and scientific fields – on Windows.
Heres a newsflash, not everybody is writing e-commerce applications.
The thing is, people who earn a living in these alternative problem spaces typically use functional languages like Erlang, OCaml, Clojure, Scheme, Common Lisp and so on. The market (apparently) is there, and while some of them still love to use vim, many would like a decent IDE – and at the moment, that IDE is called Eclipse. What Microsoft is aiming for is to take a chunk out of the almost exclusive monopoly that the Java platform has on this segment of the developer population. We don’t have to get it, any more than they don’t have to get Visual Basic.
But back to Rogers rant, apparently, this is how these guys and gals should do currying, something they are used to in most functional languages:
string Foo(int a,bool b)
{
//do stuff
}
void UseCurry()
{
Func curriedFooWithTrue = a => Foo(a,true);
//invoke curried function.
var res = curriedFooWithTrue(123);
}
This apparently, is pipelining:
var listOfInts = new List {1,2,3,4,5,6};
Func<int,int> squared = x => x*x;
var squaredListOfInts = listOfInts.Select(x => squared).ToList();
While sure, C# can do that – why would you? As soon as we want to use pipelining and currying to increase the expressiveness of our code, not just to use it for the sake of using it, we can start to write some fantastic internal DSLs.
let should test actual =
test actual
let equal expected actual =
if expected <> actual then
failwith "Expected values to be equal"
//usage
let sayHello name = "Hello " + name
sayHello "Roger" |> should equal "Hello Roger"
Note that is a generic solution. The F# compiler also figured out that the generic function equal, will only work if its arguments are actually equatable. The generic type inference, complete with generic constraints far more powerful than in C# is just sublime. Pipelining works in two directions, not just left to right.
So to continue, apparently tuples are like meh..
…but F# uses them for things like grouping:
type Person =
{
Name: string;
Address: string;
PostCode: string;
}
let testData =
Seq.init 10 (fun i ->
{
Name = "Name" + i.ToString();
Address = "Address" + i.ToString();
PostCode = "000" + (i % 2).ToString();
})
let frequencyByPostCode =
testData
|> Seq.groupBy (fun person -> person.PostCode) //seq<Tuple<string, Person>>>
|> Seq.map (fun group ->
match group with
| (pcode, people) -> printfn "%s - %d" pcode (people.Count())
)
And before we start to moan that we could just use linq, this functionality existed before use of linq was actually possible.
So lets pretend for a moment, that if we were all sitting at PDC 2012 when the next release of C# comes out and there is the feature list in all its glory:
- list comprehension
- language keywords to support asynchronous programming
- extension everything
- less curly braces, angle brackets and semi colons (my favourite)
- Generic type inference
- operators to support the generation and substitution of Expressions
- first class functions and events
- operator creation
- pattern matching
- Active patterns
- Computation expressions
I’m sure the blogosphere would be a light saying things like, “wow!”, “cool!” and “look at all the new edge case programming problems we can solve in under two lines of code!”. Some of us would like, do we need all that? Just like when Linq and Lambdas came to town.
To sum up, yes there is a business case for F#, just not in the classic sense. Will you write your next LOB app in it? That would probably be masochistic, and I would recommend against it. Will it make you money? Jury’s still out, there are successes that indicate it can be done.
[...] Jim Burger’s Maybe F# isn’t for you… [...]
Don’t forget ‘units of measure’ (http://blogs.msdn.com/andrewkennedy/archive/2008/08/29/units-of-measure-in-f-part-one-introducing-units.aspx)
Thanks @Nick, a glaring omission indeed! Units of measure are certainly one of the features that is not available in other mainstream .NET languages right now.
For those who haven’t read Andrew’s series on units of measure, it stands out as a ‘must read’ for applying F# in the real world.
[...] important, others are more involved. Jim also looks at the ROI and business case for F# in his post Maybe F# isn’t for you… concluding that for Line of Business / Ecommerce perhaps its not, but there are plenty of other [...]
I’m curious about “Will you write your next LOB app in it? That would probably be masochistic, and I would recommend against it.” — what aspects about F# do you find problematic for “LOB” apps?
Is it just the lack of designers/code generators?
First, let me qualify that I recognize that everyone has different thresholds of pain on this front. For me the productivity hit is not so much the lack of designers, but I do like to practice TDD, and I’m constantly refactoring. Refactoring tools would be my first desire. From a language perspective I’m finding that consuming things like moq & nhibernate messy. The expression discrepancy hurts a little. I also find guys struggle to fit immutability into their Ideas on architecture, which can make selling the idea hard in the first place.
Last, it can be hard to find other F# programmers locally so projects typically need to spend money training or attracting the right people.
Again, these are pain points – not blockages to writing a shopfront web site in f#.
I’m also pretty confident that this will steadily improve. It won’t be long before the tool market descends upon F# -it’s anybodies game to win right now.