Tuesday, November 29, 2011

F# Core Types


Besides the primitive types, the F# library includes several core types that will allow you to organize, manipulate and process data

Unit
The unit type is a value signifying nothing of consequence. unit can be thought of as a concrete representation of void and is represented in code via ():


> let x=();;

val x : unit=()

> x;;
val it : unit()

if expres­sions without a matching else must return unit because if they did return a value, what would happen if else was hit?

Also, in F#, every function must return a value, think method in C# and the void return type, so even if the function doesn’t conceptually return any­thing then it should return a unit value.

The ignore function can swallow a function’s return value if you want to return unit:

> let square x=x*x;;

val square :int->int

> ignore (square 3);;
val it : unit=()


Tuple
A tuple (pronounced two-pull) is an ordered collection of data,easy way to
group common pieces of data together. For example, tuples can be used to track
the intermediate results of a computation.

To create an instance of a tuple, separate group of values with comma, and optionally
place them within parentheses. Letz have fullName as an instance of a tuple, while
string * string is the tuples type:

> let fullName = ("ganesan", "senthilvel");;
val fullName : string * string = ("ganesan", "senthilvel")

Tuples can contain any number of values of any type. You can have a tuple that
contains other tuples.

No comments:

Post a Comment