F# for the C# Programmer

Posted by mbcrump on Geeks with Blogs See other posts from Geeks with Blogs or by mbcrump
Published on Sat, 22 May 2010 16:43:41 GMT Indexed on 2010/05/22 23:51 UTC
Read the original article Hit count: 538

Filed under:

Are you a C# Programmer and can’t make it past a day without seeing or hearing someone mention F#?  Today, I’m going to walk you through your first F# application and give you a brief introduction to the language. Sit back this will only take about 20 minutes.

Introduction

Microsoft's F# programming language is a functional language for the .NET framework that was originally developed at Microsoft Research Cambridge by Don Syme. In October 2007, the senior vice president of the developer division at Microsoft announced that F# was being officially productized to become a fully supported .NET language and professional developers were hired to create a team of around ten people to build the product version. In September 2008, Microsoft released the first Community Technology Preview (CTP), an official beta release, of the F# distribution . In December 2008, Microsoft announced that the success of this CTP had encouraged them to escalate F# and it is now will now be shipped as one of the core languages in Visual Studio 2010 , alongside C++, C# 4.0 and VB.

The F# programming language incorporates many state-of-the-art features from programming language research and ossifies them in an industrial strength implementation that promises to revolutionize interactive, parallel and concurrent programming.

Advantages of F#

F# is the world's first language to combine all of the following features:

  • Type inference: types are inferred by the compiler and generic definitions are created automatically.
  • Algebraic data types: a succinct way to represent trees.
  • Pattern matching: a comprehensible and efficient way to dissect data structures.
  • Active patterns: pattern matching over foreign data structures.
  • Interactive sessions: as easy to use as Python and Mathematica.
  • High performance JIT compilation to native code: as fast as C#.
  • Rich data structures: lists and arrays built into the language with syntactic support.
  • Functional programming: first-class functions and tail calls.
  • Expressive static type system: finds bugs during compilation and provides machine-verified documentation.
  • Sequence expressions: interrogate huge data sets efficiently.
  • Asynchronous workflows: syntactic support for monadic style concurrent programming with cancellations.
  • Industrial-strength IDE support: multithreaded debugging, and graphical throwback of inferred types and documentation.
  • Commerce friendly design and a viable commercial market.

Lets try a short program in C# then F# to understand the differences.

Using C#: Create a variable and output the value to the console window:

Sample Program.
  1. using System;
  2.  
  3. namespace ConsoleApplication9
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             var a = 2;
  10.             Console.WriteLine(a);
  11.             Console.ReadLine();
  12.         }
  13.     }
  14. }

image

A breeze right? 14 Lines of code. We could have condensed it a bit by removing the “using” statment and tossing the namespace. But this is the typical C# program.

Using F#: Create a variable and output the value to the console window:

To start, open Visual Studio 2010 or Visual Studio 2008. Note: If using VS2008, then please download the SDK first before getting started. If you are using VS2010 then you are already setup and ready to go.

So, click File-> New Project –> Other Languages –> Visual F# –> Windows –> F# Application. You will get the screen below.

image

Go ahead and enter a name and click OK.

Now, you will notice that the Solution Explorer contains the following:

image

Double click the Program.fs and enter the following information. Hit F5 and it should run successfully.

Sample Program.
  1. open System
  2. let a = 2       
  3. Console.WriteLine a

As Shown below:

image

Hmm, what? F# did the same thing in 3 lines of code. Show me the interactive evaluation that I keep hearing about.

The F# development environment for Visual Studio 2010 provides two different modes of execution for F# code:

  • Batch compilation to a .NET executable or DLL. (This was accomplished above).
  • Interactive evaluation. (Demo is below)

The interactive session provides a > prompt, requires a double semicolon ;; identifier at the end of a code snippet to force evaluation, and returns the names (if any) and types of resulting definitions and values.

To access the F# prompt, in VS2010 Goto View –> Other Window then F# Interactive.

Once you have the interactive window type in the following expression: 2+3;; as shown in the screenshot below:

image

I hope this guide helps you get started with the language, please check out the following books for further information.

F# Books for further reading

 

Foundations of F#
Foundations of F#

Author: Robert Pickering
An introduction to functional programming with F#. Including many samples, this book walks through the features of the F# language and libraries, and covers many of the .NET Framework features which can be leveraged with F#.

 

 

 

Functional Programming for the Real World: With Examples in F# and C#
Functional Programming for the Real World: With Examples in F# and C#

Authors: Tomas Petricek and Jon Skeet
An introduction to functional programming for existing C# developers written by Tomas Petricek and Jon Skeet. This book explains the core principles using both C# and F#, shows how to use functional ideas when designing .NET applications and presents practical examples such as design of domain specific language, development of multi-core applications and programming of reactive applications.

© Geeks with Blogs or respective owner