I've been wanting to get started with F# for quite some time now. Last semester, I heard a lecture about Haskell and functional programming, and I've been fascinated by the whole paradigm ever since.
Coming from a C# background, I'm already deeply familiar with the .NET Framework and the entire technology stack, which is what made F# even more attractive to me. Here's how I approached learning the basics of F#.
Learning a functional programming language when you're used to classic object-oriented languages requires a shift in thinking. It's not like switching from Java to C#, which are quite similar; it's a different paradigm with different rules and best practices.
The syntax of F# is very different to C#, I'll admit that, but that's actually not the hard part. Learning a new syntax is easy. The hard part is to wrap your head around a new way of approaching and thinking about problems.
#Functional Programming Basics
Functional programming focuses on evaluating expressions rather than successively executing statements that change state. In fact, functional programming languages typically discourage the usage of mutable state, that is, state that changes. Instead, immutable data structures are preferred.
Because changing state is usually avoided, traditional loop structures, such as
while loops, are replaced by recursion. If reading the word recursion made you wince, don't worry – that feeling will go away quickly. Once you get used to thinking recursively, you'll notice that it can be a very elegant way to formulate solutions to typical problems.
#More Functional Programming Concepts
There are a variety of other concepts that are implemented in languages like F# and Haskell. I'd like to refer you to this excellent summary for a quick primer on those functional programming basics.
For a more in-depth explanation, I recommend you read the Wikipedia article.
Or better yet, get yourself a copy of Real-World Functional Programming and learn from Jon Skeet and Tomas Petricek, both masters of C# and F#, respectively.
#Learning the Basics of F#
Once you've completed your share of reading the theory behind functional programming, you should dive right into some F# code. The official website lists various tutorials that help you get started, highly recommended! Alternatively, you can work through the Wiki book F# programming.
#Problems from Project Euler
Like every skill, learning to write functional programs requires time and practice. This is where Project Euler comes into play: It gives you the opportunity to tackle a vast set of math problems using the language of your choice – and that'll be F#, of course!
Oh, did I mention there's gamification? Create an account to track your progress over time. To keep you motivated, you can see how many other members have solved a particular problem already. On top of that, you'll dust off your math skills. Sounds great, doesn't it?
The difficulty of the problems varies widely. Here's a simple one to get started:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. Multiples of 3 and 5, Project Euler
That doesn't look too scary, does it? Let's dial it up a notch with the next one:
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the sum of all numbers which are equal to the sum of the factorial of their digits. Digit Factorials, Project Euler
This one is a lot more interesting already and requires some thinking. The challenging factor is that most of the time, sheer brute-forcing the solution won't work so you'll have to actually reason about how you're going to tackle the problem. You'll be rewarded with concise and well thought out code.
#Checking Your Solution
If you think you got the right answer, enter it into the solution field and Project Euler will tell you whether it's correct. In case you're stuck, you shouldn't give up immediately. First try to solve the problem in a language you're totally familiar with and afterwards translate the solution into F#. You'll learn a lot along the way.
Now it's your turn: Head over to Project Euler and solve the first problem. Happy coding!