Tuesday, February 7, 2012

F# 2.0 Console Application with Visual Studio - II

This is part of the blog that explore F# 2.0

F# 2.0 with Visual Studio 2010


 In the previous blog we explored a Hello world application with F# using the REPL support it is providing.


open System

Console.Write "Enter your name : "
let strName = Console.ReadLine()
Console.WriteLine("Hello " + strName)
let c = System.Console.ReadLine()

let is the single most important keyword you use in F# programming: it’s used to define data,
computed values, functions, and procedures.

F# is statically typed. F# also supports type inferencing

What you see is a full functioning F# application. Notice the code reduction in comparison to other .Net languages like C# or VB .Net.

Let's run the application instead of using F# interactive.
F# Console Result Shalvin

Creating and calling functions


let square n = n * n

let result = square 3
Console.WriteLine(result)


Functions are central to Functional programming. Functional programming views all programs as collections of functions that accept arguments and return values.


Function with Parameters


open System

let add(a, b) =
  let sum = a + b
  sum

Console.WriteLine(add(45, 45))

Indenting denotes the body. There is no curly brackets to denote the body of a function. White spaces are relevant in F#.

Creating a Windows Form

open System.Windows.Forms 

let form = Form(Visible = true, Text = "Shalvin")
let btn = new Button(Text = "Helo")
form.Controls.Add(btn)

Here I am setting a reference to System.Windows.Forms and creating a form with a Button.

What enthralls me the most in F# is its terseness.

No comments:

Post a Comment