F#
About the Tutorial
F# helps you in the daily development of the mainstream commercial business software.
This tutorial provides a brief knowledge about F# and its features, and also provides the
various structures and syntaxes of its methods and functions.
Audience
This tutorial has been designed for beginners in F#, providing the basic to advanced
concepts of the subject.
Prerequisites
Before starting this tutorial you should be aware of the basic understanding of Functional
Programming, C# and .Net.
Copyright & Disclaimer
Copyright 2015 by Tutorials Point (I) Pvt. Ltd.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at [email protected]
i
F#
Table of Contents
About the Tutorial .....................................................................................................................................
Audience .................................................................................................................................................. i
Prerequisites ............................................................................................................................................ i
Copyright & Disclaimer............................................................................................................................. i
Table of Contents .................................................................................................................................... ii
1. F# – OVERVIEW ................................................................................................................... 1
About F#.................................................................................................................................................. 1
Features of F#.......................................................................................................................................... 1
Uses of F# ................................................................................................................................................ 1
2. F# – ENVIRONMENT SETUP ................................................................................................. 3
Integrated Development Environment (IDE) for F# ................................................................................. 3
Writing F# Programs on Linux ................................................................................................................. 3
3. F# – PROGRAM STRUCTURE ................................................................................................ 4
4. F# – BASIC SYNTAX .............................................................................................................. 5
Tokens in F# ............................................................................................................................................ 5
Comments in F# .................................................................................................................................... 10
A Basic Program and Application Entry Point in F# ................................................................................ 11
5. F# – DATA TYPES ............................................................................................................... 12
Integral Data Types ............................................................................................................................... 12
Floating Point Data Types ...................................................................................................................... 14
Text Data Types..................................................................................................................................... 16
Other Data Types .................................................................................................................................. 16
6. F# – VARIABLES ................................................................................................................. 18
Variable Declaration in F# ..................................................................................................................... 18
ii
F#
Variable Definition with Type Declaration ............................................................................................. 19
Mutable Variables ................................................................................................................................. 20
7. F# – OPERATORS ............................................................................................................... 22
Arithmetic Operators ............................................................................................................................ 22
Comparison Operators .......................................................................................................................... 23
Boolean Operators ................................................................................................................................ 25
Bitwise Operators ................................................................................................................................. 26
Operators Precedence ........................................................................................................................... 28
8. F# – DECISION MAKING ..................................................................................................... 30
F# - if /then Statement .......................................................................................................................... 31
F# - if /then/else Statement .................................................................................................................. 32
F# - if /then/elif/else Statement ........................................................................................................... 33
F# - Nested if Statements ...................................................................................................................... 34
9. F# – LOOPS ........................................................................................................................ 35
F# - for...to and for... downto Expressions ............................................................................................. 36
F# - for… in Expressions ......................................................................................................................... 38
F# - While…do Expressions .................................................................................................................... 39
F# - Nested Loops .................................................................................................................................. 39
10. F# – FUNCTIONS ................................................................................................................ 41
Defining a Function ............................................................................................................................... 41
Parameters of a Function ...................................................................................................................... 41
Recursive Functions .............................................................................................................................. 43
Arrow Notations in F# ........................................................................................................................... 44
Lambda Expressions .............................................................................................................................. 45
Function Composition and Pipelining .................................................................................................... 45
11. F# – STRINGS ..................................................................................................................... 47
iii
F#
String Literals ........................................................................................................................................ 47
Ways of Ignoring the Escape Sequence ................................................................................................. 47
Basic Operations on Strings ................................................................................................................... 48
12. F# – OPTIONS .................................................................................................................... 51
Using Options ........................................................................................................................................ 51
Option Properties and Methods ............................................................................................................ 52
13. F# – TUPLES ....................................................................................................................... 54
Accessing Individual Tuple Members..................................................................................................... 55
14. F# – RECORDS.................................................................................................................... 56
Defining a Record .................................................................................................................................. 56
Creating a Record .................................................................................................................................. 56
15. F# – LISTS.......................................................................................................................... 59
Creating and Initializing a List ................................................................................................................ 59
Properties of List Data Type .................................................................................................................. 61
Basic Operations on List ........................................................................................................................ 62
16. F# – SEQUENCES ............................................................................................................... 70
Defining Sequences ............................................................................................................................... 70
Creating Sequences and Sequence Expressions ..................................................................................... 70
Basic Operations on Sequence .............................................................................................................. 72
17. F# – SETS ........................................................................................................................... 80
Creating Sets ......................................................................................................................................... 80
Basic Operations on Sets ....................................................................................................................... 80
18. F# – MAPS ......................................................................................................................... 84
Creating Maps ....................................................................................................................................... 84
Basic Operations on Maps ..................................................................................................................... 85
iv
F#
19. F# – DISCRIMINATED UNIONS ........................................................................................... 88
20. F# – MUTABLE DATA ......................................................................................................... 91
Mutable Variables ................................................................................................................................. 91
Uses of Mutable Data ............................................................................................................................ 93
21. F# – ARRAYS ...................................................................................................................... 95
Creating Arrays ..................................................................................................................................... 95
Basic Operations on Arrays ................................................................................................................... 96
Creating Arrays Using Functions .......................................................................................................... 101
Searching Arrays ................................................................................................................................. 104
22. F# – MUTABLE LISTS ........................................................................................................ 106
Creating a Mutable List ....................................................................................................................... 106
The List(T) Class ................................................................................................................................... 106
23. F# – MUTABLE DICTIONARY............................................................................................. 114
Creating of a Mutable Dictionary ........................................................................................................ 114
The Dictionary(TKey, TValue) Class ..................................................................................................... 114
24. F# – BASIC IO ................................................................................................................... 118
Core.Printf Module ............................................................................................................................. 118
Format Specifications .......................................................................................................................... 119
The Console Class ................................................................................................................................ 121
The System.IO Namespace .................................................................................................................. 126
25. F# – GENERICS ................................................................................................................. 131
Generic Class ....................................................................................................................................... 132
26. F# – DELEGATES ............................................................................................................. 133
Declaring Delegates............................................................................................................................. 133
v
F#
27. F# – ENUMERATIONS ...................................................................................................... 136
Declaring Enumerations ...................................................................................................................... 136
28. F# – PATTERN MATCHING ............................................................................................... 138
Pattern Matching Functions ................................................................................................................ 140
Adding Filters or Guards to Patterns ................................................................................................... 140
Pattern Matching with Tuples ............................................................................................................. 141
Pattern Matching with Records ........................................................................................................... 142
29. F# – EXCEPTION HANDLING ............................................................................................. 143
Examples of Exception Handling.......................................................................................................... 145
30. F# – CLASSES ................................................................................................................... 149
Constructor of a Class .......................................................................................................................... 150
Let Bindings ......................................................................................................................................... 151
31. F# – STRUCTURES ........................................................................................................... 152
32. F# – OPERATOR OVERLOADING ....................................................................................... 154
Implementation of Operator Overloading ........................................................................................... 154
33. F# – INHERITANCE ........................................................................................................... 156
Base Class and Sub Class ..................................................................................................................... 156
Overriding Methods ............................................................................................................................ 158
Abstract Classes .................................................................................................................................. 159
34. F# – INTERFACES ............................................................................................................ 162
Calling Interface Methods ................................................................................................................... 163
Interface Inheritance ........................................................................................................................... 164
35. F# – EVENTS .................................................................................................................... 166
The Event Class and Event Module ...................................................................................................... 166
vi
F#
Creating Events ................................................................................................................................... 167
36. F# – MODULES ................................................................................................................ 171
37. F# – NAMESPACES ........................................................................................................... 174
Declaring a Namespace ....................................................................................................................... 174
vii