Creating list ranges in F#
March 12, 2017 Leave a comment
We can easily create a list of integers in F# with a start and end value as follows:
let myRangeList = [1..10]
myRangeList will contain the integers from 1 to 10:
int list = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]
We can also add a step parameter in the middle to increment the values by some number:
let myRangeList = [1..3..10]
myRangeList will contain the values [1; 4; 7; 10].
If the step variable is 2 then the last element in the list will be 9 since 11 is more than the top value 10:
let myRangeList = [1..2..10]
int list = [1; 3; 5; 7; 9]
We can also create a list of floats:
let myFloatList = [2. ..0.3 .. 8.]
myFloatList will contain the following elements:
float list =
[2.0; 2.3; 2.6; 2.9; 3.2; 3.5; 3.8; 4.1; 4.4; 4.7; 5.0; 5.3; 5.6; 5.9; 6.2;
6.5; 6.8; 7.1; 7.4; 7.7; 8.0]
View all F# related articles here.