views:

738

answers:

9

How difficult is it to learn F# for experienced C# 3.0 developers, and/or what would you say is the most difficult part of learning F#?

A: 

The biggest leap is going to be the following fact:

C# is an Object Oriented Programming Language

F# is a Function Programming Language

Developers that aren't familiar with Functional Programming are going to need to catch up on what the differences are and how to transition between the two. The nice thing is that F# is part of the .NET framework...so many of the familiar libraries are going to be there.

Justin Niessner
This doesn't answer the question at all! It restates it.
rp
It's more of a functional vs imperative thing. An interesting thing about F# is that it's sorta mixed. You can program in F# with an imperative style.
YotaXP
+14  A: 

Starting out in F# (learning the syntax, scraping the surface of how to program in a functional manner) is not too hard. A good C# 3 developer familiar with LINQ and with a solid understanding of Lambda expressions should be able to pick up the basics quickly.

It is hard to say how hard it would be for them to break out the habit of object oriented problem solving, but problems that call for functional solutions should force them to make that leap. It is difficult to change your thinking unless you are presented with a new problem in my opinion.

All in all I would say it took me a week to learn the syntax and basics of the language in my spare time (2-3 hours a night). You'll need more time than that to get a real feel for functional programming though.

Issues

I am still struggling with types. I have been doing Project Euler problems to teach myself the syntax, so I haven't needed to any kind of complex user defined types so far.

The type inference the compiler does takes a little getting used to as well, as it is not always clear when it needs types to be specified, and when it can just work them out.

Learning

I would definitely suggest trying Project Euler problems for starters. Mathematical problems are always a good candidate for functional programming.

On a more general note, data processing problems are good too. List processing is very easy in functional languages, and is one of the first things you should learn IMO.

Books

Real-world Functional Programming: I finished this book a little while ago, and found it really good for working through how to apply functional programming principals to problems using both C# and F#. Most of the examples in the book are done in both languages, using functional programming idioms specific to each language and explaining the difference between them.

Resources

Jamie Penney
Nice - was there any particular concept that you remember having a difficult time groking?
Marcel Lamothe
I'd like to break out of the object oriented habit, too... can you give an example of a problem that lends itself to functional programming please?
Neil Williams
@Marcel Lamothe: Functional Languages are good friends of Mathematics. Some books will use terms like Higher Order Functions, closures, interpolation etc...
Perpetualcoder
@Neil Williams: Have you seen this blog? http://codemiscellany.blogspot.com/search/label/f%23
Perpetualcoder
Are types in F# radically different than in C#?
Marcel Lamothe
Actually, I would suggest not doing Project Euler problems. Functional languages are the poster child for those problems with 1-2 line solutions. I find that it is much harder to do the regular stuff.
Unknown
I haven't had to use them yet, but I don't quite grasp how they fit into the immutable data scheme of things. Some else is probably better able to answer this question.
Jamie Penney
I found Project Euler when I was first learning F#, and I am having fun figuring these problems out using it. Doing these problems will give you much experience in using the various pipe-able functions. However there is quite a bit to F# that these problems will not exercise.
YotaXP
@Perpetualcoder: Hadn't seen that, thanks.
Neil Williams
+2  A: 

You might learn the F# language pretty fast but it will definitely take some to time to use F# the way it should be used. It could be similar to your jump from C to C++ with the addition of a syntax foreign to the eyes and mindset foreign to the brain.

Perpetualcoder
+3  A: 

You can learn F# quickly, but the question is how you will program in it.

F# will let you write procedural code like C#, but it is more cumbersome, and you would be missing the point of a functional language.

Drawing from my own experience, you will have to throw away your trusty loops in exchange for list comprehensions. You will also have to learn to use nested functions, and recursion.

There are also some pitfalls to be wary of with pattern matching.

Unknown
+2  A: 

I'd say before delving into F# dip your toes in a bit of Lisp or Haskell first, or even both. After having a bit of prior experience in Lisp and Haskell, and using C# religiously for a long time, I felt right at home with F#. Then again I also have a bit of experience in OCaml (one of the main influences behind F#) so that helps a fair amount. Functional languages are an entirely different way of looking at things with the Mathematical & Science approach. Add in the power of .NET libraries available in C# that you have prior experience with and you're golden.

