I've recently been learning F# for fun (I'm a VB.NET/C# dev), and I really like some of what it has to offer. Theoretically that is. But I'm having trouble thinking up of scenarios where I would choose to code in F# rather than in C#. Any ideas?
A few arguments for pure functional programming:
For a full treatment, see Why Functional Programming Matters and Why Why Functional Programming Matters Matters. |
|||
|
From here: Asynchronous servers
Metaprogramming (e.g. parsing)
Technical computing
GUI applications
Logic programming
Testing
Performance
|
||||
|
Here's what use functional style programming for -- on a more-or-less daily basis. We do lots of statistical and actuarial things with fairly large datasets. The data fetched from the database is -- essentially static, immutable objects. No reason to create a class with methods. Each stage of the calculation adds some additional details, but doesn't essentially mutate the object. At the "end" of the pipeline we're really doing a fancy reduce to compute sums and counts and other things. Imagine this.
Each "phase" of the calculation is a functional programming loop that does simple read-calculate-yield and creates a composite object of other things plus results. (We use Python, hence the functional programming using generator functions.) It's easier to use stateless, immutable objects. |
|||||||
|
Technically, it is not a unique property of a functional programming, and F# is not a pure functional language. F#, as one of ML descendants, provides an excellent pattern matching and algebraic data types. So, for any task which requires complex data structures F# is much more expressive and easy to use than C#. Imagine implementing a compiler in C# and F# - representing an abstract syntax tree and transforms over it is much simpler if your language provides ADTs and a pattern matching. |
|||
|
Ideal for map-reduce kind of massive multi-system and massive multi-core parallelism. Pretty cool, considering that nowadays entry level servers come with 48 cores (96 counting HT). |
|||
|
If you want fully functional try Haskell, Erlang also has some very cool stuff about it. Simon Payton-Jones said about Haskell, he wants to have a program that obviously has no bugs, rather than have no obvious bugs. (I probably got the quote a bit off, but you get the idea) By constraining side effects you make it much easier to prove your code is correct. |
|||
|
One definite advantage is that it's much more easily parallelised. |
|||||
|
F#
is not fully representative of functional programming. TryClojure
instead. – Job Feb 21 '11 at 22:14