Introduction to F#

F# Introduction

F# is a multi-paradigm .NET language explicitly designed to be an ML suited to the .NET environment. or, F# is a functional programming language that runs on Microsoft's Common Language Runtime and the .NET Framework. F# programming is basically a combination of three Functional Programming as well as Imperative and Object Oriented Programming Concepts. The first paradigm is Functional programming. The second paradigm is widely adopted Object oriented programming. The third paradigm supported by F# is Language oriented programming.

Data Types In F#

Here, we will describes the types that are used in F#.

Primitive Types in F# ( Fundamental Data Types of F#)

Some types are considered primitive types, such as the Boolean type bool and integral and floating point types of various sizes, which include types for bytes and characters. these are also called Fundamental Data Types of F#.

The below table defines the Primitive Types of F#.

Data Type NameF# typeExampleSize
Shortint1610s2 byte with a sign bit
Unsigned Shortuint1680us2 byte with no sign bit
Unsigned intuint32456u4 byte with no sign bit
Unsigned longuint649999999ul8 byte with no sign bit
Singlefloat32 or single2.0f, 2.02f, 2.02e204 byte with a sign bit
Native intnativeint456nSize is platform specific
Unsigned Native intunativeint456unSize is Platform specific
Decimal decimal456mMaps to System.Decimal
Big numberbignum456IMaps to Microsoft.FSahrp.Math.BigInt

Other Data Types of F#

These data type are the built in data types in the language, which includes tuples, lists, arrays, sequences, records, and discriminated unions.

Tuples

A tuple is defined as a comma separated collection of values. For example, (8, "hi") is a 2-tuple with the type (int * string). Tuples are extremely useful for creating ad hoc data structures which group together related values. or, A tuple is a grouping of unnamed but ordered values, possibly of different types.

For example

Tuple of two integers.

(5, 6) 
Names of Tuple Types
When you write out the name of a type that is a tuple, you use the * symbol to separate elements. For a tuple that consists of two int,
such as (5,6) the type would be written as follows.
int *int 
Triple of strings

("Mical","Manoj","Gray")
Names of Tuple Types
String* String*string*
Tuple that has mixed types.
( "one", "1", "2.0" ) 
Names of Tuple Types
String*int*float 

Records

A record is similar to a tuple, except that it contains named fields. F# record types are a simple named type. You can create one or more variables in a record. When you need to access the record you can access the complete record or one of its members individually.

The syntax to create a record is:

type recordName = { [ fieldName : dataType ] + }
Example:
type website ={ Title : string; Url : string; view : int }

Lists

List data types are ordered, immutable series of elements all of the same type. 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. 

Example 

let list456 = [ 4; 5; 6 ]

Property of the list

The below example defines the Properties of the List:

let definelist = [ 4; 5; 6 ]

// Length Properties

printfn "list1.Length is %d" (definelist.Length)

// Head Properties

printfn "list1.Head is %d" (definelist.Head)

// Tail Properties

printfn "list1.Tail.Head is %d" (definelist.Tail.Head)

// Item Properties

printfn "list1.Item(1) is %d" (definelist.Item(1))

OUTPUT

F31.gif

Discriminated union

Discriminated unions are used to create well defined type hierarchies and hierarchical 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 an Abstract Syntax Tree. This data type is useful for heterogeneous data. This data type has a finite number of distinct alternative representations. F# discriminated unions are type safe and work with Pattern matching.

Example

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"

Sequence

A sequence is a logical series of elements all of one type. Sequences are particularly useful when you have a large, ordered collection of data but do not necessarily expect to use all the elements. Individual sequence elements are computed only as required, so a sequence can provide better performance than a list in situations in which not all the elements are used.

For example:

The simplest form specifies a range. For example, seq { 2 .. 10 } creates a sequence that contains five elements, including the endpoints 2 and 10. You can also specify an increment (or decrement) between two double periods. For example, the following code creates the sequence of multiples of 20:

seq { 0 .. 20 .. 100 }
Variables
To declare a variable, use the let operator, followed by a name for the variable and assign a value to it using the assignment operator "=". The Syntax 
for declaring a Variable is:
Defining a integer variable 

let a = 5;
Example

let a = 5;

printfn "%d"a;

OUTPUT

F32.gif
Example

let x = 6

let y = 5

let z = x* y

printfn "a: %i" x

printfn "b: %i" y

printfn "c: %i" z

OUTPUT

F33.gif


Similar Articles