Creating list ranges in F#

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.

Advertisement

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: