F# for C# Developers
Tao Liu
Published by Microsoft Press
Special Upgrade Offer
If you purchased this ebook directly from oreilly.com, you have the following benefits:
DRM-free ebooksuse your ebooks across devices without restrictions or limitations
Multiple formatsuse on your laptop, tablet, or phone
Lifetime access, with free updates
Dropbox syncingyour files, anywhere
If you purchased this ebook from another retailer, you can upgrade your ebook to take advantage of all these benefits for just $4.99. to access your ebook upgrade.
Please note that upgrade offers are not available from sample content.
A Note Regarding Supplemental Files
Supplemental files and examples for this book can be found at http://examples.oreilly.com/9780735670266-files/. Please use a standard desktop web browser to access these files, as they may not be accessible from all ereader devices.
All code files or examples referenced in the book will be available online. For physical books that ship with an accompanying disc, whenever possible, weve posted all CD/DVD content. Note that while we provide as much of the media content as we are able via free download, we are sometimes limited by licensing restrictions. Please direct any questions or concerns to .
Foreword
People often ask, What can F# do that C# cannot? In this book, you will discover much of what F# can do! You will see familiar things such as object programming and design patterns. Further, you will also see powerful new things like pattern matching, piping, first-class events, object expressions, options, tuples, records, discriminated unions, active patterns, agents, computation expressions and, perhaps most distinctively, type providers.
However, we also need to ask the other question: What can C# do that F# cannot? There is one important part to this answer that I will focus on here: C# can cause NullReferenceExceptions . What? I hear you ask. Does F# not have nulls? Right! Perhaps the most important thing the C# programmer needs to know about F# is that F# does not use nulls in routine programming.
Lets look at some evidence. People using F# at a major UK energy company did a study of two similar ETL (Extract, Transform, Load) applications.[] Broadly speaking, the applications were in the same zone in terms of functionality or, if anything, the F# application implemented more features. The F# project had a very low bug rate, and its code was 26 times smaller. The size difference is not only the result of language differences; there are also differences in design methodology. The C# project is characterized by the inappropriate overuse of elaborate object abstractions often seen in Java projectsfor example, elaborate and unnecessary class hierarchies.
Interestingly, the comparison records that the C# project had 3036 explicit null checks, where a functionally similar F# project had 27, a reduction of 112 times in the total number of null checks. The other statistics in the comparison shown are also compelling, particularly the defects since go live: the F# code had zero defects since go live, and the C# code had too many. These are not unrelated: nulls cause defects. In my opinion, the lack of nulls in routine coding alone makes it worth switching your programming teams to F# where possible.
In this book, you will learn many wonderful things about F#. But dont lose sight of the big picture: F# is about writing accurate, correct, efficient, interoperable code that gets deployed on time in enterprise scenarios. It does this partly by removing the most pernicious of evils: nulls. If you and your team embrace it, then, all else being equal, your life will be simpler, happy, and more productive.
Don Syme
F# Community Contributor
Introduction
F# is a functional programming language from Microsoft. It is the first class language shipped in Visual Studio. It has been applied successfully in several areas, such as in the areas of financial software and web development. If you are a C# developer and want to use functional programming to write concise code with fewer bugs, F# is the right tool for you.
F# for C# Developers introduces, in an organized way, the F# language and several applications. It starts from how F# can perform imperative and object-oriented programming tasks and then moves on to covering unique F# features, such as type providers. By introducing F# design patterns with a large number of samples, this book not only delivers a basic introduction but also helps you apply F# in your daily programming work.
In addition to covering core F# core features, I also discuss F# HTML5 development, F# Azure development, and using general-purpose graphics processing units (GPGPUs) with F#. Beyond the explanatory content, each chapter includes examples and downloadable sample projects you can explore for yourself.
Who Should Read This Book
I wrote this book to help existing C# developers understand the core concepts of F# and help C# developers use F# in their daily work. It is especially useful for C# programmers looking to write concise code for algorithm design, web development, and cloud development. Although most readers will have no prior experience with F#, the book is also useful for those familiar with earlier versions of F# and who are interested in learning about the newest features.
You should have at least a minimal understanding of .NET development and object-oriented programming concepts to get the most benefit from this book. You also should have a basic understanding of data structures and generic algorithms. Experience in using C# is required as well.
Who Should Not Read This Book
This book is aimed at both experienced .NET C# developers who interested in extending their knowledge in functional programming and beginners in F# who want to understand F# and apply F# to their daily programming work. If you have no C# programming experience, this book might be difficult for you.
Organization of This Book
This book is divided into three sections, each of which focuses on a different aspect. , introduces several real-world applications, including web development, Azure cloud development, and GPGPU.
Finding Your Best Starting Point in This Book
The various sections of F# for C# Developers cover a wide range of technologies. Depending on your needs and your existing understanding, you might want to focus on specific areas of the book. Use the following table to determine how best to proceed through the book.
If you are | Follow these steps |
New to F# but experienced with C# | Focus on for some unique F# features |
Familiar with earlier versions of F# | Briefly read if you need a refresher on the core concepts, but also want to focus on type providers. |
Most of the books chapters include hands-on samples that let you try out the concepts just learned. No matter which sections you choose to focus on, be sure to download and install the sample applications on your system.
Conventions and Features in This Book
This book presents information using conventions designed to make the information readable and easy to follow:
Boxed elements with labels such as Note provide additional information or alternative methods for completing a step successfully.
Text that you type (apart from code blocks) appears in bold. A plus sign (+) between two key names means that you must press those keys at the same time. For example, Press Alt+Tab means that you hold down the Alt key while you press the Tab key.