John T
Heh, but then I have to learn Lisp/Haskell and F# - Why not just learn F# =)
Marcel Lamothe
Not learn them, just look at a few pages of some tutorials and see how it works. You'll thank me later.
John T
+13  A: 

For me, one of the more difficult parts was really learning algebraic data types (discriminated unions and tuples) and pattern matching, since I had really not deeply encountered these concepts in other languages.

As for getting up to speed, here is my favorite online content for learning F#. Since you're coming from C#, you may find 'what does this C# code look like in F#' useful.

Brian
Thanks for actually specifying the most difficult parts for you!
Marcel Lamothe
Is pattern matching in F# much different than, say, regular expressions, or XPath?
Marcel Lamothe
For my own reference, discriminated unions are similar to unions in C++ or Variants in VB, except they are strongly typed.
Marcel Lamothe
And to answer my own questions about pattern matching - the construct is similar to a C# switch statement, but with the ability to switch on expressions, not just constants. Very powerful!
Marcel Lamothe
@Marcel - The `match` expression is similar to a switch statement, as you say, but it's not the only place you can use pattern matching. `let x,y = someTuple` uses pattern matching to break a tuple into its parts during assignment, for example.
Joel Mueller
+4  A: 

As stated in other answers, the question isn't so much C# vs F#, as Object-Oriented/Procedural vs Functional.

Having said that, if you've dabbled in Linq, lambdas, anonymous delegates, you've already been doing some functional and/or functional-inspired stuff, so the first hurdle of 'functions as variables' is behind you.

There is a book in the pipes (Functional Programming in the Real World)* which I'm very much looking forward to, as it explains functional programming with C# and F# examples. I thoroughly recommend this webcast, by the author, which I think is an excellent taster. He walks you through an example in procedural C#, then transforms it into functional C#, then hops over to do the same in procedural F#, then functional F#. Very nice.

The hardest thing for me is deciphering the cryptic type inference error messages, though apparently they're working on improving that.

Side note, there are quite a lot of F# questions on Stack overflow, but you can't find them by searching for "searchword f#", you have to search for "searchword [f#]"

*Of which the co-author/editor is rather well-known to the community ;)

Benjol
Thanks for tip on searching for F# - I was wondering about that!
Marcel Lamothe
Yeah, this is a longstanding gripe of mine, but no-one's listening :( http://stackoverflow.uservoice.com/pages/1722-general/suggestions/20840-bug-search-for-f-fails
Benjol
A: 

If you want to learn F#, you need to be familar with Reflector or at least reading IL code.

F# does a lot of strange things that on the surface just don't make sense. Examples:

  • You have three kinds of null. CLR null, Nullable, and Option.
  • They can be combined. If you see Option you need to check for both None and Some(null) before using the string.
  • You can overload most operators like addition (+), concatenate (^), and greater than (>).
  • Other languages will recognize these overloads in your libraries.
  • You canot use an overloaded concatenate operator from F#.
  • If you use the > operator, it doesn't honor your overload. Instead it looks for the IComparable interface.
  • The compiler won't tell you that using > without implementing IComparable will be a runtime error.

In short, F# is an incredibly inconsistent language and you can't truly understand it without digging into the code it produces.

Jonathan Allen
Please, nobody wants to hear your rants here. Do it in your own blog if you want.
Mauricio Scheffer
So what's your suggestion? Just pretend these problems don't exist until someone trips over them and then laugh at the poor sob? "Ha ha, look at that fool. Hes too dumb to realize you can't overload > like you overload +"
Jonathan Allen
It is a bit of a rant, but it does somewhat answer my question, as in: there are some things that are very different than C# that aren't so much related to the functional stuff (tuples, pattern matching, etc.) but are just plain odd or inconsistent.
Marcel Lamothe
Using the ">" operator without implementing IComparable will not produce a runtime error for F# types because they provide their own structural comparison. Indeed, that is an important part of what makes F# so powerful.
Jon Harrop
+1  A: 

How difficult is it to learn F# for experienced C# 3.0 developers,

Really easy. You should be able to do serious work after 1 month of learning F#.

and/or what would you say is the most difficult part of learning F#?

Firstly, learning to use record and union types and pattern matching instead of classes. Secondly, learning to factor code more aggressively using first-class functions.

These are minor issues though. Pick up any decent book on F# and you'll grok them quickly enough. Then you can get on to the really juicy stuff line parallelism and asynchronous workflows and...

Jon Harrop