Table Of Contentwww.it-ebooks.info
Programming F#
www.it-ebooks.info
www.it-ebooks.info
Programming F#
Chris Smith
foreword by Don Syme
Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo
www.it-ebooks.info
Programming F#
by Chris Smith
Copyright © 2010 Chris Smith. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (http://my.safaribooksonline.com). For more information, contact our
corporate/institutional sales department: (800) 998-9938 or [email protected].
Editor: Laurel Ruma Indexer: Jay Marchand
Production Editor: Loranah Dimant Cover Designer: Karen Montgomery
Production Services: Newgen, Inc. Interior Designer: David Futato
Illustrator: Robert Romano
Printing History:
October 2009: First Edition.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc. Programming F#, the image of a bullfinch, and related trade dress are trademarks
of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and author assume
no responsibility for errors or omissions, or for damages resulting from the use of the information con-
tained herein.
TM
This book uses RepKover™, a durable and flexible lay-flat binding.
ISBN: 978-0-596-15364-9
[M]
1254512912
www.it-ebooks.info
Table of Contents
Foreword .................................................................. xiii
Preface ..................................................................... xv
Part I. Multiparadigm Programming
1. Introduction to F# ....................................................... 3
Getting to Know F# 3
Visual Studio 2010 4
Your Second F# Program 5
Values 6
Whitespace Matters 6
.NET Interop 8
Comments 8
F# Interactive 8
Managing F# Source Files 11
2. Fundamentals ......................................................... 13
Primitive Types 13
Numeric Primitives 14
Arithmetic 15
Conversion Routines 17
BigInt 17
Bitwise Operations 18
Characters 18
Strings 19
Boolean Values 20
Comparison and Equality 21
Functions 22
Type Inference 23
v
www.it-ebooks.info
Generic Functions 24
Scope 25
Control Flow 27
Core Types 29
Unit 29
Tuple 30
Lists 32
Aggregate Operators 36
Option 39
Printfn 41
Anatomy of an F# Program 42
Modules 43
Namespaces 43
Program Startup 44
3. Functional Programming ................................................ 47
Programming with Functions 48
Immutability 49
Function Values 50
Recursive Functions 53
Symbolic Operators 55
Function Composition 57
Pattern Matching 62
Match Failure 64
Named Patterns 64
Matching Literals 65
when Guards 66
Grouping Patterns 67
Matching the Structure of Data 68
Outside of Match Expressions 69
Alternate Lambda Syntax 70
Discriminated Unions 70
Using Discriminated Unions for Tree Structures 72
Pattern Matching 73
Methods and Properties 75
Records 75
Cloning Records 76
Pattern Matching 77
Type Inference 77
Methods and Properties 78
Lazy Evaluation 79
Lazy Types 79
Sequences 80
vi | Table of Contents
www.it-ebooks.info
Sequence Expressions 81
Seq Module Functions 82
Aggregate Operators 83
4. Imperative Programming ............................................... 85
Understanding Memory in .NET 86
Value Types Versus Reference Types 87
Default Values 87
Reference Type Aliasing 89
Changing Values 89
Reference Cells 91
Mutable Records 92
Arrays 92
Indexing an Array 93
Array Slices 95
Creating Arrays 96
Pattern Matching 97
Array Equality 97
Array Module Functions 98
Multidimensional Arrays 99
Mutable Collection Types 101
List<'T> 101
Dictionary<'K,'V> 102
HashSet<'T> 104
Looping Constructs 105
While Loops 105
For Loops 106
Exceptions 108
Handling Exceptions 109
Reraising Exceptions 111
Defining Exceptions 111
5. Object-Oriented Programming .......................................... 115
Programming with Objects 115
The Benefits of OOP 115
When OOP Breaks Down 116
Understanding System.Object 116
Common Methods 117
Object Equality 119
Generated Equality 120
Understanding Classes 122
Explicit Construction 123
Implicit Class Construction 124
Table of Contents | vii
www.it-ebooks.info
Generic Classes 125
The Self-Identifier 127
Methods and Properties 127
Properties 128
Setting Properties in the Constructor 129
Methods 129
Static Methods, Properties, and Fields 131
Method Overloading 133
Accessibility Modifiers 134
Inheritance 136
Method Overriding 138
Categories of Classes 139
Casting 141
6. .NET Programming .................................................... 145
The .NET Platform 145
The CLI 145
Garbage Collection 146
Interfaces 148
Using Interfaces 149
Defining Interfaces 150
Object Expressions 151
Object Expressions for Interfaces 152
Object Expressions for Derived Classes 153
Extension Methods 154
Extending Modules 155
Enumerations 156
Creating Enumerations 156
Conversion 157
When to Use an Enum Versus a Discriminated Union 158
Structs 159
Creating Structs 159
Restrictions 161
When to Use a Struct Versus a Record 161
7. Applied Functional Programming ....................................... 163
Units of Measure 163
Defining Units of Measure 165
Converting Between Units of Measure 166
Generic Units of Measure 167
Active Patterns 168
Single-Case Active Patterns 169
Partial-Case Active Patterns 170
viii | Table of Contents
www.it-ebooks.info
Parameterized Active Patterns 172
Multi-Case Active Patterns 173
Using Active Patterns 174
Using Modules 178
Converting Modules to Classes 178
Intentional Shadowing 181
Controlling Module Usage 182
Mastering Lists 183
List Operations 184
Using Lists 185
Tail Recursion 187
Understanding the Stack 188
Introducing Tail Recursion 190
Tail-Recursive Patterns 192
Programming with Functions 196
Currying 196
Eliminating Redundant Code 197
Closures 198
Functional Patterns 199
Memoization 199
Mutable Function Values 202
Lazy Programming 203
8. Applied Object-Oriented Programming ................................... 205
Operators 205
Operator Overloading 205
Indexers 207
Adding Slices 209
Generic Type Constraints 211
Delegates and Events 214
Defining Delegates 215
Combining Delegates 217
Events 218
Creating Events 218
The Event<_,_> Class 220
The Observable Module 221
Creating .NET Events 225
Part II. Programming F#
9. Scripting ............................................................ 229
F# Script Files 230
Table of Contents | ix
www.it-ebooks.info