ZetCode

F# tutorials

F# is a functional-first programming language for the .NET platform. Values are immutable by default, functions are first-class, and the compiler infers most types, so programs stay short while remaining statically typed. The language has records, discriminated unions, options, pattern matching, and pipelines, and because it runs on .NET it can use every .NET library and mix functional code with objects and mutable state where that is the better fit. F# is used for data processing, financial and scientific computing, web services, and scripting.

F# toolchain

F# is part of the .NET SDK; installing the SDK provides the compiler and F# Interactive. The examples in these tutorials are F# scripts with the .fsx extension, run with dotnet fsi main.fsx, so no project file is needed. Larger programs are created as projects with dotnet new console -lang F# and built with dotnet run.

Language basics

The introduction installs the .NET SDK, uses F# Interactive, and runs the first scripts and projects. Data types covers integers, floats, decimals, Booleans, characters and strings, and operators covers arithmetic, comparison, Boolean and bitwise operators and their precedence. Variables introduce immutable values, mutable variables, and naming rules. Let bindings cover local bindings in blocks, let ... in, shadowing, lazy values, and literals, and do bindings run code for its side effects and discard unneeded results with ignore. Print functions covers printfn, printf, format specifiers, sprintf, eprintfn, and Console.WriteLine. Expressions shows that almost everything in F# is an expression with a value and a type, and in what order expressions are evaluated, and code blocks explains how indentation forms blocks.

Control flow and pattern matching

Conditionals use if, elif, and else as an expression that returns a value. Loops cover for...to, for...in, and while, nested loops, and ending a loop early, and ranges generate stepped, character, and floating-point values as lists, arrays, and sequences and slice strings. The match expression chooses between cases with constants, the wildcard, or-patterns, variable patterns, when guards, and literals.

Functions

Functions covers parameters, type annotations, function types, unit functions, currying, partial application, and functions as values. Lambdas are anonymous functions passed to higher-order functions, bound with let, and used as closures. Pipelines and composition chain functions with |>, <|, >> and << and define custom operators, and recursion covers let rec, accumulators, tail calls, and mutual recursion. Variable scope covers local values, nested functions, shadowing, and closures, and type inference explains how the compiler works out the types of values and functions and when it makes them generic.

Types

The unit type has the single value () and appears in functions that are called for their side effects and in actions passed as unit -> unit functions, and the option type represents a value that may be missing without using null, with Option.map, Option.bind, and default values. Tuples group values without declaring a type and return several values from a function, while records have named fields, structural equality, copy-and-update expressions, patterns, and members, and anonymous records need no type definition. Discriminated unions describe values that are one of several cases, with data, recursion, and type parameters, and the Result type returns Ok or Error and chains validations with Result.bind. Active patterns define custom patterns for match: single-case, multi-case, partial, and parameterized ones. Struct types are value types: struct records, tuples, and unions, and the voption type; enums name integer constants and bit flags. The obj type is the base of all .NET types, with type tests and downcasts, and boxing and unboxing shows what converting value types to obj costs. Mutability shows mutable variables in loops and lambdas, mutable record fields, and their immutable alternatives. Reference cells hold mutable state that can be passed to functions, shared by closures, and stored in records, and copy by value and reference explains which types are copied and which are shared.

Collections

Lists are immutable linked lists and arrays are fixed-size mutable collections; both articles iterate, map, filter, merge, and sort elements. Sequences are lazy and can be infinite. Maps store key-value pairs and sets hold unique values with union, intersection, and difference. List comprehensions build collections with yield and yield!, query expressions filter, sort, and group data in a LINQ-like style, and sort orders lists of numbers, strings, tuples, and records by one or several keys.

Strings and text

Strings covers conversion to numbers, escape sequences, verbatim strings, searching, and building strings. String interpolation embeds typed values and expressions with format specifiers, alignment, and padding, and regular expressions match, search, and capture text with the .NET Regex class and active patterns.

Program structure

Modules and namespaces group functions and types, control access with private, RequireQualifiedAccess, and AutoOpen, and organize the files of a project. Exceptions covers try...with, try...finally, failwith, raise, custom exceptions, and use, and classes define objects with constructors, methods, properties, and static members. Verbose syntax writes the same code with explicit keywords such as let ... in, begin ... end, and done instead of indentation.

Utilities

Random generates random numbers, seeds the generator, picks random elements, and builds random sequences.

last modified December 2, 2026