liveBook · Manning

Mutate operator

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

The mutate operator is F# syntax using <- to update a mutable binding or location.

The mutate operator is F#’s <- syntax for updating a mutable binding. F# reserves = for equality comparison, separating mutation from comparison and making immutable values the shorter default. section 4.2.3

let name = "isaac"     #1
let mutable age = 42   #2
age <- 43              #3
if age = 43 then ...   #4

Compare familiar assignment and equality syntax with F#’s explicit mutable binding, <- update, and = comparison. section 4.2.3

Updating a mutable gas tank

Declare mutable state before using the operator: let mutable gas = 100.0. A drive function can then update it with gas <- gas / 2.0, gas <- gas - 10.0, or gas <- gas - 1.0 depending on the distance. section 4.2.3

Use <- when modeling changing state directly. For immutable data, prefer a function that returns the next value, such as passing each gas state into drive and binding the resulting states. section 4.2.3

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

How deep it goes
Defines <-, contrasts it with =, and demonstrates mutable and immutable gas-tank implementations.
Examples
Uses variable comparisons and a gas-tank simulation with far, medium, and near driving distances.
What it assumes
Assumes familiarity with assignment and equality comparison while introducing their distinct F# operators.
What you can do after
Apply it in F# to update mutable bindings and distinguish mutation from equality checks.
How it connects
Connects mutation to mutable data, immutable data, assignment, and equality comparison.

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.