17

I'm a Java/C++ developer that never spent time in learning C# and the relative .NET foundation.

Then I learned OCaml because I had to use it for my master thesis so I heard about F# and wondered: will F# allow me to easily use the .NET API to build fully featured applications (that may involve GUIs, sockets, whatever) without any problem?

I ask this because it seems that F# syntax and concepts are quite similar to OCaml and when they are different it's just because a more OOP approach is used so Java knowledge would help me in filling that hole.. if F# is able to use the same resources as C# without the need to learn C# syntax I would really consider that.. is it worth trying?

Apart from that, are the books available on Amazon for F# (mainly one book from O'Reilly and 3-4 books from Apress) good to learn advanced techniques? Because now I'm quite fond of functional programming but never worked on .NET platform so I really don't know where to start from.

Thanks in advance

Outman
  • 3,100
  • 2
  • 15
  • 26
Jack
  • 131,802
  • 30
  • 241
  • 343
  • 5
    If you know Java already then picking up the C# syntax shouldn't be too difficult. See this post for info on GUI programming in F# http://stackoverflow.com/questions/2796272/how-to-learn-gui-programming-in-f – Longball27 Jul 15 '10 at 15:23
  • 1
    I wouldn't like to pick C# just because I discovered functional programming and I would like to deep into .NET by that point of view.. – Jack Jul 15 '10 at 15:33
  • I see and sympathise with your point, and I'm sure you'll like F# (which is actually based on OCaml, and even has an OCaml compatibility mode). But I'd just like to point out that C# has support for full closures and first-order functions, and it's possible to program in a very functional style even there. The only thing I miss when doing this, is type inference. – harms Jul 15 '10 at 15:35
  • Your knowledge of Java syntax should help in understanding C# examples of .NET library usage in places where F# examples are lacking in the MSDN documentation. And yes, graphical designer support aside, F# can do pretty much anything C# can do with the platform. – Joel Mueller Jul 15 '10 at 15:36
  • 9
    I disagree that the only thing missing from C# for functional programming is type inference. C# lacks a composable type system (i.e. delegates are nominally typed rather than structurally typed like F# function types), void is not a proper type in C# and C# makes a separation between statements and expression, this will bite you when programing in an FP style. Also, C# lacks records with copy and update semantics and union types, which are also important when functional programming. Tomas’ book does a good job of explaining why these things are important: http://www.manning.com/petricek/ – Robert Jul 15 '10 at 16:10
  • 2
    and, while not strictly FP, C#'s lack of pattern matching is a huge disappointment. – pblasucci Jul 15 '10 at 16:43
  • So I don't get it.. is F# integrated inside VS2008 with common GUI building tools or do I have to do it by hand (like I'm used to do with Swing)? Last time I tried something with VS it was MFC on VS6 (that had a simple visual GUI builder). Apart from that it has full support with .NET libraries right? – Jack Jul 15 '10 at 16:45

6 Answers6

13

If you know a few languages already like you say, Expert F# 2.0 by Don Syme is an excellent in depth view of F#.

Mostly you'll struggle with learning the libraries, but the MSDN reference is pretty good.

I think it's most definitely worth trying. Knowing C# won't give you any great advantages in learning F#, apart from the experience with the framework.

Mau
  • 14,234
  • 2
  • 31
  • 52
7

will F# allows me to use easily the .NET API to build fully featured applications (that may involve GUIs, sockets, whatever) without any problem?

I expect to see a few different perspectives here; I'll offer mine.

There are a few aspects to a 'language' that affect how easily/smoothly you can build 'fully-featured' applications with it:

  • the language itself
  • the library/framework it is paired with
  • the tooling capabilities (IDE integration)
  • outside support (samples, 3rd-party libraries, community)

F# is a terrific language, whose core is based on OCaml. It shares the same library/framework (.NET) as C#, and so with regards to the first two bullets, F# is as capable as C# for building such apps.

F# is a number of years younger than C# though, so both the Visual Studio tooling and the community/samples are not as mature as that of C#. Right now, there are fewer 'project templates' and 'designers' that work with F# in the VS box, and it is harder to find samples/libraries. The F# community is great, and is rapidly starting to fill in the holes of the 4th bullet (and even some of the third), but for building apps that would rely heavily on the 3rd and 4th bullets above, C# still has an advantage today (July 2010). Yes, you can build anything you like today with F#, but there are still cases, wher the end-to-end experience with C# will be smoother/easier.

Brian
  • 117,631
  • 17
  • 236
  • 300
5

In my experience, you need some C# to learn F#, especially for the .Net part.

