Advertisment

F#: Do More With Less Coding

author-image
PCQ Bureau
New Update

Most scientists use mathematics as a language to define their models. Since

long, these scientists have been trying to bridge the gap between scientific or

mathematical models and the executable code. Functional programming language

allows developers to write programs that are close to the mathematical models.

F# is a functional programming language for .NET framework. This strongly typed

language uses type inference, which means data type need not be explicitly

declared by programmers; instead they are declared by the compiler during

compilation. F# takes best features of both imperative and declarative

programming paradigms making it a language both scientists and programmers can

identify with. It tries to facilitate a mathematician become a better programmer

and vice versa. It also combines the power of declarative languages popular with

scientists such as Ocaml and Lisp, with .NET framework, giving developers a

powerful development environment.

Advertisment

Direct Hit!

Applies To: C# developers



USP: Understand how this functional
programming language works



Primary Link: www.msdn.com


Keywords: Microsoft F#

Working with F#



To work with the latest version of this functional programming language, one

can either download and install the Visual Studio 2010 Beta 1 or use the CTP

add-on for Visual Studio 2008. In this sample implementation we use Visual

Studio 2008 with the F# add-on. To download and install this add-on visit 'msdn.microsoft.com/en-us/fsharp/default.aspx'

and download 'InstallFSharp.msi' file. Once this add-on has been installed, it

will add a new project type, 'Visual F#' in the 'New Project' window. To start

creating an F# application, open Visual Studio and create a new F# project by

selecting 'F# Application' from template window ('ConsoleApplication1'). Here is

a simple F# application that calculates squares for ten numbers:

#light



let square x = x * x


let numbers = <1 .. 10>


let s = List.map square numbers


printfn "Square of 10 numbers = %A"s


open System


Console.ReadKey(true)




Advertisment
After installing F# CTP add-on to Visual Studio 2008 SP1,

you shall see a new project type, 'Visual F#,' in the New Project window.

In the above code '#light' is the complier directive while the second line of

the code declares a function that calculates squares of numbers. The difference

between this code and similar C# code shown below is that one does not need to

specify 'type' information. F# compiler figures this out automatically. The

third line of code lists the ten numbers to be squared. The fourth line maps

square function to each number to be squared.

To use F# interactive window, select the relevant part of

the code to be executed and press 'ALT+ENTER.' The output shall appear in

the FIC window.
Run the program by pressing 'F5.' For a similar program in

C#, one has to write more lines in code.
Advertisment

To run this code simply press 'F5' from the key board. This will result in a

console window with squares of first ten numbers. To enhance efficiency while

writing code, F# plug-in for C# also comes with 'F# Interactive Console' for

compiling and executing code snippets. Simply highlight the code to be executed

and press 'ALT+ENTER.' This will bring up the FSI window. After this, the

highlighted code is directly sent to FSI and results are displayed. In the FSI

window, type:

List.map square <12..24>;;

Now press 'ENTER.' The result will be displayed as:

Advertisment

val it : int list =



<144; 169; 196; 225; 256; 289; 324; 361; 400; 441; 484; 529; 576>

If you write the same application in C#, the number of code lines is

considerably higher. Have a look:

using System;



using System.Text;

Advertisment

namespace ConsoleApplication1



{


class Program


{


static void Main(string<> args)


{


int Square;


Console.WriteLine ("\tNumber\tSquare");


for (int Number = 1; Number <= 10;Number++)






{



Square = Number * Number;


Console.WriteLine ("\t{0}\t{1}", Number, Square);


}

Console.ReadLine();



}


}


}

After having gone through the sample implementation given above, it must have

become abundantly clear that the way code is to be written in F#, the latest

functional programming language, included in the Visual Studio 2010 is simple

and close to how one actually tries to find solutions to mathematical

expressions related to day-to-day problems, thus making things a lot simpler for

scientists and engineers.

Advertisment