Overview: Introducing F# and Functional Programming
Applies to: Functional Programming
Authors: Tomas Petricek and Jon Skeet
Get this book in Print, PDF, ePub and Kindle at manning.com. Use code “MSDN37b” to save 37%.
Summary: This overview discusses different programming styles that are used in F# and then explains functional programming in more detail. It also provides links to more information about practical benefits of functional programming and its core concepts.
This topic contains the following sections.
- Programming Styles in F#
- What is Functional Programming?
- Summary
- Additional Resources
- See Also
This article is an excerpt from Real World Functional Programming: With Examples in F# and C# by Tomas Petricek with Jon Skeet from Manning Publications (ISBN 9781933988924, copyright Manning Publications 2009, all rights reserved). No part of these chapters may be reproduced, stored in a retrieval system, or transmitted in any form or by any means—electronic, electrostatic, mechanical, photocopying, recording, or otherwise—without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews.
Programming Styles in F#
F# is a programming language that combines the succinct and expressive functional programming style with the robustness of the .NET Framework. Applications developed in F# typically combine multiple styles of programming:
Functional programming is used for writing the core components of an application. It is used to elegantly implement main algorithms, the processing of data, or other key tasks. Code written in the functional style is often safer and easier to reason about. For example, functional languages avoid using the null value. It can also elegantly model the problem domain and solve problems in that domain.
Object-oriented programming is used for two purposes. First, object-oriented concepts provide a great way for organizing larger program components. In a typical F# application, the functional style is used to implement core components and the components are encapsulated by using objects. Second, the object-oriented programming support in F# is needed for smooth integration with .NET. It is used when calling .NET APIs and when creating F# components accessible from other .NET languages.
Concurrent, parallel, and reactive programming are increasingly important paradigms. F# doesn’t have a direct language support for any of them, but the functional nature of the language facilitates the writing of concurrent, parallel, and reactive programs. Using asynchronous workflows, it is possible to elegantly write communication in a client/server application or parallelize a computation.
Imperative programming—As discussed later, functional programming prefers the use of immutable (read-only) values, because it makes understanding programs easier and also helps in concurrency. F# allows the use of imperative style of programming as well. This is mainly useful for optimization of performance-critical parts of an application and for certain .NET interoperability scenarios.
This overview focuses on functional programming. In many ways, the functional approach is different from the style that is familiar from languages like C#, Visual Basic, or C++. The difference is not just in the syntax but in the essential principles that the language builds on.
The most benefits from using F# come when writing code in a functional way. To some extent, this means that developers need to forget everything they know about programming and learn to think in terms of functional principles. This is not completely true because functional programming already influenced many technologies that you may already be using, including LINQ, SQL, XSLT and XAML.
F# as part of .NET Ecosystem
Thanks to the multi-paradigm nature of F#, the language is a first-class .NET citizen and you can use it to develop any .NET applications or libraries. Often, the best option is to use F# for developing a part of a system that can benefit the most from the functional approach. You can, for example, implement a calculation engine in F#, wrap it as a .NET object, and reference it from a C# application.
This approach also makes it easier to integrate F# within a larger organization. The F# part can be developed and maintained by a small number of F# programmers, so you can get the benefits of functional style without teaching F# to everyone in your organization. You can find more information about business aspects of F# as well as some case studies in a whitepaper F# in the Enterprise.
The articles in this section give you a brief overview of the key functional concepts and their benefits. Before looking at them, the following section clarifies what functional programming actually means.
What is Functional Programming?
Finding a precise definition of functional programming is difficult. Among the various functional languages, there’s no clear set of features that every functional language must have. Nonetheless, functional languages share common properties and support somewhat different styles of expressing solutions to programming problems. It’s easiest to describe functional programming by comparing it with the most common alternative option: imperative programming.
Functional programming is a style of programming that emphasizes the evaluation of expressions, rather than execution of [statements]. The expressions in these languages are formed by using functions to combine basic values.
Adapted from: http://www.cs.nott.ac.uk/~gmh/faq.html
The meaning of this, somewhat abstract, description will become clear after looking at the two approaches that the it compares. The “evaluation of expressions” in the first sentence represents the functional approach, compared with the “execution of statements” style of imperative code. Let’s take a look at these two options in detail:
**Execution of statements—**The program is expressed as a sequence of statements. The program specifies how to achieve the end result by creating objects and manipulating them. When using this approach, code typically works with objects that can be changed. The statements describe modifications that need to be performed step by step to achieve the desired result. For example, imagine an object is a cup of black coffee. The object can be modified to get the desired result, for example, by adding two sugars.
**Evaluation of expressions—**In the functional style, the program code is an expression that specifies the properties of the desired result. Code doesn't specify the steps necessary to construct the object and it can’t be accidentally used before it’s created. For example, you can ask for a cup of coffee with two packets of sugar. It is not possible to drink the coffee before the sugar is added because the desired object is a coffee with sugar. When the customer gets the cup, it already contains the sugar.
This may still sound like a subtle difference; yet, it leads to huge changes in the way you design code. The single axiom is that code is written as expressions instead of a sequence of statements, but this approach has many logical consequences. Components are encapsulated and composed differently, techniques for writing reusable code differ; code uses data structures that are more suitable for representing the result of a complex computation… You can find more information about these differences and their consequences in the additional resources below.
Summary
This overview discussed different programming paradigms that you can use in F# and gave a high-level overview of what functional programming is. Perhaps the most important thing to remember is that functional programming builds on very different principles. The key difference is that functional programs are written as expressions and are evaluated to get the result of the expression. This is quite different from imperative programs, which are written as a sequence of statements that execute step by step.
Additional Resources
Why should you invest your time into learning a new programming paradigm? For some, the first reason is that knowing functional programming makes you a better programmer, even if you continue using C# in your daily job. However, in many domains, functional programming makes you and your team more productive. The following three articles present some of the real-world advantages of the functional approach:
This brief overview explained that functional programming is based on “evaluating expressions.” This defines the entire programming style and the key functional language features stem from this single concept. The following articles explain the most important features without going into any technical details about F# itself:
To download the code snippets shown in this article, go to https://code.msdn.microsoft.com/Chapter-1-Introducing-F-c967460d
See Also
This article is based on Real World Functional Programming: With Examples in F# and C#. Book chapters related to the content of this article are:
Book Chapter 1: “Thinking differently” contains a detailed explanation of the difference between the imperative and the functional programming concepts and explains the benefits of the functional approach.
Book Chapter 2: “Core concepts in functional programming” explains concepts that follow from the difference between "the execution of statements" and "evaluation expressions".
The following MSDN documents are related to the topic of this article:
Visual F# is an overview of the F# language and contains a list of functional features with links to more detailed information.
F# in the Enterprise: Accelerated Analytical and Parallel .NET Development with F# 2.0 includes numerous case studies of using F# and discusses the typical F# use cases.
Introduction to Functional Programming in F# includes a walkthrough showing basic F# concepts and an article discussing first-class functions, which is a way of writing reusable expressions.
Previous article: Introducing Functional Programming
Next article: Overview: Benefiting from Declarative Style