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.

Friday, November 25, 2011

TechMela


Last week, itz completely packed with technical conferences. To start with, Amazon had Amazon Web Services (AWS) Cloud tour at Chennai on Tuesday 15 Nov. Amazon CTO Dr. Werner Vogels was the chief guest for the conference with impressive key note address. He talked about cloud computation via Hadoop, MapReduce and storage via NoSQL. We had discussion panel with 4 CTOs who are prime customers for AWS Cloud. On Thursday 17 Nov, our firm had architecture summit 2011 and our CTO addressed about governance, life cycle management, listening to the customers and shared services. On Friday 18 Nov, Microsoft had their web camp with key note address by Gianugo Rabellino, product manager from Seattle. Sessions were around HTML5, CSS3, Webmatrix and Razor with hands on demo.

On Saturday Nov, took part in ThoughtWorks bar camp at their Chennai office. I heard about the work culture of the firm and their innovation on Agile development; but literally thrilled on seeing their work environment. It needs lot of maturity among their developers community. Miles to go! Back to the tech mela. Presented a session on Cloud computing using Azure between 10:30-11:15 am with few tech geeks. Had few hands on demo and fighting debates. Really loved it. Attended other sessions on Thrift, Scala, Coffeescript, Rails and open source impact. Itz pack jam of technical stuff through out the week and so named as TechMela.

Lessons learnt from TechMela:
  • Map reduce for the independent high CPU intensive threads
  • Evolution of Amazon and itz cloud entry as pioneer for their internal need
  • AWS storage trend using NoSQL model
  • Building shared services along with the development
  • Life cycle management impact on architecture
  • Vital role of governance in end to end architecture
  • Emerging technology role by HTML5 and itz impact on RIA vendors like Microsoft, Adobe
  • Role of functional programming (like scala, F#) to build complex app
  • New frameworks like Thrift, Rails
  • Awesome environment to present my cloud paper

Of course, had great opportunity to connect tech giants (CTOs, local tech geeks). Happy thanks giving week end, folks! Will continue our F# journey...


Saturday, November 19, 2011

F# Function


Function Definition
F# function is defined as values, except everything after the name of the function serves as parameters. Herez incrementor function


> let incrementer x = x+1;;

val incrementer : int->int

> incrementer 6;;
val it:int=7


Unlike C#, F# has no return keyword; it returns the last evaluted expression.

Type Inference
F# is statically typed, calling incrementer function with floating value will result in a compilation error as:

> incrementer 6.0;;

error FS0001: This expression has type float


Reason is becaz of type inference, which requires explicitly state of the types. Type inference is different from dynamic typing. Though, F# allows to omit types, it doesn't mean that type checking is not enforced at compile time.

Generic Function
Type of generic parameter can have the name of any valid identifier prefixed with an apostrophe. Below code defines incrementer function using a type annotation that forces the parameter x to be generic.

> let incrementer (x:'a) = x+1;;

val incrementer : 'a->'a


Writing generic code is important for maximizing code reuse. In this example, parameter 'a can be an int, float, string, user defined type, etc.

Saturday, November 12, 2011

F# Primitive Types


A type is abstraction and is primarily about enforcing safety. F# is statically typed, meaning that type checking is done at compile time. For example, if a function accepts an integer as a parameter, you will get a compilation error if you try to pass non integer value.

F# contains two varieties of numeric primitives as integer and floating point numbers. All numerical primitives are listed in the attached diagram. F# uses a let binding to create a value for numeric primitives. It allows to specify values in hexadecimal (base 16), octal (base 8) or binary (base 2) with prefix 0x, 0o, 0b. Few let bindings are:

> let hex =0xFCAF;;
val hex : int = 64687
> let bin =0b00101010y;;
val bin : sbyte = 43y

Arithmetic
You can use standard arithmetic operators on numeric primitives as +, -, *, /. Sample F# code is

> 32450s + 1s;;
val it : int16 = 32451s

By default, arithmetic operators do not check for overflow. So, if you exceed the range, the result will overflow to be negative.

Math
F# features all the standard math functions as
abs - absolute value of a number
ceil – round up to the nearest number
exp – raise a value to a power of e
floor – round down to the nearest integer
pown – power of an integer
sqrs – squre root of a given number

Conversion
F# doesn’t perform implicit conversions for primitive types. As an example, assigning int64 to int16 eliminates subtle bugs by removing surprise conversions. Developer must use an explicity conversion function like System.Convert

Bitwise Operation
Primitive integer types support bitwise operators for manipulating values at a binary level. Bitwise operators are &&& - and, ||| - or, ^^^ - xor, <<< - left shift, >>> - right shift

BigInt
If the application is dealing with larger data, F# provides BigInt type for representing arbitrarily long integers. BigInt uses I suffix for literals as

> open System.Numerics
let megabyte = 1024I * 1024I
let gigabyte = megabyte * 1024I

Although BigInt is heavily optimized for performance, itz much slower than using the primitive integer data types.

Saturday, November 5, 2011

first program


Visual Studio 2010 includes a new programming language, F#. To create a new console application

  1. On the File menu, point to New, and then click Project.
  2. If you cannot see Visual F# in the Templates Categories pane, click Other Languages, and then click Visual F#. The Templates pane in the center lists the F# templates.
  3. Look at the top of the Templates pane to make sure that .NET Framework 4 appears in the Target Framework box.
  4. Click F# Application in the list of templates.
  5. Type a name for your project in the Name field.
  6. Click OK.
  7. The new project appears in Solution Explorer.

First F# program holds integer (its square) and string value and then print them later.


open System
let iNum = 5
let strMsg = "Hello"
let iSquare = iNum * iNum
System.Console.WriteLine(iNum )
System.Console.WriteLine(aString)
System.Console.WriteLine(iSquare)


Like other .NET languages, press CTRL+F5 to run the code. A Command Prompt window appears that contains the following values.

Otherwise, you can execute F# program using Interactive window. First, select the let expressions in the previous procedure. Then, right-click the selected area and then click Send to Interactive. Alternatively, press ALT+ENTER.

The F# Interactive window opens and the results of interpreting the let expressions are displayed.