liveBook · Manning

Indentation

As explained in F# in Action: A Revised Edition of Get Programming with F#

Indentation is leading whitespace in source code that can delimit or structure blocks according to a language's syntax rules.

In F#, indentation is syntax, not decoration. Indenting code creates a scope, which controls where names are accessible and how long they remain available. This removes much of the brace-and-semicolon boilerplate used by more verbose styles while keeping the structure visible. section 3.1.1 section 3.4

A function body must be indented farther than its definition. Nested bindings remain inside that body, while the final indented expression supplies the function result. Align related expressions consistently so that the intended scope is immediately clear. section 3.1.1 section 3.4

Listing 3.1 F# syntax basics
let addTenThenDouble theNumber =                     #1
    let addedTen = theNumber + 10                    #2
    let answer = addedTen * 2                        #3
    printfn $"({theNumber} + 10) * 2 is {answer}"
    let website = System.Uri "https://fsharp.org"    #4
    answer                                           #5

The nested bindings, output call, object creation, and final result are all indented beneath the function definition, placing them in one function scope without braces or explicit return syntax. section 3.1.1

A small indented function

let greetPerson name age = $"Hello, {name}. You are {age} years old" The greeting expression is indented under greetPerson, so it forms the function body. Use spaces or tabs, but ensure the body begins at least one space beyond the definition; four spaces is a common convention. section 2.5.5

When writing indented F# code:

  • Place the body of each function farther right than its definition. section 2.5.5
  • Use indentation to keep local let bindings within the scope that needs them. section 3.4
  • Prefer the concise layout when types, braces, semicolons, and return statements add no needed meaning. section 3.1.1

How F# in Action: A Revised Edition of Get Programming with F# covers it

How deep it goes
Explains indentation through function and local-binding layout, without detailing parser rules or complex nested constructs.
Examples
Demonstrates an arithmetic function with local bindings and a greeting function executed in F# Interactive.
What it assumes
Teaches function-body indentation from zero while using simple F# function definitions and script execution.
What you can do after
Apply it via F# scripts to structure function bodies and limit local symbol visibility.
How it connects
Connects indentation with scope, immutable let bindings, type inference, and F# Interactive.

Also explained in

Compare how 6 books explain indentation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of F# in Action: A Revised Edition of Get Programming with F#. The section links point to the parts of the book it draws on.