Preface

There is a new language in Visual Studio called F#. It will help you write more expressive, easier-to-maintain code while enabling you to take advantage of the breadth of the .NET platform. Learning F# will not only make you more productive, it will also make you a better programmer. Once you have mastered concepts such as functional programming introduced in F#, you can apply them to programs written in other languages, as well as have a new perspective on programming.

Introducing F#

So what actually is F#? In a nutshell, F# is a multiparadigm programming language built on .NET, meaning that it supports several different styles of programming natively. I’ll spare you the history of the language and instead just go over the big bullets:

  • F# supports functional programming, which is a style of programming that emphasizes what a program should do, not explicitly how the program should work.

  • F# supports object-oriented programming. In F#, you can abstract code into classes and objects, enabling you to simplify your code.

  • F# supports imperative programming. In F# you can modify the contents of memory, read and write files, send data over the network, and so on.

  • F# is statically typed. Being statically typed means that type information is known at compile time, leading to type-safe code. F# won’t allow you to put a square peg into a round hole.

  • F# is a .NET language. It runs on the Common Language Infrastructure (CLI) and so it gets things like garbage collection (memory management) and powerful class libraries for free. F# also supports all .NET concepts natively, such as delegates, enumerations, structures, P/Invoke, and so on.

Even without all the jargon, it is clear that F# is a broad and powerful language. But don’t worry, we’ll cover it all step by step.

Who This Book Is For

This book isn’t intended to be an introductory text on programming and assumes familiarity with basic concepts like looping, functions, and recursion. However, no previous experience with functional programming or .NET is required.

If you come from a C# or VB.NET background, you should feel right at home. While F# approaches programming from a different viewpoint, you can apply all of your existing .NET know-how to programming in F#.

If you come from an OCaml or Haskell background, the syntax of F# should look very familiar. F# has most of the features of those languages, and adds many more to integrate well with .NET.

What You Need to Get Going

F# is included as part of Visual Studio 2010. This includes the F# compiler and project system, and contains all the features like syntax highlighting and IntelliSense that you would expect. A full version of Visual Studio 2010 is not required to use F#, however. F# will work on top of the Visual Studio 2008 shell, which is a free, slimmed-down version of Visual Studio. You can download the Visual Studio 2008 shell (Integrated Mode) at:

http://www.microsoft.com/downloads/details.aspx?FamilyID=40646580-97FA-4698-B65F-620D4B4B1ED7

Once you have the Visual Studio 2008 shell, you can install the latest F# Community Technology Preview, from the Microsoft F# Developer Center.

On non-Microsoft platforms, you can still write and deploy F# applications using the open source Mono platform.

How the Book Is Organized

This book is divided into two parts. Part I focuses on multiparadigm programming in F#. Early chapters will be devoted to programming in a specific F# paradigm, while later ones will help flesh out your understanding of language capabilities. By the end of Part I, you will be fluent in the F# language and its idioms.

Part II will introduce a few lingering concepts but primarily focus on applying F# in specialized areas. By the end of Part II, you will know how to utilize F# as a scripting language for parallel programming, and for creating domain-specific languages.

Part I, Multiparadigm Programming

Chapter 1, Introduction to F#, presents the F# language and the Visual Studio 2010 integrated development environment (IDE). Even if you are familiar with Visual Studio, I recommend you read this chapter, as F# has some unique characteristics when it comes to building and running projects.

Chapter 2, Fundamentals, introduces the core types and concepts that will be the foundation for all other chapters.

Chapter 3, Functional Programming, introduces functional programming and how to write F# code using this style.

Chapter 4, Imperative Programming, describes how to mutate values and change program state in an imperative manner.

Chapter 5, Object-Oriented Programming, covers object-oriented programming from creating simple types to inheritance and polymorphism.

Chapter 6, .NET Programming, goes over some style-independent concepts exposed by the .NET Framework and CLI.

Chapter 7, Applied Functional Programming, covers more advanced topics in functional programming such as tail recursion and functional design patterns.

Chapter 8, Applied Object-Oriented Programming, describes how to develop and take advantage of a rich type system. Special attention will be paid to how to leverage the functional aspects of F# to make object-oriented code better.

