Data Types in F#

Data Types In F#

Data types provide a way to handle Data Elements. When you relate an element with a Data type you are giving a specific range of values to that particular Element. F# programming uses an Immutable Data Type. If you know the difference between String and StringBuilder in .Net than you can easily understood that a String is an immutable data type and StringBuilder is a mutable type. When you make changes in a string, each change causes a new Object. F# contains two types of Data Types as mutable and immutable types. Mutable data types (Fundamental Data Types) are the same as what .Net contains. The new data types that F# has are Immutable Types. F# is a strongly typed Language. Every expression, value and data element contains a well defined and valid type which is fixed at the time of source code compilation.

Fundamental Data Types



Immutable Data Types- F# includes Functional Programming component supporting eager evaluation and for functional programming it supports a set of immutable data types such as Tuples, Discriminated union, Records and Lists .


Immutability means that the memory associated with an identifier, once assigned is fixed and can not be changed. F# is not a pure functional programming language and it enables value mutability by the mutable keyword.

If you want to define a type that is modifiable you can give the name of the identifier with mutable keyword.

let mutable a=5

You can change the mutable value by (<-) destructive assignment operator:

a <- 6

When you print the value of "a" it will print the changed value.


let mutable a=5
a <- 6
printfn "%d"a


Immutable Data Types are


Tuples are ad hoc data types that can be used to group two or more related values into a single Object. These are written as comma separated entities in F#. A Tuple is a simple type that groups multiple types of values. The length of the Tuple can be identified at the time of Compilation. Creating a Tuple value is very easy. You write a comma separated list of values enclosed in parentheses. On the first line we create a Tuple and assign it a tup value. Here we used the Type Inference mechanism of F#.


> let tup = ("Tuple Data Type!", 06)
val tp : string * int


> let tuple = (06, "Tuple Data Type");;
val tuple : int * string
> let (num, str) = tuple;;
val num : int
val str : string

Discriminated union

Discriminated unions are used to create well defined type hierarchies and hierarchal data types. This data type is used for showing a data type that can store one of several options. The example of a discriminated union is Abstract Syntax Tree. This data type is useful for heterogeneous data. This data type have a finite number of distinct alternative representations. F# discriminated unions are type safe and work with Pattern matching.


type area =
    | Right
    | Left
    | Top
    | Bottom
    member this.ToLatin() =
        match this with
        | Right -> "Wand"
        | Left -> "Coin"
        | Top -> "Cup"
        | Bottom -> "Sword"
> let card = Right;;
val card : Suit = Right
> card.ToLatin()
val it : string = "Wand"
> let card2 =Top
val card2 : Suit = Top
> card2.ToLatin()
val it : string = "Cup"


F# record types are a simple named type. A Record is a group of variable that are considered as one entity. You can create one or more variables in a record. When you need to access the record you can access the complete record or its member individually. The syntax to create a record is:

type RecordName= { Members }

To declare members you will use the following syntax:

MemberName: DataType

Student = { Rno : int; FName : string; LName : string; Gender : char }


Lists data types are used for storing a collection of values. A F# list is a typical linked list type. It can be either a empty list or a cell containing a value or a reference to the tail, which is itself a list. Lists in F# is an immutable and ordered series of elements.

let list456 = [ 4; 5; 6 ]

We can also define a list by putting Link Breaks between elements, in which a semicolon will be optional.

let list456 = [
6 ]

Now we are discussing example of some data types.



let age:byte = 21uy;
printf "Person Age: ";
printfn "%d" age;




let number = 0xFE;
printfn "Number: %d" number;


Signed Byte-


let roomTemperature = -82y;
printfn "When anyone will enter, the room temperature will  %d" roomTemperature;



In this article I covered all mutable and immutable data types.

Up Next
    Ebook Download
    View all
    View all