An Introduction to the F# Programming Language
Take Advantage of Functional Programming in ASP.NET
By Joydip Kanjilal
The F# programming language a multi-paradigm functional programming language provides type safety, interoperability, enhanced performance, and scripting capabilities. This article is an attempt to present this new language, its features and benefits with some sample code examples.
What Is F#?
The F# programming language gives you the necessary tools to solve the algorithmic complexities of symbolic processing. Wikipedia states, F# (pronounced F Sharp) is a multi-paradigm programming language, targeting the .NET Framework, that encompasses functional programming as well as imperative object-oriented programming disciplines. It is a variant of ML and is largely compatible with the OCaml implementation. F# was initially developed by Don Syme at Microsoft Research but is now being developed at Microsoft Developer Division and will be productized as a fully supported language in the .NET Framework and Visual Studio ecosystem. F# is powerful, scalable, type safe, interoperable, yet a simple functional programming language. See http://en.wikipedia.org/wiki/F%E2%99%AF_%28programming_language%29 for more details.
But what is functional programming? Wikipedia states, In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast with the imperative programming style that emphasizes changes in state. See http://en.wikipedia.org/wiki/Functional_programming for more details.
You can use F# directly from the Visual Studio IDE or by executing the fsi.exe tool from the directory where you installed F#.
Features of the F# Programming Language
Here s a list of some of the many features of the F# programming language:
- Strongly typed and uses type inference; allows explicit type declaration
- Support for expressions
- Support for object orientation
- Built on top of the Microsoft .NET Common Type System
- Rich library; collections, math, etc.
- Support for lazy evaluation
- Support for higher order functions
- Support for lambda functions
- Pattern matching
- Support for function composition
F# and ASP.NET
The F# library is fully compatible with the .NET object model. You have full support for ASP.NET from within F#. Here are the steps to follow to get started with F# and ASP.NET:
- Click on File | New | Project in Visual Studio and create an F# project
- Set the F# Project type to DLL
- Create a Web site and add the F# project to the Web site
- Copy the .fs file created from the F# Project folder to the Web Site Project Folder
You should also specify the following in your application s configuration file:
FSharp.Compiler.CodeDom, Version=<strong style="color:red;">18.104.22.168</strong>,
When you create a new Web site in Visual Studio and select the default language as F#, the Default.aspx and Default.aspx.fs files will be created by default. Here s some sample code to get you started:
<%@ Page Language="F#"
CodeFile="Default.aspx.fs" Inherits="MyFSharpProject.Default" %>
<asp:Button ID="btnClick" RunAt="server" Text="Click Here" OnClick="btn_OnClicked" /><br />
<asp:Label ID="lblMessage" RunAt="server" />
And here s the code you ll use in the code-behind file:
type Default() =
val mutable btnClick : Button
val mutable lblMessage : Label
member this.btn_OnClicked(sender, e) =
(printf "Hello World!"))
You re done! When you execute the application, you ll see a Button control displayed in the Web page. Simply click on it to see the message, Hello World! displayed in the Label control.
F# is a strongly typed functional programming language targeted at the .NET Framework. As noted in Expert F# (by Don Syme, Adam Granicz, and Antonio Cisternino, Apress 2007), It combines the succinctness, expressivity, and compositionality of typed functional programming with the run-time support, libraries, interoperability, tools, and object model of .NET.
Joydip Kanjilal is a Microsoft MVP in ASP.NET. He has more than 12 years of industry experience in IT with more than six years in Microsoft .NET and its related technologies. He has authored articles for some of the most reputable sites, including http://www.asptoday.com, http://www.devx.com, http://www.aspalliance.com, http://www.aspnetpro.com, http://www.sql-server-performance.com, and http://www.sswug.com. Many of these articles have been selected at http://www.asp.net, Microsoft s official site for ASP.NET. Joydip was also a community credit winner at http://www.community-credit.com a number of times. He is currently working as a Lead Architect in a reputable company in Hyderabad, India. He has years of experience in designing and architecting solutions for various domains. His technical strengths include, C, C++, VC++, Java, C#, Microsoft .NET, AJAX, Design Patterns, SQL Server, Operating Systems, and Computer Architecture. Joydip blogs at http://aspadvice.com/blogs/joydip and spends most of his time reading books and blogs, and writing books and articles. His hobbies include watching cricket and soccer and playing chess.