INTRODUCTION

This is a book on the F# programming language.

On the surface of things, that is a nonsensical and/or intuitively obvious statement, given the title of this book. However, despite the apparent redundancy in saying it aloud, the sentence above elegantly describes what this book is about: We are not attempting to teach developers how to accomplish tasks from other languages in this one, nor are we attempting to evangelize the language or its feature set or its use "over" other languages. We assume that you are reading this book because you have an interest in learning the F# language: its syntax, its semantics, its pros and cons, and its use in concert with other parts of the .NET ecosystem.

WHO THIS BOOK IS FOR

In order to keep to the core focus of the book, we assume that you, the reader, are a journeyman .NET developer, familiar with at least one of the programming languages in the .NET ecosystem: C# or Visual Basic will be the most common language of choice for those in the .NET community — but if you've learned C++/CLI or even one of the lesser-known .NET languages (IronPython or IronRuby, perhaps, or even one of the dozens or so of "alternative" languages for the CLR), you'll still be able to follow along without too much difficulty.

In particular, we assume that you're already comfortable with concepts like assemblies, managed code, and executing on top of a virtual machine, so none of that ".NET 101" topical material appears here, as it would be redundant to what you've already read in a beginning or intermediate C# or Visual Basic book. You've probably used Reflection at some point in your career, and/or XML, WinForms, Windows Presentation Foundation (or its web-based successor, Silverlight), Windows Communication Foundation, with maybe a little database access thrown in for good measure.

In short, we assume that you've been developing in the .NET environment for the past couple of years. If that's not you, you may want to start with one of the introductory .NET titles, such as Beginning C# 3.0 or Beginning Visual Basic 2010. For that reason, there are no "hand-holding" steps found in a variety of other books when it comes to creating Visual Studio Solution files or creating new Projects; not only would those steps be useless to the journeyman .NET developer (not to mention somewhat insulting), but they are also useless to the F# developer who wants to use MonoDevelop and the Mono implementation of the Common Language Runtime for writing and executing F# code. Considering that F# has held full compatibility with Mono as a goal since its inception, ignoring that crowd felt rude.

If you are of the handful of functional programmers who've been working in Haskell, Ocaml, ML, or some Lisp-based derivative, you're probably not going to like this book. We spend no time discussing monads, monoids, or catamorphisms here, nor do we consider that a missing part of the book that "we just didn't have time to cover;" the book you want will be written entirely differently than this one, in a different style than this one, and probably by entirely different authors.

This book assumes you are comfortable with object-orientation and want to start down the path of learning not only how to program with functional concepts, but that you want to work with both functions and objects, in some kind of pleasant harmony, on top of the CLR.

If you're still not sure if you're the right reader for this book, take this little test: read Chapter 1, Primer, and see if that makes sense to you. If it seems way too simple, you're probably more functionally-minded than we assumed the average reader to be; you'll find the book useful as a reference, perhaps, or as a skimmable guide to the syntax, but you won't find mind-popping concepts in here. If it seems way too difficult, then don't despair — that chapter is intended as a microcosm of where we want you to end up.

If that chapter seems "just right," though, head on over to the register.

WHAT THIS BOOK COVERS

This book takes a four-part approach to the F# language (officially, the F# 2.0 version of the language): basics, objects, functions, and "integration" (meaning how F# interacts with various .NET technologies like Silverlight or relational databases). We go over the 90% of the F# programming language that we think you're going to need.

Note that we're not a complete reference on the language. We ignore the 10% of the language specification that we think most .NET developers will never run into, on the grounds that if you do run into one of those edge-case constructs, you'll have enough familiarity with the F# syntax to figure it out, or you'll look it up in the F# 2.0 Language Reference (or ask somebody in forums or on a mailing list) for the exact details. (Our guess is that it'll be at least a year or so as a practicing F# developer before you run into one of those constructs.)

HOW THIS BOOK IS STRUCTURED

The book is arranged in, we think, the order a practicing .NET (which means, "object-trained") developer will find most useful.

Part 0 is the Primer, a gentle up-sloping introduction to the concepts that the F# language incorporates as first-class citizens as well as a hint of some of the ways in which functional programming can be useful within the .NET environment.

Part I covers Basics. Here, you'll find definitions of the primitive types, flow control, syntax rules, and other such things that have to be defined before anything else can be discussed.

Part II covers Objects. F# permits the creation of new classes, interfaces and other object-oriented types, as well as the traditional set of object-oriented features such as implementation inheritance, and they're all covered here. In keeping with the tone of the book, we don't spend a lot of time teaching you what "IS-A" means; as a practicing .NET developer, you already know that. So this chapter focuses primarily on the syntax, and how F# "does objects" both similarly to, and differently from, its sibling languages C# and Visual Basic.