Part II, Programming F#

Chapter 9, Scripting, examines F# as a scripting language and how to make the most of F# script files.

Chapter 10, Computation Expressions, introduces an advanced F# language feature that will enable you to eliminate redundant code and add new capabilities to the core F# language.

Chapter 11, Asynchronous and Parallel Programming, takes a look at how to use F# to take advantage of multiple cores on a processor and the facilities in the F# and .NET libraries for parallel programming.

Chapter 12, Reflection, provides a look at the .NET reflection library and how to use it to create declarative programs.

Chapter 13, Quotations, introduces F# quotation expressions and how they can be used to do metaprogramming, as well as execute F# code on other computational platforms.

Appendixes

This book also features a couple of appendixes to flesh out any extra concepts you might be interested in.

Appendix A does a quick sweep through the existing technologies available on the .NET platform and how to use them from F#.

Appendix B covers how to write F# to interoperate with existing libraries as well as unmanaged code using P/Invoke and COM-interop.

Conventions Used in This Book

The following font conventions are used in this book:

Italic

Used for new concepts as they are defined.

Constant width

Used for code examples and F# keywords.

Constant width bold

Used for emphasis within program code.

Pay special attention to note styles within this text.

Note

Notes like this are used to add more detail for the curious reader.

Warning

Warnings are indicated in this style to help you avoid common mistakes.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the U.S. or Canada)
707-829-0515 (international/local)
707-829-0104 (fax)

Although we’ve tested and verified the information in this book, you may find that some aspects of the F# language have changed since this writing (or perhaps even a bug in an example!). We have a web page for the book, where we list examples and any plans for future editions. You can access this page at:

http://oreilly.com/catalog/9780596153649

You can also send messages electronically. To be put on the mailing list or request a catalog, send email to:

To comment on the book, send email to:

For information about this book and others, as well as additional technical articles and discussion on F#, see the O’Reilly website:

http://www.oreilly.com

or the O’Reilly .NET DevCenter:

http://www.oreillynet.com/dotnet

or the Microsoft Developer Network portal for F#:

http://www.msdn.com/fsharp

Using Code Examples

This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Programming F#, by Chris Smith. Copyright 2010 Chris Smith, 978-0-596-15364-9.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

Safari® Books Online

Note

Safari Books Online is an on-demand digital library that lets you easily search over 7,500 technology and creative reference books and videos to find the answers you need quickly.

With a subscription, you can read any page and watch any video from our library online. Read books on your cell phone and mobile devices. Access new titles before they are available for print, and get exclusive access to manuscripts in development and post feedback for the authors. Copy and paste code samples, organize your favorites, download chapters, bookmark key sections, create notes, print out pages, and benefit from tons of other time-saving features.

O’Reilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from O’Reilly and other publishers, sign up for free at http://my.safaribooksonline.com.

Acknowledgments

I get all the credit for writing this book, but the real effort for putting together the F# language comes from F# teams at Microsoft in Redmond and Microsoft Research in Cambridge, England. I’ve had immense pleasure working with a ridiculously talented group of people and without their effort F# would just be a TODO sticky note on Don Syme’s monitor.

Here are the people at Microsoft who made F# happen:

Anar AlimovJoe Pamer
Andrew KennedyJomo Fisher
Baofa FengKyle Ross
Brian McNamaraLaurant Le Blun
Daniel QuirkLuke Hoban
Dmitry LomovMatteo Taveggia
Dominic CooneyRalf Herbrich
Don SymeSantosh Zachariah
Gordon HogensonSean Wang
Greg NeverovTimothy Ng
James MargetsonTomas Petricek

Also, from the amazing editors at O’Reilly to the outstanding people in the F# community, this book could not have been possible without the help of many others: John Osborn, Laurel Ruma, Brian MacDonald, Matt Ellis, André van Meulebrouck, Stephen Toub, Jared Parsons, Dustin “honorary member” Campbell, Michael de la Maza, Alex Peake, Ryan Cavanaugh, Brian Peek, Matthew Podwysocki, Richard Minerich, Kate Moore, and finally Stuart Bowers—you’re easily a level-9 F# ninja by now.

Get Programming F# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.