F# Tutorial
Learn F# step by step: 37 lessons in 9 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting Started
- What Is F#? A Functional-First Language for .NET Meet F#, the functional-first language on .NET. Learn what makes it different, where it is used, how it compares with C#, and what you will build in this course.
- Installing .NET and Using F# Interactive (dotnet fsi) Install the .NET SDK, run F# in the interactive shell (dotnet fsi), execute .fsx scripts, create a console project, and pick an editor such as VS Code, Rider or Visual Studio.
- Your First F# Program: printfn, Comments and Entry Points Write your first F# program. Learn printfn and format specifiers, comments, significant whitespace, modules and namespaces, open, and the [<EntryPoint>] main function.
- Values, let Bindings, Scope and Mutability in F# Understand F# let bindings: immutable values, type inference and annotations, shadowing, scope and nested lets, naming rules, and when to use mutable and ref cells.
Language Basics
- F# Basic Types: Integers, Floats, Bool, Char and More A tour of F# primitive types: int, int64, byte, float, decimal, bool, char, numeric literals and suffixes, overflow behaviour, and how to convert between types explicitly.
- F# Strings: Literals, Interpolation, Slicing and the String Module Work with F# strings: literals, triple-quoted and verbatim strings, interpolation with format specifiers, sprintf, concatenation, slicing, the String module and .NET string methods.
- F# Operators, Precedence and Type Conversion Learn F# arithmetic, comparison, boolean, bitwise and string operators, operator precedence, custom operators, and how to convert between types with int, float, string and parse functions.
- F# if/else Expressions and for, while and Loops Learn F# control flow: if/elif/else as expressions, for-to and for-in loops, while loops, early exit patterns, and why recursion and collection functions replace most loops.
- F# Tuples, Struct Tuples and the unit Type Group values with F# tuples, take them apart by pattern, return several values from a function, use fst and snd, struct tuples, and understand unit, ignore and discards.
Functions and Pipelines
- Defining Functions in F#: Parameters, Types and Generics Learn how to define F# functions with let, call them without parentheses, annotate types, return values, write generic and inline functions, use rec, and organise helper functions with nesting.
- Currying and Partial Application in F# Explained Understand why every F# function takes one argument, how currying works, how to create new functions by supplying some arguments, and how to order parameters so partial application is useful.
- F# Pipelines and Function Composition: |>, >> and << Master the F# pipeline operator |> and composition operators >> and <<. Build readable data transformations, understand type inference flow, and compare pipelines with nested calls.
- Recursion in F#: rec, Tail Calls, Accumulators and Mutual Recursion Write recursive functions in F#: base case and recursive case, accumulator style, tail-call optimisation, avoiding stack overflow, mutual recursion with and, and when to prefer List.fold.
- Lambdas and Higher-Order Functions in F#: map, filter, fold Use F# lambda expressions, pass functions as arguments, return functions from functions, and learn map, filter, fold, closures and function values with clear examples and diagrams.
Collections and Sequences
- F# Lists: Immutable Linked Lists, Cons, Append and the List Module Learn F# lists: literals, ranges, cons (::) and append (@), head/tail, indexing costs, the List module functions, sorting, grouping, zip, and how immutable lists share memory.
- F# Arrays: Creation, Indexing, Mutation, Slicing and the Array Module Learn F# arrays: literals, Array.init and zeroCreate, zero-based indexing, in-place mutation, slicing, 2D arrays, the Array module with sort, map, filter, and when arrays beat lists.
- F# Sequences: Lazy seq Expressions, Infinite Data and the Seq Module Understand F# sequences: lazy evaluation, seq expressions with yield, infinite sequences with Seq.initInfinite and unfold, caching, the Seq module, and when to choose seq over list or array.
- F# Maps and Sets: Immutable Dictionaries, Set Operations and Dictionary Use F# Map for immutable key-value lookups and Set for unique values: creation, add, remove, tryFind, set union, intersection and difference, plus when to pick Dictionary or HashSet instead.
- F# List Comprehensions and Choosing Collection Functions Build collections with F# list, array and seq comprehensions, use yield and yield!, nested for loops and guards, then learn which collection function to choose for each task with worked examples.
Types, Records and Unions
- Records and Anonymous Records Learn F# records: declaring fields, creating values, copy-and-update with with, structural equality, struct records, anonymous records and when to choose them over tuples or classes.
- Discriminated Unions Learn F# discriminated unions: cases with data, single-case wrappers, recursive types, struct unions, how they differ from enums, and how to model real domains so invalid states cannot exist.
- Units of Measure and Type Abbreviations Learn how F# catches unit mistakes at compile time with units of measure, when type abbreviations help, how enums differ from unions, and how to make small wrapper types that prevent bugs.
- Classes, Interfaces and Object Expressions Learn object-oriented F#: classes with primary constructors, members, mutable state, interfaces, abstract classes, inheritance and object expressions, and when to prefer functions and records.
Pattern Matching and Options
- Pattern Matching in Depth Master F# pattern matching: constant, tuple, list, record and union patterns, guards, as, or-patterns, nested patterns, wildcards and completeness checking, with a runnable C# comparison.
- Option and ValueOption Learn how F# replaces null with Option: Some and None, the Option module (map, bind, defaultValue), pipelines, ValueOption for performance, and interop with nullable .NET values.
- Result and Error Handling Learn error handling in F#: the Result type with Ok and Error, Result.map and bind, validation with custom error unions, exceptions with try/with, and railway-oriented programming.
- Active Patterns Learn F# active patterns: single-case, multi-case and partial patterns with the banana-clip syntax, parameterized patterns, parsing with patterns, and how they make match expressions read like the domain.
Async and Concurrency
- Async Workflows Learn F# async workflows: async { }, let! and do!, running with Async.RunSynchronously and Async.Start, parallel composition, error handling, cancellation and the difference from .NET tasks.
- Tasks and Parallel Code Learn the F# task { } expression for .NET Task interop, awaiting tasks from async code, Array.Parallel and PLINQ-style parallelism, shared state pitfalls and when to use which tool.
- MailboxProcessor and Agents Learn F# agents with MailboxProcessor: posting messages, receive loops, replying with PostAndReply, keeping state without locks, error handling, and a safe counter and cache example.
Interop and Tooling
- Interop with C# and .NET Learn F# and C# interoperability: using .NET libraries from F#, null and out parameters, overloads and optional arguments, exposing F# code to C# callers, and mixing projects in one solution.
- Projects, Packages and the dotnet CLI Learn the F# project workflow: dotnet new, the fsproj file and compile order, NuGet packages, solutions, F# Interactive and .fsx scripts, #r references, formatting with Fantomas and publishing.
- Testing F# Code Learn how to test F# code: xUnit and Expecto test projects, assertions, naming tests with backticks, testing pure functions and Results, property-based testing with FsCheck, and test organisation.
Projects and Practice
- Project: Bank Account Simulator Build a bank account model in F#: records for state, a union for commands and events, validation with Result, applying events with fold, and an agent that serializes access, step by step.
- Project: Expression Evaluator Build a calculator in F#: tokenize text with a list of unions, parse with recursive descent and precedence, evaluate the tree with pattern matching, report errors with Result, and simplify expressions.
- Project: Text Statistics Build a text analyzer in F#: split text into words, count frequencies with Seq.countBy, rank with sortBy, compute averages and longest words, read files lazily, and print a report with pipelines.
- F# Practice Exercises Twenty F# exercises with hidden solutions covering records, unions, pattern matching, options, results, collections, active patterns and async, plus a self-assessment checklist and what to learn next.