1

I know there are lots of external packages on linear algebra but my question is really when do I use f# and when c#? I make a simple example and while I did this I realized maybe it is too simple. but lets say I want to do element by element division of two arrays:

Imperative c#:

   static double[] elementdivideimp (double[] arr1, double[] arr2)
    {
        var res = new double[arr1.Length];
        for (int i = 0; i < arr1.Length; i++)
        {
            res[i] = arr2[i]/arr1[i];
        }
        return res;
    }

LINQ c#:

static double[] elementdivideL(double[] arr1, double[] arr2)
    {
        return arr1.Zip(arr2, (a, b) => b/a).ToArray();
    }

f#:

let elementdividefunc a b = Array.map2 (fun i j -> (j / i)) a b 

As said maybe this is too simple but I m really struggling to decide which language to go for when I face a programming challenge. SO when do I use which?

nik
  • 1,672
  • 2
  • 17
  • 36
  • I can't think of anything in linear algebra that would really make me lean significantly in one direction or the other. The main difference between C# and F# is difference in outlook (object-oriented vs. functional). But most math can be done in either pretty easily. – sircodesalot Oct 07 '13 at 19:07
  • 3
    Asking which language to use is like asking which glass to drink out of. It's a preference generally. But keep this in mind - when you're a hammer, everything looks like a nail. But generally speaking, make sure you use the right tool for the job. If `F#` is that tool - fine. – Mike Perrenoud Oct 07 '13 at 19:08
  • 6
    If you want to make C# look even worse, you can write just `Array.map2 (/) a b` :-) – Tomas Petricek Oct 07 '13 at 19:13
  • I know what you think I should be using...:) – nik Oct 07 '13 at 19:15

1 Answers1

4

As already mentioned in the comments, use the right tool for the job.

Now, the question is, when is F# the right tool for the job. To get a useful answer, it is important to look at the problem from the business perspective - what business problems are you facing and can F# (or any other language) help you solve them?

I think the talk Succeeding with functional-first languages in the industry by Don Syme looks at this problem from the right perspective. It looks at the specific task of developing analytical components and explains what problems people usually face in this domain (correctness, complexity, efficiency and time-to-market) and how better languages can help you overcome those (and many of the points are based on the evidence collected by the F# foundation and numerous earlier SO questions #1 #2).

I think you will not get a clear answer just by comparing fairly simple snippets of code, but your example demonstrates that:

  • F# code is generally more concise which likely reduces time-to-market
  • Compared with imperative C#, you do not need to handle corner cases, which aids correctness
  • You can easily use data structures like arrays, so your code is more efficient (I have not tested this, but I think the C# code uses IEnumerable<T> and will actually be slower in this case).

If you look at other business areas, then you may find different problems and then you can base your evaluation on those. But for analytical components (computations), I think there is a good evidence for languages like F#.

Community
  • 1
  • 1
Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
  • 1
    I've noticed that F# really excels with workflows. C# can emulate this with LINQ, but functional programming lends itself to data-in-data-out. – Dustin Kingen Oct 07 '13 at 19:28
  • thanks Tomas. I will check the links you provided. I work in finance and not as programmer, which means I dont really have time to be professional coder, I just code to make my life easier, either in the area of financial modelling or data juggling of some sort. So I m still trying to figure out which language to learn further, I spent some time on both c# and f# - hence my question – nik Oct 07 '13 at 19:32
  • 2
    I have immense respect for Tomas Petricek and everyone else in the F# community, but I feel a lot of people here are being politically correct. So I'll be the ogre in the group and declare that the correct answer in this case is quite obviously F#. For someone with equivalent skills in both F# and C#, the former is nearly always the better choice. F# is less noisy syntactically, allows for better localization of logic, and offers the goodness of pattern matching. The only time C# would be the better choice is when you are working in a group in which the other members don't know F#. – Shredderroy Oct 07 '13 at 21:20