5

F# Eye for the C# Guy

 9 months ago
source link: https://devm.io/csharp/f-sharp-c-sharp-intro
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Introduction – What is F#?

F# Eye for the C# Guy


When many developers talk about .NET these days, there is a tendency to think that .NET === C#. Yes, there’s VB.NET, of course, but if that’s still being actively used these days as a preferred programming language by anyone, I’m yet to see any evidence. For most developers, this is about it.

This attitude however, means missing out on one of the key cornerstones of the .NET family – F#. Despite having a fraction of C#’s popularity, F# is still – to this day – an important part of the .NET infrastructure. Would you believe that many features that find their way into C# start out as requests by the F# team, and by virtue of sharing a common Intermediate Language (IL) end up usable by C# developers as well.

In many ways, when you look at F#, you’re getting glimpses of the future of C#.

What is it, though? Many folks asked that question would answer that F# is a .NET Functional language. That’s actually not entirely true, and also requires a bit of explanation. What does it mean to be a functional language?

Well, let’s move back a step. When you’re designing an application you’ve got a series of choices to make. The first, naturally enough, is the language you’re going to develop in. Let’s say C# for the sake of argument. After that, you start to make choices about design patterns, things like repositories, inversion of control containers, etc. Stuff that’s beyond the scope of this article. There is another choice that you make too, whether you realise it or not – and that’s which programming paradigm to follow.

Ok, great. So, what’s a paradigm? It’s a style of programming. Object-Oriented Programming (OOP) is a paradigm. For many developers, it’s the only one they ever learn, but there are others. Many others. But only two that tend to be popular with modern developers – OOP and Functional Programming (FP).

I doubt that OOP needs much introduction, it’s the style of programming that most of us are taught when we first learn. OOP is an example of an imperative paradigm. Imperative programming is all about using loops and If statements to tell the virtual needle of the execution environment exactly which line to go to and when. OOP uses this approach to instantiate objects, which it then builds up and modifies as the program executes.

FP takes an entirely different approach. FP is from a family of paradigms called declarative. The most common declarative language you’re likely to have encountered is some flavour of SQL. Declarative languages aren’t concerned with the precise order of operations, they leave that to the execution environment. If you think about that with SQL, the lines you write aren’t executed in that order. Quite often the “SELECT” statement is the last thing executed. Declarative languages all take that approach – they are more concerned with describing what we want, and leaving the details to the execution environment. Imperative languages are far more concerned with how we obtain the result, often at the expense of code readability.

Both C# and F# are hybrid languages – that is, they support both OOP and FP to some extent. But crucially they support them both to different extents. C# was designed as an OOP language...


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK