ZetCode

F# code blocks

last modified December 2, 2026

In this article we look at how F# groups code into blocks with indentation, and at the rules for laying out lines inside a block.

Many languages mark blocks with curly brackets or with keywords such as begin and end. F# uses indentation instead, like Python. The rules that decide where a block starts and ends are called the offside rule. The keywords that make the structure explicit still exist; they are covered in F# verbose syntax.

A block is indented

A block starts on the line after the equals sign of a binding and is indented further than the line that starts the binding. All lines of the block begin in the same column.

block.fsx
let message =
    let first = "Hello"
    let second = "there"
    first + " " + second + "!"

printfn "%s" message

The value of message is computed by a block of three lines.

let message =
    let first = "Hello"

The block is indented by four spaces, which is the usual convention. Any number of spaces works, as long as all lines of the block use the same one. The last line of the block gives its value; the local bindings in a block are covered in F# let bindings.

printfn "%s" message

The line with printfn starts in the first column again. This ends the block, and the line belongs to the top level of the script.

λ dotnet fsi block.fsx
Hello there!

The offside line

The column in which the first token of a block starts is the block's offside line. The block does not have to start on a new line; it may start right after the equals sign.

offside.fsx
let area = let width = 3
           let height = 4
           width * height

let perimeter =
  let width = 3
  let height = 4
  2 * (width + height)

printfn "area: %d, perimeter: %d" area perimeter

The two bindings use different layouts.

let area = let width = 3
           let height = 4
           width * height

The block of area starts on the same line as the binding. Its first token, let width, sets the offside line in column 12, and the following lines are aligned to it. If the name were changed to a longer one, every line of the block would have to move; this is why the layout with the block on a new line is more common.

let perimeter =
  let width = 3

The block of perimeter is indented by two spaces. The amount of indentation does not matter, only the alignment of the lines.

λ dotnet fsi offside.fsx
area: 12, perimeter: 14

Lines left of the offside line

A line inside a block may not start to the left of the block's offside line. Such a line ends the block too early.

misaligned.fsx
let total =
    let price = 25
    let quantity = 3
   price * quantity

printfn "total: %d" total

The last line of the block starts one column too far to the left.

    let quantity = 3
   price * quantity

The block of total ends before price * quantity, so its last line is the let quantity binding. A block must end with an expression that gives its value, and the compiler reports it. The path of the file is shortened in the output.

A line indented further than the others is an error too, usually FS0010: Unexpected keyword, because the compiler reads it as the continuation of the previous line. Tab characters are not allowed for indentation at all; a tab gives error FS1161: TABs are not allowed in F# code. Editors for F# insert spaces when the Tab key is pressed.

λ dotnet fsi misaligned.fsx
misaligned.fsx(3,5): error FS0588: The block following this 'let' is unfinished. Every code block is an expression and must have a result. 'let' cannot be the final code element in a block. Consider giving this block an explicit result.

Continuing a long expression

An expression that does not fit on one line can continue on the next lines. The continuation lines are indented further than the start of the expression.

continuation.fsx
let subtotal =
    1200.0 * 0.8
    + 340.0
    - 90.5

let total =
    subtotal *
        1.2

printfn "subtotal: %.2f, total: %.2f"
    subtotal
    total

let label =
    "invoice "
    + "2026-"
    + "0042"
printfn "%s" label

The program splits arithmetic, a call of printfn, and a string concatenation over several lines.

let subtotal =
    1200.0 * 0.8
    + 340.0
    - 90.5

A line that starts with an infix operator, such as + or -, continues the expression on the line above. The offside rule makes an exception for these operators: they may start in the block's column, or even a little to the left of it, so that the operands stay aligned.

let total =
    subtotal *
        1.2

When the operator ends the line, the expression is not complete, and the next line must be indented further. Putting the operator at the start of the line is the more common style.

printfn "subtotal: %.2f, total: %.2f"
    subtotal
    total

The arguments of a call can be moved to the next lines when they are indented further than the name of the function.

λ dotnet fsi continuation.fsx
subtotal: 1209.50, total: 1451.40
invoice 2026-0042

Nested blocks

A binding inside a block can have a block of its own. Each level of nesting has its own offside line. Blank lines and comments inside a block are ignored by the offside rule.

nested.fsx
let report =
    let items = 3
    let unitPrice = 12.5

    // the discount has its own block
    let discount =
        let ratePerItem = 0.04
        let bonus = 0.02
        ratePerItem * float items + bonus

    let cost = float items * unitPrice * (1.0 - discount)
    sprintf "%d items, discount %.0f%%, cost %.2f" items (discount * 100.0) cost

printfn "%s" report

The block of report contains the nested block of discount.

    let discount =
        let ratePerItem = 0.04
        let bonus = 0.02
        ratePerItem * float items + bonus

The inner block has its offside line four columns to the right of the outer one. When the next line returns to the column of let discount, the inner block ends and the outer one continues. The blank line and the comment before the inner block do not affect it. The %% specifier prints a percent sign.

λ dotnet fsi nested.fsx
3 items, discount 14%, cost 32.25

Blocks in parentheses

Parentheses can enclose a block as well. Inside them, the closing parenthesis may go back to the column of the line that opened it.

parens.fsx
let shipping = (
    let weight = 2.5
    let pricePerKilo = 4.0
    weight * pricePerKilo
)

let handling = (let fee = 1.5 in fee * 2.0)

printfn "shipping: %.2f, handling: %.2f" shipping handling
printfn "total: %.2f" (
    shipping
    + handling
)

Two values are computed in parenthesized blocks, and a third block is the argument of printfn.

let shipping = (
    let weight = 2.5
    let pricePerKilo = 4.0
    weight * pricePerKilo
)

The opening parenthesis ends the first line, the block is indented, and the closing parenthesis returns to the first column. This layout is common for long arguments and for lists, which are written in square brackets.

let handling = (let fee = 1.5 in fee * 2.0)

A short block fits on one line with the let ... in form.

λ dotnet fsi parens.fsx
shipping: 10.00, handling: 3.00
total: 13.00

An applied example

The last example computes the price of a train ticket. It uses nested blocks, a block in parentheses, and expressions continued over several lines.

ticket.fsx
let distance = 420.0
let pricePerKm = 0.09
let passengers = 3

let ticket =
    let basePrice = distance * pricePerKm

    // a surcharge for the kilometres over 300
    let surcharge =
        let extraKm = distance - 300.0
        let extraRate = 0.5
        extraKm * pricePerKm * extraRate

    basePrice + surcharge

let groupPrice = (
    let discount = 0.2
    float passengers
    * ticket
    * (1.0 - discount)
)

printfn "ticket: %.2f EUR"
    ticket
printfn "group of %d: %.2f EUR"
    passengers
    groupPrice

A ticket for 420 km costs 9 cents per km. The kilometres over 300 cost 50 percent more, and groups get a discount.

let groupPrice = (
    let discount = 0.2
    float passengers
    * ticket
    * (1.0 - discount)
)

The group price is computed in a parenthesized block. Its last expression is spread over three lines that start with the * operator.

λ dotnet fsi ticket.fsx
ticket: 43.20 EUR
group of 3: 103.68 EUR

Source

F# style guide: Code formatting guidelines

F# language reference: Verbose syntax

In this article we have formed blocks with indentation, looked at the offside line, misaligned lines and tabs, continued long expressions, nested blocks, and put blocks in parentheses. The same rules apply to the bodies of functions, conditionals and loops. The keywords that make blocks explicit are covered in F# verbose syntax.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all F# tutorials.