Thursday, 6 February 2014

Type Annotations in F#

In this article we are going to see the Type annotations in F#, Type annotations is specify the type in the input parameter.if we are not specify the Type then the function can't able to find which type is this to do operations like replace in string.

#light
 
open System.Data.SqlClient
open Microsoft.FSharp

let rep pharse =
pharse.Replace('t','7').Replace('o','0')



Now compiling above code shows error that not able to find the correct type, because the usage of Replace function we have to specify the Type as Annotation so the above code is reformatted.


#light
 
open System.Data.SqlClient
open Microsoft.FSharp

let rep (pharse:string) =
pharse.Replace('t','7').Replace('o','0')

 Output:
   rep token ;;
   70ken

From the above code you can learn the Type annotation in F#.



Read a Data from a Database using F#

In this article we are going to see how to read a data from the database in F#. We are going to read the data using .Net framework class .

SQL :-
  
create table test
(
id int identity(1,1),
name varchar(100),
occupation varchar(100)
)

insert into test(name,occupation)
values ('KA','AT')




F# :-

open System.Data.SqlClient
let readddata =
use conn = new SqlConnection(@"Data Source = rajesh-PC\sqlserver2008; Initial Catalog = rajesh; Integrated Security=true")
use cmd = new SqlCommand("select id,name,occupation from test", conn)
conn.Open()
use rdr = cmd.ExecuteReader()
while rdr.Read() do
       printfn " Id:- %s  Name:- %s Occupation:- %s " (rdr.GetInt32(0).ToString())                  (rdr.GetString(1)) (rdr.GetString(2)) 

output: 

 Id:- 1  Name:- Suresh  Occupation:- EP
 Id:- 2  Name:- Sam     Occupation:- DE
 Id:- 3  Name:- KA      Occupation:- AT


From this post you can learn how to read the data from the Database.

Wednesday, 5 February 2014

Create a Fibonacci series in F#

In this post we are going to see how to create a Fibonacci series in F#. we can create the fibonacci in simple steps.


open System

let rec fib n =
match n with
| 1 -> 1
| 2 -> 1
| n -> fib(n - 1) + fib(n - 2)
   
Press Alt+enter by select the code and enter the Fib 5 in Interactive window.

> fib 6 ;;
val it : int = 8
> fib 20 ;;

val it : int = 6765

From this sample you see how to create the Fibonacci.

Tuesday, 4 February 2014

Round Corner Form in Winforms - C#

In this article we are going to see how to create a rounded corners form in C#, So far that we are going to create a custom form, we can do it in many ways by overriding the Paint method and write customized code, otherwise used the extern method.

C# Code:

public class RoundedBaseForm : Form
    {
        public RoundedBaseForm()
        {
            Region = Region.FromHrgn(CreateRoundRectRgn(50, 50, Height, Width, 480, 250));
        }

        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        private static extern IntPtr CreateRoundRectRgn(int LeftRect, int TopRect, int RightRect, int BottomRect, int Width, int Height);

    }


public partial class Form1 : RoundedBaseForm
    {
        public Form1()
        {
            InitializeComponent();
            //this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;           
            this.BackColor = Color.FromArgb(255,207, 81);
          
        }               
    }
  

Output:

For value 450 * 250 : Circle


For value 450 * 60 : Cylinder
From this post you can learn how to create a customized Forms.

Sunday, 2 February 2014

Factorial Program in F#

In this article we are going to see the create a program for factorial in F#, normally create a Factorial means takes four to five line of code, but in F# it is simple in one line we can write that.


rec is the keyword makes the function specify that is recursively called

open System

printfn "Factorial"

let rec factorial x = if x=0 then 1 else x * factorial(x-1)

Console.WriteLine(factorial(5))


Console.ReadKey()


Output:
Factorial
120


Create a Function and Pass the parameter for the function - F#

In this post we are going to see how to create and parameter function and call that function.Always specify the parameter after to that of fucntion variable name then start implement the function name. When call the function parameter must be pass to the next to the function name with a space, then the code next to the parameter will be consider as operations.


     Sample: 

open System

let f x = 3*x + 4*x + 10

let intval = 3
let result = f (intval + 2)
Console.WriteLine("Result1 :- {0}",result)

let result2 = f intval + 2
printfn "Result2 :- %d" result2

Console.ReadKey()


Output:
                Result1 :- 45
                Result2 :- 33


In the following sample you can see the code results in two output, only the difference is pass the with out bracket , because the input is the combination of two values.


From this post you can learn how to create a function and pass the parameter to the function.

Hello world Application in F#

In this post we are going to see how to create the Hello World Application in F#. F# is a Functional Programming along with object oriented. Open means refer the System.


F# Code:


#light

open System

let a = 1
let b = 2
let c = a + b
Console.WriteLine "Hello World"
Console.WriteLine(c)
Console.Read()

Output:
   Hello World
   3

From this post you can learn how to create the Hello World sample Application.

F# - Language

In this article we are going to see the detailed information and description of F# Language , F# means F-Sharp, It's a functional Programming  language.

What is a Functional Programming ?
   Functional Programming considers the all programs as a collection of functions.Functions in a functional programming are very much like a mathematical they do not change the state of a program, Because it doesn't alter the value of a parameters, results are return to a new value.
  If once the value is assigned to a variable ,it allocates the value in the memory and it doesn't change that.For create a new results it copy the values and change the copied value and given result back.

Functional programming provides a solutions for computing problems, its recursive nature give solution for many common tasks.but functional programming is a Stateless.

Functional programming treats the function itself as a values and pass it as parameters to another functions.

F# :-

F# is a combination of  functional programming and Object Oriented in a same program.F# is a strongly typed and but also uses inferred typed. so there is no need to specify the type explicitly.It is modeled on Objective Caml.

F# is seamlessly integrated with the .Net Framework.It can produces the exe for any Common Language Infrastructure.It also run on any environment that have CLI, F# Can run on Windows, Linux, Apple as well as Google Android OS.

F# was implemented by Don Syme at Microsoft Research (MSR) in cambridge. The project has now been embrassed by Microsoft Corporate in Redmond.


From this post you can understand what is F# Language from where it is created and for what purpose.