F# variables
last modified December 2, 2026
In this article we bind names to values and work with mutable variables in F#.
A variable is a name that refers to a value. In most languages a
variable is a storage location whose content can be overwritten at any time.
F# works the other way round: a name is bound to a value with the
let keyword, and the binding cannot change. F# calls these
names values. When a value has to change, we declare it with the
mutable keyword.
Binding values
The let keyword binds a name to a value. The name can then be
used anywhere below the binding.
let city = "Bratislava" let population = 475000 let founded = 907 printfn "%s has %d inhabitants" city population printfn "It was first mentioned in %d" founded
We bind three values and print them.
let city = "Bratislava" let population = 475000
The name is on the left of the equals sign and the value on the right. We do
not write the types; F# infers that city is a
string and population an int.
λ dotnet fsi values.fsx Bratislava has 475000 inhabitants It was first mentioned in 907
Values computed from expressions
The right side of a binding may be any expression. It is evaluated once, when the binding is reached, and the name refers to the result.
let width = 12 let height = 5 let area = width * height let perimeter = 2 * (width + height) printfn "width %d, height %d" width height printfn "area: %d" area printfn "perimeter: %d" perimeter
The area and the perimeter of a rectangle are computed from two other values.
let area = width * height let perimeter = 2 * (width + height)
The area value holds the number 60, not the formula. Since
width and height cannot change, area
can never become stale. The operators are covered in
F# operators.
λ dotnet fsi expressions.fsx width 12, height 5 area: 60 perimeter: 34
Values cannot be changed
A value bound with let is immutable. Trying to assign a new value
to it is a compile error.
let speed = 50 printfn "speed: %d" speed speed <- 60 printfn "speed: %d" speed
The program tries to change the speed with the assignment operator
<-.
speed <- 60
The compiler rejects the line and suggests the mutable keyword.
The script is compiled as a whole before it runs, so not even the first
printfn is executed. The path of the file is shortened in the
error messages of this article.
λ dotnet fsi immutable.fsx immutable.fsx(4,1): error FS0027: This value is not mutable. Consider using the mutable keyword, e.g. 'let mutable speed = expression'.
The equals sign does not assign
Outside of a let binding, the = sign compares two
values. It is a common mistake to write it to change a value.
let speed = 50 speed = 60 printfn "speed: %d" speed printfn "speed = 60 is %b" (speed = 60) printfn "speed = 50 is %b" (speed = 50)
The third line looks like an assignment, but it is a comparison.
speed = 60
The line computes false and throws the result away; the value
of speed stays 50. In a script the result is discarded silently.
In a compiled project, the compiler warns that the result of the expression
is implicitly ignored.
printfn "speed = 60 is %b" (speed = 60)
The %b placeholder prints a Boolean. The comparison is in
parentheses because it is a single argument of printfn.
λ dotnet fsi comparison.fsx speed: 50 speed = 60 is false speed = 50 is true
Mutable variables
A binding declared with let mutable is a variable whose value
can be replaced. The new value is assigned with the <-
operator.
let mutable score = 0 printfn "score: %d" score score <- score + 10 printfn "score: %d" score score <- score * 3 printfn "score: %d" score
A score starts at zero and is changed twice.
let mutable score = 0
The mutable keyword comes after let. A mutable
variable must be given an initial value, like any other binding.
score <- score + 10
The right side is evaluated first, using the current value of
score, and the result replaces the old value. When to prefer
mutable variables and how to avoid them is the subject of
F# mutability.
λ dotnet fsi mutable.fsx score: 0 score: 10 score: 30
A variable has one type
The type of a variable is fixed when it is declared. A mutable variable can receive a new value, but only of the same type.
let mutable level = 1 printfn "level: %d" level level <- 2 printfn "level: %d" level level <- "expert" printfn "level: %d" level
The level is an integer; then we try to store a string in it.
let mutable level = 1
The type is inferred from the initial value: level is an
int. The type can also be written explicitly, as in
let mutable level: int = 1; type annotations are covered in
F# data types.
level <- "expert"
The assignment does not compile. The error points to line 7, column 10, where the string starts.
λ dotnet fsi one_type.fsx
one_type.fsx(7,10): error FS0001: This expression was expected to have type
'int'
but here has type
'string'
Names
A name starts with a letter or an underscore and continues with letters, digits, underscores and apostrophes. Names are case-sensitive. Values and variables are usually written in camelCase.
let firstName = "Lucia" let item2 = "pencil" let max_size = 100 let total' = 250 let ``unit price`` = 12 let name = "Lucia" let Name = "Peter" printfn "%s, %s, %d" firstName item2 max_size printfn "%d, %d" total' ``unit price`` printfn "%s and %s" name Name
The program binds values with several kinds of names.
let total' = 250 let ``unit price`` = 12
An apostrophe may appear in a name; it is often used for a modified version
of a value, read as "total prime". A name between double backticks may
contain spaces and other characters. Backticks also allow keywords as names:
let type = "triangle" fails with error FS0010: Unexpected
keyword 'type' in binding, while let ``type`` = "triangle"
compiles.
let name = "Lucia" let Name = "Peter"
Since names are case-sensitive, these are two different values. Names that start with an uppercase letter are used for types, modules and record fields.
λ dotnet fsi naming.fsx Lucia, pencil, 100 250, 12 Lucia and Peter
Source
F# language reference: Keyword reference
In this article we have bound names to immutable values, changed mutable
variables with the <- operator, and looked at the rules for
names. The types of values are covered in
F# data types, and local bindings and
shadowing in F# let bindings.
Author
List all F# tutorials.