Part III takes on an entirely different tone, that of Functions, and will be the new material and concepts for most readers. Instead of using Visual Studio, you use the REPL (Read-Evaluate-Print-Loop) for writing snippets of code and seeing them execute immediately. Instead of building up objects, you create functions. And so on. Readers could stop after the end of Part II and have F# as "a better C#," but doing so would be missing out on some of the very things that make F# vastly more interesting than C#.

Part IV, then, shows how to use F# with a variety of different technologies (XML, Silverlight, services, and so on), and tries to show one way — not the way — to use F# beyond straight console-mode applications. As tempting as it might be to jump straight here and cut-and-paste the code into your own applications, resist the urge until you've read through the first three parts. It's vastly more satisfying to be able to predict what the code will look like as you read along, than to just see the code execute and have no real idea why.

WHAT YOU NEED TO USE THIS BOOK

We assume that you have Visual Studio 2010 installed on your system. As of this writing, unfortunately, no "Visual 2010 F# Express" edition download exists from Microsoft, so you'll need to have Visual Studio 2010 Professional installed, with (obviously) the Visual F# feature set installed with it.

If you have a Visual Studio 2008 installation, you can still play in the F# playground, but it's a bit harder: You must find and download the Visual F# install bits for Visual Studio 2008 (it's a Visual Studio package), and install them. Technically, this is the "F# 2.0 for Windows + Visual Studio 2008 (April 2010 release)" version.

If you don't have any version of Visual Studio and are unwilling to acquire one, another option is available to you: a self-constructed version of "Visual F# Express." By installing the Visual Studio 2008 Shell and the aforementioned "F# 2.0 for Windows..." bits, you will have a version of Visual Studio 2008 that contains only the F# language compiler and nothing else — no ASP.NET, no Silverlight, nothing. (As of this writing, the F# team was exploring whether the Visual Studio 2010 Shell would work as a host for the F# 2.0 bits, and while it's likely that such a combination will work, none of the authors here can verify that for a fact.)

If you are looking to use F# on Mono, then you want to download the "F# 2.0 for Mono/Mac/Linux (compiler binaries)" and install according to the instructions there.

As of this writing, the "F# Downloads" page on Microsoft Research (research.microsoft.com) has links to all of the different flavors discussed here.

Our assumption is that the vast majority of our readership will be using Visual Studio 2010 or Visual Studio 2008, so if you're one of the readers outside of that population set, we apologize ahead of time for whatever conflicts you will find with command-prompt output, screen shots, or references to IDE instructions.

CONVENTIONS

To help you get the most from the text and keep track of what's happening, we've used a number of conventions throughout the book.

Warning

Boxes with a warning icon like this one hold important, not-to-be-forgotten information that is directly relevant to the surrounding text.

Note

The pencil icon indicates notes, tips, hints, tricks, or asides to the current discussion.

As for styles in the text:

  • We show keyboard strokes like this: Ctrl+A.

  • We show file names, URLs, and code within the text like so: persistence.properties.

  • We present code like this:

    We use a monofont type for code examples.

SOURCE CODE

As you work through the examples in this book, you may choose either to type in all the code manually, or to use the source code files that accompany the book. All the source code used in this book is available for download at www.wrox.com. When at the site, simply locate the book's title (use the Search box or one of the title lists) and click the Download Code link on the book's detail page to obtain all the source code for the book. Code that is included on the website is highlighted by the following icon:

SOURCE CODE

Listings include the filename in the title. If it is just a code snippet, you'll find the filename in a code note such as this:

SOURCE CODE

Note

Because many books have similar titles, you may find it easiest to search by ISBN; this book's ISBN is 978-0-470-52801-3.

Once you download the code, just decompress it with your favorite compression tool. Alternately, you can go to the main Wrox code download page at www.wrox.com/dynamic/books/download.aspx to see the code available for this book and all other Wrox books.

ERRATA

We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in one of our books, like a spelling mistake or faulty piece of code, we would be very grateful for your feedback. By sending in errata, you may save another reader hours of frustration, and at the same time, you will be helping us provide even higher quality information.

To find the errata page for this book, go to www.wrox.com and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Book Errata link. On this page, you can view all errata that has been submitted for this book and posted by Wrox editors. A complete book list, including links to each book's errata, is also available at www.wrox.com/misc-pages/booklist.shtml.

If you don't spot "your" error on the Book Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We'll check the information and, if appropriate, post a message to the book's errata page and fix the problem in subsequent editions of the book.

P2P.WROX.COM

For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a Web-based system for you to post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.

At p2p.wrox.com, you will find a number of different forums that will help you, not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:

  1. Go to p2p.wrox.com and click the Register link.

  2. Read the terms of use and click Agree.

  3. Complete the required information to join, as well as any optional information you wish to provide, and click Submit.

  4. You will receive an e-mail with information describing how to verify your account and complete the joining process.

Note

You can read messages in the forums without joining P2P, but in order to post your own messages, you must join.

Once you join, you can post new messages and respond to messages other users post. You can read messages at any time on the Web. If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.

For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works, as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset