F# Cheat Sheet
Core syntax for F#, covering bindings, functions, pipelines, discriminated unions, and pattern matching on .NET.
2 PagesIntermediateMar 25, 2026
Values & Functions
Immutable bindings and function definitions.
fsharp
// Values and functionslet x = 42 // immutable bindinglet name = "SkillVeris"let add a b = a + b // function definitionlet square x = x * xprintfn "Sum: %d" (add 2 3) // formatted printlet mutable counter = 0 // explicitly mutablecounter <- counter + 1
Pipelines & Composition
The pipe operator and function composition.
fsharp
let isEven n = n % 2 = 0[1..10]|> List.filter isEven|> List.map (fun n -> n * n)|> List.sum|> printfn "Result: %d"// Function composition with >>let addThenDouble = (+) 1 >> (*) 2
Core Types
F#'s main type-system building blocks.
- record- Immutable named-field type: type Point = { X: int; Y: int }
- discriminated union- Sum type: type Shape = Circle of float | Square of float
- option- Represents an optional value: Some 5 or None
- tuple- Fixed-size grouping: let pair = (1, "a")
- unit- Type with a single value (), analogous to void
- list vs array- list: T list (immutable, linked); array: T[] (mutable, fixed size)
Pattern Matching
Matching on unions and options.
fsharp
type Shape = | Circle of radius: float | Rectangle of width: float * height: floatlet area shape = match shape with | Circle r -> System.Math.PI * r * r | Rectangle (w, h) -> w * hmatch Some 5 with| Some x when x > 0 -> printfn "positive %d" x| Some _ -> printfn "non-positive"| None -> printfn "none"
Records & Discriminated Unions
Immutable data modeling with records and DUs.
fsharp
type Point = { X: float; Y: float }let p = { X = 1.0; Y = 2.0 }let p2 = { p with Y = 5.0 } // copy-and-updatetype Shape = | Circle of radius: float | Rectangle of width: float * height: floatlet area shape = match shape with | Circle r -> System.Math.PI * r * r | Rectangle (w, h) -> w * h
Async Workflows
Concurrency with async and Async.Parallel.
fsharp
let fetchAsync url = async { use client = new System.Net.Http.HttpClient() let! html = client.GetStringAsync(url) |> Async.AwaitTask return html.Length}let results = [ "https://a.com"; "https://b.com" ] |> List.map fetchAsync |> Async.Parallel |> Async.RunSynchronously
Collection Functions
Common List/Seq/Array module operations.
- List.map- apply a function to every element, returning a new list
- List.filter- keep only elements matching a predicate
- List.fold- accumulate a result left-to-right from an initial state
- List.choose- map then keep only the Some results, dropping None
- List.collect- map each element to a list and concatenate (flatMap)
- Seq.groupBy- group elements by a key selector into (key, seq) pairs
- List.sortBy- sort by a projected key
Option & Result Handling
Railway-oriented error handling with Option and Result.
fsharp
let tryParse (s: string) = match System.Int32.TryParse s with | true, v -> Some v | _ -> Nonelet divide a b = if b = 0 then Error "divide by zero" else Ok (a / b)let result = divide 10 2 |> Result.map (fun x -> x + 1) |> Result.mapError (fun e -> $"failed: {e}")
Pro Tip
Use |> liberally to keep data-transformation pipelines readable left-to-right instead of nesting function calls inside-out.
Was this cheat sheet helpful?
Explore Topics
#FCheatSheet#Programming#Intermediate#ValuesFunctions#PipelinesComposition#CoreTypes#PatternMatching#Functions#DevOps#CheatSheet#SkillVeris