8

I have a book coming on F#, but at the moment I am pretty uninformed, so I thought I'd ask. From very little I know of F#, I am struggling to see what it gains me over C# bar a possible syntactic neatness. There seems nothing conceptually new, or that isn't do-able in piian C#

I did Forth way back when (20 years ago nearly!) and I already incorporate passing function delegates as parameters into methods (been doing that sort of thing forever, it seems).

Stylistically I am not keen on anonymous methods - is that going to be an issue?

Though I suppose syntactic neatness is not to be sniffed at :-)

kpollock
  • 3,899
  • 9
  • 42
  • 61

10 Answers10

13

(Caveat: This first bit is not really an answer to your question. And I am very biased, as a member of the F# team.) Having been using F# for nearly a year now, I find that whenever I have to write C# code it feels like walking through mud. There are so many curlies and semicolons, and oh-my-gosh-quit-making-me-write-all-the-darn-types! It just feels slow. I work with lots of C# code and so I still read and debug it almost daily, and it's fine for that (even better than F# for debugging; we still need to improve the F# debugger integration a bit), but I have found that writing C# code now feels like a chore; F# is just much more enjoyable to code.

As for actual answers to your questions, a lot of people say 'tuples', but I say 'meh' to that. Type inference, discriminated unions and pattern matching, a functional library, pipelining, and syntactic neatness (syntax matters!) are bigger winners to me. (And if you want to write async code today, F# blows the doors off everyone else.)

Brian
  • 117,631
  • 17
  • 236
  • 300
  • Yep, that pretty much sums the feeling up. Coding in C# is just so _frustrating_, and for no benefit, either. If I saw that VS2010 screenshot correctly, F# has quite nice debugging in Dev10, right? – MichaelGG Jan 23 '09 at 19:51
  • I really like F# alot. But C#'s InitelliSense still feel's somehow superior, especially in VS2010, so that in the end it feels like I actually have to type _less_. Would be great if e.g. the pipeline operator (|>) triggered IntelliSense in a similar way that the .-operator does in C#. – Frank Apr 23 '10 at 14:53
  • @Novox: indeed, there are still a lot of improvements we could make to the VS tooling experience for F#; C# has a few years' head start, but we are catching up quickly :) – Brian Apr 23 '10 at 16:05
11

I like both F# and C#, but generally I prefer F#:

I would say if you try and to immutable programming in C# you soon run into the problem that you can't return more than one thing from a method/function. F# solves neatly using tuples to allow you to return more than one value.

Another problem with delegates in C# is that they are nominal. You can have two delegates with exactly the same signature, yet they are not compatible just because they have different names. You can use lambdas or anonymous delegates to work round this problem but F# solves in a cleaner way: it just check if the signatures match.

Union types are great and it's hard to see C# ever offering exactly this functionality.

Robert
  • 6,407
  • 2
  • 34
  • 41
  • ah yes, I can see where delegates with the same sig but different names still working would be handy. as regards returning multiple values, I tend to have ref params (an object e.g a list or yield return – kpollock Dec 22 '08 at 17:03
  • I guess I'll have to wait for the bookand bit of a play around to see for sure. – kpollock Dec 22 '08 at 17:05
  • You can return several things at once in C# -- defining your own tuples is quite easy. – Alexey Romanov Dec 23 '08 at 22:24
  • 1
    Ref params work but feel a little low level to me. Defining you're own tuples works too, but is still slightly more work (but will be better in .NET 4 when they are in the framework). Anything you can do in F# you can do in C#, F# just makes certain things easier. – Robert Dec 24 '08 at 09:09
  • 1
    The beauty of tuples in f# is that you can unpack them in the same way you pack them - I don't expect that will happen in c#. – Benjol Dec 30 '08 at 22:53
6

In addition to the other answers: Automatic generalization.

This gives F# a huge step up over C#, Scala, etc. Here's a trivial example, the "snd" function to get the second value from a pair. In F#:

  let snd (a,b) = b

The compiler automatically figures things out and makes it fully generic:

  val snd : 'a * 'b -> 'b

In C#:

static Tb snd<Ta, Tb>(Tuple<Ta, Tb> x) { return x.B; }

1/3 the code, 100% less noise. Now, extend that to more complex function types, say, something taking a tuple, and returning a dictionary of some enum to a function. Ouch.

And these are simple scenarios. Add in some generic constraints, relationships between type parameters, and well, it gets really difficult in C#. A few times with C#, I've had to really stop, think, and calculate which generic parameters I need, even if it's not a difficult application. In F#, I can code out the idea, and generally speaking things get generalized as much as possible with no further work from me. Lovely.

MichaelGG
  • 9,976
  • 1
  • 39
  • 82
5

Another big advantage is the full type inference system. local functions, lambdas, tuples and lists reduces code very much.

ChaosSpeeder
  • 3,468
  • 5
  • 30
  • 38
4

I think one of the big advantages of F# is the better support for tuples.

4

Yes, at this moment of time the advantages are "just" pattern matching, immutability by default, simpler (but less familiar) syntax, and pretty good type inference.

The two big ones for me (not counting type inference) are a much nicer monadic syntax (computation expressions in F# versus LINQ queries in C#) and quotations (vs. LINQ expressions).

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • The book has turned up now, so I have started reading. And here I only just cured myself of using 'let'.... :-). – kpollock Dec 24 '08 at 09:55
3

I must say the way they handle Async and parallelism in F# is impressive ... most impressive. Check out this video from PDC.

JP Alioto
  • 44,864
  • 6
  • 88
  • 112
2

I find it quite difficult to present the benefits in some simple way. My belief is that the benefits are not in some langauge features (be it immutablity by default, type inferrence or anything else mentioned here). The difference is really in a completely different development style that you can use when working with F#.

You can find some more information:

Hope this helps!

Community
  • 1
  • 1
Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
1

Functional programming is vastly different from object-oriented programming. However, since F# is an object-oriented functional language, and since C# is a functional object-oriented language, these two languages will seem to be pretty close to each other.

yfeldblum
  • 65,165
  • 12
  • 129
  • 169
0

There are things like discriminated unions, but I reached the exact same conclusion that you did.

And I'm not a big fan of the tuples - I like named properties, so I don't need to remember everything by position.

Things like immutabiliy is a false lead - since you can do that in C#, and hopefully C# 5 will make it easier too.

Re syntactic neatness... I find C# neater and easier to follow, bugt that might be familiarity.

There is a better switch syntax, but that can be introduced into C# - see here (including replies). Re anon-methods, nobody is forcing you to use them, but lambdas are a very tidy way of expressing intent in many cases.

Community
  • 1
  • 1
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900