51 points · by gspyrou · 16y ago · 19 comments
Edit: Here's the link to the original post: http://news.ycombinator.com/item?id=1109754
The benefit comes from that many "small" things you do in C# become much nicer in F#:
C#:
int target;
int someValue;
if (dict.TryGetValue("key", out someValue)) {
target = someValue * 100;
} else {
target = 42; // default
}
F#: let target = match dict.TryGetValue "key" with
| true, x -> x * 100
| _ -> 42 // default
Not only is the code more concise in F#, we get another benefit too. The temporary value x, needed because of the use of out params in C#, no longer exists in the F# version. In C#, this temp val escapes its needed scope - it should only be needed in the branch where it's used, but instead it's now a normal local.A more large scale win is any async code. In F#, you can use the async workflow (monad), and with 1 character (!), you make the compiler emit code that'd take at least 10 extra lines in C#. Inside an async workflow, a let! (example: let! foo = bar.AsyncGetResponse()) is like calling bar.BeginGetResponse, bar.EndGetResponse, while preserving the current exception handlers and all locals. It makes heavily async code trivial to write, where in C#, it's near impossible.
If it was on the JVM, looks like a serious competitor to Scala with some distinct advantages (! operator for sure) along with a little simpler syntax.
* Inbuilt support for sequence head and tail - very important for recursive functions.
* Due to the heavily object-oriented nature of C#, type inference is really limited. This makes writing anything non-trivial in functional style rather tedious as your type declarations will become several lines long really quickly. For example, try writing a Y Combinator in C# and F# to see the difference.
* Parallel processing is a lot easier in F#.
* Matching in F# is a lot more powerful than switch-case in C#, which makes functional programming easier.
My point is: functional programming support in C# is good enough for simple things but serious functional programming is a lot easier in F#. On the other hand, OO is a bit cumbersome in F#. I wouldn't use F# for typical web or enterprise create-update-delete database stuff but it is really great for things where functional programming is usually strong at, especially if you need it to work together with C# code.
There's nothing in functional programming that prevents it from using in "typical web" apps though, as demonstrated by the engine that runs HN, news.arc, for example.