1. Introduction
F# is an open-source, cross-platform, functional-first programming language that empowers all levels of programmers to write simple, robust code for a huge range of practical purposes. F# combines the succinctness, expressivity, efficiency, and robustness of functional programmingprogramming based on transformations of datawith the runtime support, libraries, interoperability, tools, and object models of modern programming frameworks. F# is the safe choice for modern programming, embracing both enterprise quality through tools for a wide range of editors and operating systems, and openness, allowing you to contribute to the language and its ecosystem.
This book will help you become an expert in using F# for a broad range of practical problems. F# is a functional-first language, where functional programming is the first option used for solving most programming problems. However, F# differs from many functional languages in that it is both highly interoperable and embraces imperative and object-oriented (OO) programming where necessary. It also provides the benefits of both compiled and dynamic languages, allowing both the rapid iteration typical of dynamic languages and the performance and robustness of a strongly-typed compiled language.
Functional programming has long inspired students and programmers alike with its simplicity and expressive power. Applied functional programming is booming. A new generation of functional languages is reaching maturity; some functional language constructs have been integrated into languages such as C#, Java, and Python, and there is now widespread expertise available in the pragmatic application of functional programming techniques. There is also strong evidence that functional programming offers significant productivity gains in important application areas such as data access, ETL (Extract, Transform, Load) pipelines, enterprise architecture, financial modeling, machine learning, and analysis tools. More recently, functional programming is part of the rise of declarative programming models, especially for data queries, concurrent programming, parallel programming, and reactive programming.
The F# designers have adopted a design philosophy that allows you to take the best and most productive aspects of these paradigms and combine them while still placing primary emphasis on simplicity. This book will help you understand the power that F# offers through this combination. The designer of the F# language, Don Syme, is one of the authors of this book. This book benefits from his authority on F# and from all the authors years of experience with F# and other programming languages.
F# benefits from a supportive and energetic worldwide community that is happy and willing to help you in your journey. You can find them everywhere: on StackOverflow (tag fsharp), Twitter (#fsharp), LinkedIn, GitHub, Slack, Facebook, and more. Most of all, you can find them via fsharp.orgthe home of the F# Software Foundation, the non-profit foundation whose mission is to promote and protect the F# language and that acts as a strong voice for the language worldwide. The F# community contributes to an ecosystem of free open-source components and tools, including important core tools such as FSharp.Data, that bridge to a host of other programming libraries and services. F# also combines the best of openness and enterprise support: it is an open-source, cross-platform language under an OSS-approved license, and companies including Microsoft and Xamarin provide supported tooling for F# through tools such as Visual Studio and Xamarin Studio. Combined with its technical features, this makes F# an exciting and safe choice for mainstream professional programming activities.
F# offers an approach to computing that will continue to surprise and delight, and mastering F# programming techniques will help you become a better and happier programmer regardless of the language you use. There has been no better time to dive into the beautiful and productive world of F# programming, and F# offers the best route to learn and apply modern programming techniques so as to solve real-world problems.
The Genesis of F#
F# began in 2002, when Don Syme and others decided to ensure that the ML approach to pragmatic functional language design found a high-quality expression for the .NET platform. The project was closely associated with the design and implementation of Generics for the .NET Common Language Runtime. The first stabilized, supported version of F# was F# 2.0 in 2010. F# has always been open source and cross platform, and now adopts open engineering for all its components. In 2015, F# 4.0 was finalized, including commits from both enterprise and community contributors. This is the version of the language described in this book and is also the version included with Visual Studio 2015 and Xamarin Studio 5.0.
F# draws its core language from the programming language OCaml, which in turn comes from the ML family of programming languages, which dates back to 1974. F# also draws from C# for its object model; from Haskell, with regard to two language featuressequence expressions and computation expressions; and from Python, with regard to its indentation-aware syntax.
Despite the similarities to OCaml and Haskell, programming with F# is quite different in practice. In particular, the F# approach to object programming and dynamic language techniques is substantially different from other mainstream functional languages. Programming in F# tends to be more object oriented than in other functional languages. Programming also tends to be more flexible, as F# embraces techniques such as dynamic loading, dynamic typing, and reflection, and it adds techniques such as expression quotation, units-of-measure, type providers, and active patterns. We will cover these topics in this book and use them in many application areas.
F# began as a .NET language interoperating with C# and .NET libraries, and is quickly and easily able to leverage the thousands of libraries and techniques developed by the broader .NET community. However, F# is beginning to escape the .NET box; for example, it may also be compiled to Android, Apple iOS, Linux, JavaScript, and GPUs. F# 4.0 can be compiled to JavaScript through the open-source toolkits FunScript and WebSharper ( websharper.com ). Also, .NET is beginning to loosen its association with Windows, as the NET Core Framework is open source and cross platform, and you can contribute to it. F# code can also be edited and executed directly in most web browsers through sites such as tryfsharp.org and cloudsharper.com .
About This Book
This book is structured in three parts. deal with more advanced F# programming techniques.
Throughout this book, we address both programming constructs and programming techniques . Our approach is driven by examples: we show code, and then we explain it. Frequently, we give reference material describing the constructs used in the code and related constructs you may use in similar programming tasks. Weve found that an example-driven approach helps bring out the essence of a language and the way the language constructs work together. You can find a complete syntax guide in the appendix, and we encourage you to reference it while reading the book.
The books chapters are as follows, starting with basic F# techniques:
, Your First F# Program: Getting Started With F#, begins by introducing F# Interactive, a tool you can use to interactively evaluate F# expressions and declarations and that we encourage you to use while reading this book. In this chapter, you will use F# Interactive to explore some basic F# and .NET constructs, and we introduce many concepts that are described in more detail in later chapters.