Book Image

Mastering F#

By : Alfonso García-Caro Núñez, Suhaib Fahad
Book Image

Mastering F#

By: Alfonso García-Caro Núñez, Suhaib Fahad

Overview of this book

F# is a multi-paradigm programming language that encompasses object-oriented, imperative, and functional programming language properties. Now adopted in a wide range of application areas and is supported both by industry-leading companies who provide professional tools and by an active open community, F# is rapidly gaining popularity as it emerges in digital music advertising, creating music-focused ads for Spotify, Pandora, Shazam, and anywhere on the web. This book will guide you through the basics and will then help you master F#. The book starts by explaining how to use F# with Visual Studio, file ordering, and the differences between F# and C# in terms of usage. It moves on to explain the functional core of F# such as data types, type declarations, immutability, strong type interference, pattern matching, records, F# data structures, sequence expressions, and lazy evaluation. Next, the book takes you through imperative and asynchronous programming, F# type providers, applications, and testing in F#. Finally, we look into using F# with distributed programming and using F# as a suitable language for data science. In short, this book will help you learn F# for real-world applications and increase your productivity with functional programming.
Table of Contents (16 chapters)

Immutability, type declarations, and strong type inference


F# strongly adheres to immutability, meaning that the values declared in the program cannot be modified once they are constructed. Immutability also makes the code more readable because updates in the program state are more visible; thus it is easier to detect which operations affect the state. There are also F#-specific types, such as tuples, records, discriminated unions, and option types, which are immutable in their nature. They exhibit the following characteristics:

  • Being immutable
  • Values cannot be null
  • Built-in structural equality and comparison

F# has pattern matching as a special feature (from the ML family). We will go through pattern matching over predefined types and then discuss the different structural types and how to construct values with them. In further chapters, we will take a look at all the F#-specific types and ways to declare and use them.

Pattern matching

Pattern matching is defined by a match expression, which has...