F# For and While Loop Examples: For To, For InLoop over ranges and collections with the for-to, for-in and while loops.
For, while. It starts at 0. Then it reaches 1 and then 2. A loop continues on. It does not care about us. It just wants to iterate again.
With for and while, we implement loops in the F# language. Loops are powerful, but functional-based designs are usually emphasized here.
This program shows 2 for-loops. Both of the constructs loop over the same values. In programs, either form can be used.
First: The first for-loop uses an iteration variable called i and goes from 0 through 3 inclusive.
Second: This loop uses range syntax. The two periods are part of an inclusive range of 0, 1, 2 and 3.
F# program that uses for loops
// Loop from 0 through 3 inclusive.
// ... The variable i is incremented by 1 each time.
for i = 0 to 3 do
printfn "For, to: %A" i
// This is the same loop but with a range.
for i in 0 .. 3 do
printfn "For, in: %A" i
For, to: 0
For, to: 1
For, to: 2
For, to: 3
For, in: 0
For, in: 1
For, in: 2
For, in: 3
A for-in loop enumerates over each element in a collection (like a list). Here we create a list of ints called "codes."List
Note: The for-in loop accesses all elements in the list. It provides no indexes, so the syntax is simpler.
F# program that uses for-in loop over list
let codes = [3; 12; 34; 3]
// Loop over ints from list.
for code in codes do
// If code is 3 then write a special word.
if (code = 3) then
// Write value of int from list.
printfn "%A" code
Downto. With this keyword we create a decrementing loop. After each iteration, the iteration variable (in this program "i") is reduced by 1.
F# program that uses for, downto
// Specify a decrement for-loop with downto.
for i = 10 downto 6 do
printfn "%A" i
The most versatile loop in F# is the while-loop. Here we can increment, decrement, or loop forever. We must be careful not to make mistakes.
Mutable: The iteration variable used as a counter in a while-loop should be marked as mutable so we can change it.
Decrement: We decrement the variable x by assigning it the current value minus 1.
F# program that uses while-loop
// Use an iteration variable that starts at 4.
// ... The variable must be mutable.
let mutable x = 4
// Continue looping while x is positive.
while x >= 0 do
// Write value.
printfn "%A" x
// Decrement x.
x <- x - 1
In the spec we find that a for-loop over a range is compiled to the same thing as a for-to loop. And we learn that this is a "primitive elaborated form" which is fun to say.
Quote: For-loops over ranges that are specified by variables are a primitive elaborated form. When executed, the iterated range includes both the starting and ending values in the range, with an increment of 1.F# Language Specification: fsharp.org
A review. In functional programs, recursion and method-based iteration is often preferred. But loops are still wonderful. In F# we can use iteration and increment variables.
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to firstname.lastname@example.org.