Similar to your situation, I didn't know any .Net and C# before learning F#. The first hard part for me is asynchronous IO programming. I get a full understanding of it until I read the async chapter of CLR via C#. And I soon found that what bited me was not computation expression/monads, it is .NET. Then I continue to read CLR via C#, e.g. I know that in .Net 1d array is efficient, however 2d array is not that efficient. I also get to know delegates and events in .Net.

.Net is imperative. C# is the mother tone for .Net. IMHO, learning C# (at least reading C#) is required for an F# programmer.

Yin Zhu
  • 16,980
  • 13
  • 75
  • 117
3

One thing still missing from F# in Visual Studio are the tools which help automate the user interface design process. These are quite sophisticated in the case of C#, but in F# you'll have to do all the plumbing by hand. So if your goal is to learn to produce WPF (etc.) applications, C# is currently a better bet. If your goal is to learn the mechanical details of .NET, WPF (etc.), either language will do.

An even better approach, in my opinion, is to learn the two -- F# and C# -- in tandem. Your experience with Java and OCaml gives you a good head start on both C# and F#. Use the C# to take care of the UI, and the F# to do the underlying work.

TechNeilogy
  • 1,271
  • 7
  • 13
  • You mean that it's possible to easily mix up C# and F# and let C# care about GUI (for example) while F# is used for the program logic? They can interface one with the other seamlessly? – Jack Jul 15 '10 at 17:33
  • @Jack "seamlessly" might be a little too strong a word. You will need to keep your F# in a separate assembly (like a Java package) from your C# and GUI. You will also probably face a few minor challenges where the two interface with each other. – Mike Schenk Jul 15 '10 at 20:32
  • There is some F# functionality which is specifically designed to work with that language, and is difficult to use from C#, but F# is a full-fledged .NET language. As such, it is fairly simple to write classes and functions which look no different than referenced functions from any other .NET language, whether C#, MC++, or VB. The portable parts of the functionality are fairly simple to learn and keep straight. An F# book or online F# examples will provide example of intermixing F# and C#. Maybe not perfectly seamless, but at least painlessly seamless, lol. – TechNeilogy Jul 15 '10 at 21:22
  • +1 Agreed 1. Microsoft actually recommends C# (rather than F#) for web and desktop front ends, because of the designer support. 2. Whenever you search the web for solutions to particular .Net problems, you are likely to get a C# solution. It usually easier to just cut and paste this, than translate to F# – Stephen Hosking Jul 22 '10 at 23:13
1

The simple answer is: "yes", F# has full access to all .NET libraries and can both consume and produce code written in or for any other .NET language, as well as direct interfacing with COM and native DLL's.

That said, the tooling support for F# is not as mature as that for C#. While you might find it a bit easier to learn .NET using C#, you won't be inherently limited by starting with F#.

Most significantly, the GUI builder cannot presently produce F# code, one cannot produce strongly-typed ASP.NET codebehind pages (but can create weakly typed ones), and there is no direct support for creating tests in F# (but plugins exist which do).

However, one can easily write GUI code by hand in F#, and it is often easier to do so and quite a bit more terse than equivalent C#. For example, properties can be set on the constructor; e.g., let f = new Form(Text="Window Title") and lambda functions are converted to delegates automatically; e.g., event.Add( fun e -> doSomething() ).

It is also possible to, for example, create a DSL for describing the UI. For a bit more information, see F#: is there no UI (like WPF) for it?

A recommended style, as pointed out by TechNeilogy, is to write the logic in F# as a library, and call that from a GUI created in C#.

Community
  • 1
  • 1
James Hugard
  • 3,232
  • 1
  • 25
  • 36
  • Oh, and F#'s support for writing asynchronous and parallel code is simply wonderful. It is *far* easier to write correct non-blocking IO in F# than in any other language I've worked with. – James Hugard Jul 16 '10 at 15:12
  • One exception to the "interface with anything" is that there is currently no language-level support for dynamic objects created in, say, IronPython or IronRuby. While it may be possible to interface with such objects, this won't be as easy as, say, using VB or C#. – James Hugard Jul 16 '10 at 15:18
1

I will say "No". The challenging of learning F# is to change your mindset of programming, recursion, high order function, pattern matching, etc. Very importantly, you need to think to program in a "functional" way, not imperative as C# or Java. Learning API is always easy, but learning how to design application is not.

That is my 2 cents.

Liang Wu
  • 1,772
  • 3
  • 18
  • 26
  • 1
    Just to point it out, the OP already knows OCaml, which is a sister language to F#, so writing code in largely compatible F# should not pose any challenge. – James Hugard Jul 17 '10 at 15:41