13,044,584 members (60,836 online)
Technical Blog
Add your own
alternative version

#### Stats

11.9K views
Posted 5 Dec 2011

# F# List

, 5 Dec 2011
 Rate this:
Please Sign up or sign in to vote.
F# List

Last week Tuples group values into a single entity. List allows you link data together to form a chain. F# defines a list as ; delimited values enclosed in brackets as:

`let countDown = [9;8;7;6;5;4;3;2;1];;`

F# has only two operations. They are (i) cons :: operator, to add an element in front of list (ii) append @ operator, to add at the end. Examples are:

```>let countDown = 10::countDown;;
val countDown : int list = [10;9;8;7;6;5;4;3;2;1]
>let countDown = 0@countDown;;
val countDown : int list = [10;9;8;7;6;5;4;3;2;1;0]```

### List Range

To declare a list of ordered numeric values, List range specifies the lower and upper range as:

```>let counter = [1..10];;
val counter : int list = [1;2;3;4;5;6;7;8;9;10]```

### List comprehension

It's a rich syntax that allows you to generate list inline with F# code. The body of the list comprehension will execute until it terminates, and the list will be made up of elements returned via `yield `keyword.

```let numbersNear x =
[
yield x-1
yield x
yield x+1
];;```

### List.map

`List.map `function creates a new collection by applying a function to the given collection. Just have a look at the attached image example.

When you print `r1 `the example, you should get the output as 2,3,4,5.

### List.Iter

It iterates through each element of the list and calls a function that you pass as a parameter.

## License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

## About the Author

 Architect India
Ganesan Senthilvel is a passionate IT leader with demonstrated 2 decades’ experience in architecture, design and implementing cutting edge solutions to address business opportunities of Enterprise applications. He earned Masters in Computer Science and Master in Business Administration. Now, he is pursing Doctorate program in Big Data. He is consistent technical contributor via COE, Blog, Whitepaper, Summit, Certification, etc. and also highly skilled at providing coaching and mentoring to internal teams and external institutes. Earned Microsoft Certifications like MCP, MCAD and financial domain certification.
He maintains his weekly blog at http://ganesansenthilvel.blogspot.in

## Comments and Discussions

 -- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 5 Dec 2011
Article Copyright 2011 by Ganesan Senthilvel
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid