Home

F# Collections

 

Fundamentals of Lists

 

Introduction

A list is an immutable series of items. The items must be arranged in order based on their types:

The word immutable means that once the list is created, it cannot change (we will see that you can add new items to the list but then you get a new list, the original one stays "as is").

Introducing to Creating a List

To create a list, start with an opening square bracket "[" and end with a closing square bracket "]". An empty list is one that doesn,t have any item, but it is still considered a list. To create an empty list, use the square brackets but leave them empty. Here is an example:

[]

Otherwise, inside the brackets, give the values of the list, separated by semicolons. Here are examples:

// Numbers
[ 12; 22; 48; 115; 306 ];
// Characters
[ 'c'; 'd'; 'g'; 'j'; 'm' ];
// Names
[ "Frank"; "James"; "Joshua"; "Mary" ];

You don't have to write all items on the same line. In fact, you can write the square brackets on their own lines and the items on a line after the opening square bracket. Here is an example:

[
"Frank"; "James"; "Joshua"; "Mary"
];

Althoug you don't have to indent, indentation makes the list easier to read:

[
    "Frank"; "James"; "Joshua"; "Mary"
];

Another alternative is to have each member of the list on its own line. Here is an example:

[
    "Frank";
    "James";
    "Joshua";
    "Mary"
];

If you decide to write each item on its own line, you can omit the semicolons. Here are examples:

[
    "Frank"
    "James"
    "Joshua"
    "Mary"
];

Creating a Range

A list is a series of arranged items. If you use the items that are known in advance and they are consecutive, you can specify only the beginning and end of the series. When creating the list, in the square brackets, type the starting item, followed by .., and followed by the ending item. It is a good idea to put a space before and after .. to make it easier to read. Here are examples:

// A range of numbers
[ 2 .. 8 ];
// A range of characters
[ 'h' .. 'q' ];

Naming a List

A list is a kind of variable. Although you can use it withoug naming it, if you plan to refer to refer to it in different parts of your code, you should give it a name. To do this, use let followed by a name and assign the list to it. Here is an example:

let numbers = [ 12; 22; 48; 115; 306 ];

Remember that the items can be separated from the square brackets by different lines:

let characters = [
    'c'; 'd'; 'g'; 'j'; 'm'
];

 

  

Using Lists

 

Introduction

To present the list of items to the console, you can call the printfn() function and use %A as the placeholder. Here is an example:

printfn "%A" [ "Frank"; "James"; "Joshua"; "Mary" ];

This would produce:

["Frank"; "James"; "Joshua"; "Mary"]
Press any key to continue . . .

You can also write the items on different lines. Here is an example:

printfn "%A" [
    "Frank"
    "James"
    "Joshua"
    "Mary"
];

Adding an Item to a List

As mentioned already, a list is immutable. As an alternative, you can add a new item to the list but you would get a new, different list, that contains the original items and the new one. To perform this operation, use the let operator and a new name, assign the new value to it, followed by :: and the original list. Here is an example:

// Names
let names = [ "Frank"; "James"; "Joshua"; "Mary" ];

printfn "%A" names;

let additional = "Gabriel" :: names;
printfn "%A" additional;

This would produce:

["Frank"; "James"; "Joshua"; "Mary"]
["Gabriel"; "Frank"; "James"; "Joshua"; "Mary"]
Press any key to continue . . .

Keep in mind that the original list has not changed:

// Names
let names = [ "Frank"; "James"; "Joshua"; "Mary" ];

printfn "%A" names;

let additional = "Gabriel" :: names;
printfn "%A" additional;

printfn "%A" names;

This would produce:

["Frank"; "James"; "Joshua"; "Mary"]
["Gabriel"; "Frank"; "James"; "Joshua"; "Mary"]
["Frank"; "James"; "Joshua"; "Mary"]
Press any key to continue . . .

Concatening Some Lists

Concatening two list consists of adding them. To perform this operation, use the @ operator in place of +. Here is an example:

let parents = [ "Frank"; "James"; "Joshua"; "Mary" ];
let students = [ "Hermine"; "Sebastien"; "Lester" ];

printfn "Parents:  %A" parents;
printfn "Students: %A" students;

printfn "\nFamilies: %A"  (parents @ students);

This would produce:

Parents:  ["Frank"; "James"; "Joshua"; "Mary"]
Students: ["Hermine"; "Sebastien"; "Lester"]

Families: ["Frank"; "James"; "Joshua"; "Mary"; "Hermine"; "Sebastien"; "Lester"]

Press any key to continue . . .

If you plan to use the result many times, you can store it in a variable. To do this, assign the concatenation to the variable. Here is an example:

let parents = [ "Frank"; "James"; "Joshua"; "Mary" ];
let students = [ "Hermine"; "Sebastien"; "Lester" ];
let families = parents @ students;

printfn "Parents:  %A" parents;
printfn "Students: %A" students;

printfn "Families: %A" families;

 

 

 

  

Fundamentals of Tuples

 

Introduction

A tuple is a list of values and that list is ordered. The values can be numbers, characters, or strings. The members of the list can also consist of expressions, etc:

Creating a Tuple

To create a tuple, use the parentheses. Inside the parentheses, list the values separated by commas. Here is an example:

// Integrals
( 1, 4, 6, 12, 36 );
// Characters
( 'b', 'd', 'e', 'g' );
// Strings
( "Aaron", "Jennifer", "Yerimah" );

Not all members have to be of the same type. That is, you can create a tuple whose members are of different type. Just make sure that the value you give to a member makes it clear to the compiler what type of value it is. Here is an example of a tuple that contains an integer, a string, and a character:

(937404, "Bertha Herlin", 'F' );

The items in the tuple can also derive from previously declared and initialized variables. Here is an example:

let nbr1 = 16;
let nbr2 = 24;
let nbr3 = 58;

( nbr1, nbr2, nbr3 );

Naming a Tuple

If you plan to use a tuple many time, you should store it in a variable. When declaring the variable, use the let operator and assign the tuple to it. Here is an example:

let numbers = ( 1, 4, 6, 12, 36 );

As mentioned in the first lesson, F# is an infered language: the compiler is able to figure the type of a variable based on the value you assign to it. This also applies to tuples. When you create a tuple, once you specify its members, the compiler applies the appropriate type to each member. Still, if you want, you can indicate the data type of each member. To do this, after name of the tuple, type : followed by the type of each member; the types are separated by *. Here is an example:

let numbers : int * int * int * int * int = ( 1, 4, 6, 12, 36 );

If the items are of different types, enter the appropriate type of a member in the corresponding placeholder. Here is an example:

let studentInfo : int * string * char = ( 937404, "Bertha Herlin", 'F' );

 

 

Using a Tuple

 

Introduction

A tuple is primarily a list. The primary way you can use it consists of displaying its values to the user. To do this from the printfn() function, use the %A placeholder. Here is an example:

printfn "%A" ( 1, 4, 6, 12, 36 );

This would produce:

(1, 4, 6, 12, 36)
Press any key to continue . . .

If the tuple is stored in a variable, use the same %A in the printfn() function and pass the name of the the variable as the second argument. This can be done as follows:

let numbers = ( 1, 4, 6, 12, 36 );
printfn "%A" numbers;

 

 

Previous Copyright © 2010-2011 FunctionX Next