10

Possible Duplicate:
What are the benefits of using C# vs F# or F# vs c#?

My team is currently using C# .NET to develop enterprise applications for our company. We have a history of Winforms dev, but are now moving over toward SilverLight.

My boss recently saw a video on F# and thought it looked pretty exciting, and he has asked me to check it out. My question is this - what advantages would a functional language (like F#) give over an OO language (like C#) in the realm of enterprise application development?

I really want to see if there are any compelling reasons to even begin contemplating a shift. Some F# and C# comparison code might be nice to see as well.

Community
  • 1
  • 1
Craig Schwarze
  • 11,367
  • 15
  • 60
  • 80
  • 4
    Your boss' recommendation isn't compelling enough to check it out? :) – John Weldon Feb 04 '10 at 22:01
  • 5
    One thing to keep in mind, depending on the size and experience of your team: Don't underestimate the initial productivity hit you're going to take switching from a language *and approach* the team knows to one they have to learn. It's not just learning the syntax, it's understanding it at a deep level, correctly applying it to overall structure, etc. Not saying it's not worth it (I know nothing about F#, actually), just that switching from class-based OOP to functional programming or prototype-based OOP (such as JavaScript) is a non-trivial effort which requires a non-trivial business case. – T.J. Crowder Feb 04 '10 at 22:08
  • 3
    @T.J. Crowder - seconded. The whole imperative to functional mindset shift shouldn't be underestimated. – Brian Agnew Feb 04 '10 at 22:09
  • 1
    @T.J. Crowder, and @Brian Agnew -- Agreed ++, but don't underestimate the long term benefits and improvements in how you think of problems after making that shift. – John Weldon Feb 04 '10 at 22:11
  • 1
    @John - yes. I'm trying to move from Java to Scala. There are definite benefits. It's just that my initial Scala code looks/reads like, well, Java :-) – Brian Agnew Feb 04 '10 at 22:12
  • 1
    Agreed with Crowder. I am starting on F#, and unless your team has previous experience with functional languages, expect a steep learning curve. – Mathias Feb 04 '10 at 22:12
  • 1
    @Brian Agnew -- sounds like my F# and C# :) – John Weldon Feb 04 '10 at 22:14
  • 1
    @Brian: I can't speak on Scala, but re: F# vs C#, it is somewhat possible to write F# code in C# style - but then there isn't much of a point in switching... The hard part is to accept that you have to rethink everything and embrace a functional approach. – Mathias Feb 04 '10 at 22:15
  • 2
    @Mathias - absolutely. That switch is the difficult part :-) Sorry. That's the point I was trying to make. – Brian Agnew Feb 04 '10 at 22:19

3 Answers3

10

You can express a lot of concepts much more concisely in a functional language like F#.

Functions are first class objects, and can be applied directly to collections to perform transformations/filtering much more efficiently.

Immutability is encouraged in functional languages, and this makes (for instance) multi-threaded code much more reliable (you know data structures aren't changing under you).

By being able to write multi-threaded code more reliably and more easily it's much easier to take advantage of multiple processors/cores (increasingly important now Moore's law doesn;t apply so much).

Note that you can use your existing C# objects within F#. As such, you may want to write certain parts of your code in F# and other parts in C#. You should be able to mix and match according to your requirements and the suitability of each approach.

Mark Bell
  • 28,985
  • 26
  • 118
  • 145
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • 5
    +1 Multithreading is trivial without side effects. After all, if you aren't writing state, there's no need to lock anything. – Joel Feb 04 '10 at 22:11
4

I just ordered the Manning 'Real World Functional Programming' which does a fantastic job of explaining all of the nooks and crannies of F#, functional programming, and especially how it compares to C#, and OOP

What I'm coming down to is that functional programming in general increases the 'rigor' of your programs, assuming you conform to the principles of side-effect free functions, and immutability.

Additionally, there seem to be benefits to thinking about code in a declaritive sense, rather than an imperative sense. In OOP I tend to think of the steps it takes to accomplish something, but in FP it seems like you're more concerned with how you want to apply functions to data to obtain results.

John Weldon
  • 39,849
  • 11
  • 94
  • 127
3

firstly you ask "functional language (like F#) give over an OO language (like C#) i" f# is both functional and OO. any thing you would write in c# can be directly translated to f#. F# is a multi paradigm language but the advantage comes from the functional part in my opinion. f# and other functional langauge are a clear winner when it comes to writing multi threaded applications due to the higher possibilities for the compiler to reason about what can safely be run in parallel.

When it comes to web development f# has a very cool tool kit that allows you to write all code (as in server side and client side) in the same language aka f#. the client side will be translated into JavaScript by the web toolkit.

Rune FS
  • 21,497
  • 7
  • 62
  • 96
  • 3
    While both modes of usage are supported, F# is a functional language with imperative features, and C# is an imperative language with functional features. It would be exceedingly tedious to put 'mutable' in each declaration. Also, imperative programming inhibits the ability of F# to do pattern matching and type inference. – Joel Feb 04 '10 at 22:35
  • @Joel well according to Don Syme one of the main architects of F# my statement about F# being multiparadigm is true. Im simply paraphrasing his book expert F# and your imcorrect on the mutable you don't need that for doing OO in F# only if you wish things to be mutable and lastly you're also wrong on the pattern matching. OO in F# _can_ be combined with pattern matching. One of the mail goals of F# was to combine the Worlds of OO and FP and btw type inference works even with non F# classes. – Rune FS Feb 05 '10 at 09:44