Expert F#
■■■
Don Syme, Adam Granicz, and
Antonio Cisternino
Syme_850-4FRONT.fm Page i Monday, October 29, 2007 10:07 AM
Expert F#
Copyright © 2007 by Don Syme, Adam Granicz, and Antonio Cisternino
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-13: 978-1-59059-850-4
ISBN-10: 1-59059-850-4
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Lead Editors: Jim Huddleston, Jonathan Hassell
Technical Reviewer: Tomáš Petrícek
Editorial Board: Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick,
Jason Gilmore, Kevin Goff, Jonathan Hassell, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper,
Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh
Project Manager: Sofia Marchant
Copy Editor: Kim Wimpsett
Associate Production Director: Kari Brooks-Copony
Production Editor: Ellie Fountain
Compositor: Susan Glinert
Proofreader: April Eddy
Indexer: Present Day Indexing
Artist: Kinetic Publishing Services, LLC
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or
visit http://www.springeronline.com.
For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600,
Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://
www.apress.com.
The information in this book is distributed on an “as is” basis, without warranty. Although every precaution
has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to
any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly
by the information contained in this work.
The source code for this book is available to readers at http://www.expert-fsharp.com.
Syme_850-4FRONT.fm Page ii Monday, October 29, 2007 10:07 AM
This book is dedicated to the memory of James Huddleston,
the editor at Apress who initiated this book project and encouraged
the authors with his insights, loyalty, enthusiasm, and humor.
Jim passed away in February 2007, an enormous loss
to his family, Apress, and the authors.
Syme_850-4FRONT.fm Page iii Monday, October 29, 2007 10:07 AM
v
Contents at a Glance
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxi
About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
■CHAPTER 1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
■CHAPTER 2 Getting Started with F# and .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
■CHAPTER 3 Introducing Functional Programming . . . . . . . . . . . . . . . . . . . . . . . . . 27
■CHAPTER 4 Introducing Imperative Programming . . . . . . . . . . . . . . . . . . . . . . . . . 69
■CHAPTER 5 Mastering Types and Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
■CHAPTER 6 Working with Objects and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . 125
■CHAPTER 7 Encapsulating and Packaging Your Code . . . . . . . . . . . . . . . . . . . . . 155
■CHAPTER 8 Mastering F#: Common Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . 181
■CHAPTER 9 Introducing Language-Oriented Programming . . . . . . . . . . . . . . . . 211
■CHAPTER 10 Using the F# and .NET Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
■CHAPTER 11 Working with Windows Forms and Controls . . . . . . . . . . . . . . . . . . 275
■CHAPTER 12 Working with Symbolic Representations . . . . . . . . . . . . . . . . . . . . . 317
■CHAPTER 13 Reactive, Asynchronous, and Concurrent Programming . . . . . . . 355
■CHAPTER 14 Building Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
■CHAPTER 15 Working with Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
■CHAPTER 16 Lexing and Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
■CHAPTER 17 Interoperating with C and COM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
■CHAPTER 18 Debugging and Testing F# Programs . . . . . . . . . . . . . . . . . . . . . . . . . 523
■CHAPTER 19 Designing F# Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
■APPENDIX F# Brief Language Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571
Syme_850-4FRONT.fm Page v Monday, October 29, 2007 10:07 AM
vii
Contents
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxi
About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
■CHAPTER 1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
The Genesis of F# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Who This Book Is For . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
■CHAPTER 2 Getting Started with F# and .NET . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Creating Your First F# Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Turning On the Lightweight Syntax Option . . . . . . . . . . . . . . . . . . . . . 9
Documenting Code Using XMLDocs . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Understanding Scope and Using “let” . . . . . . . . . . . . . . . . . . . . . . . . 10
Understanding Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Calling Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Using Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Using Properties and the Dot-Notation. . . . . . . . . . . . . . . . . . . . . . . . 16
Using Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Using Imperative Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Using .NET Libraries from F# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Using open to Access Namespaces and Modules. . . . . . . . . . . . . . . 21
Using new and Setting Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Fetching a Web Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Syme_850-4FRONT.fm Page vii Monday, October 29, 2007 10:07 AM