An Introduction to the F# Programming Language
Take Advantage of Functional Programming in ASP.NET
October 30, 2009
asp:Feature
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 functionalprogramming language provides type safety, interoperability, enhancedperformance, and scripting capabilities. This article is an attempt to presentthis new language, its features and benefits with some sample code examples.
What Is F#?
The F# programming language gives you the necessary toolsto solve the algorithmic complexities of symbolic processing. Wikipedia states, F# (pronounced F Sharp) is a multi-paradigm programming language, targetingthe .NET Framework, that encompassesfunctional programming as well as imperative object-oriented programmingdisciplines. It is a variant of ML and is largely compatible with the OCamlimplementation. F# was initially developed by Don Syme at Microsoft Researchbut is now being developed at Microsoft Developer Division and will beproductized as a fully supported language in the .NETFramework 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%29for more details.
But what is functional programming? Wikipedia states, Incomputer science, functional programming is a programming paradigm that treatscomputation as the evaluation of mathematical functions and avoids state andmutable data. It emphasizes the application of functions, in contrast with theimperative programming style that emphasizes changes in state. See http://en.wikipedia.org/wiki/Functional_programmingfor more details.
You can use F# directly from the Visual Studio IDE or byexecuting 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; allowsexplicit type declaration
Support for expressions
Support for object orientation
Terse
Built on top of the Microsoft .NET Common TypeSystem
Rich library; collections, math, etc.
Support for lazy evaluation
Support for higher order functions
Support for lambda functions
Pattern matching
Support for function composition
Scalable
Interoperable
F# and ASP.NET
The F# library is fully compatible with the .NET objectmodel. You have full support for ASP.NET from within F#. Here are the steps tofollow to get started with F# and ASP.NET:
Click on File | New | Project in Visual Studioand create an F# project
Set the F# Project type to DLL
Create a Web site and add the F# project to the Website
Copy the .fs file created from the F# Projectfolder to the Web Site Project Folder
You should also specify the following in your application sconfiguration file:
language="F#;f#;fs;fsharp" extension=".fs" type="Microsoft.FSharp.Compiler.CodeDom.FSharpAspNetCodeProvider, FSharp.Compiler.CodeDom, Version=1.9.3.14, Culture=neutral,PublicKeyToken=a19089b1c74d0809"/> When you create a new Web site in Visual Studio and selectthe default language as F#, the Default.aspx and Default.aspx.fs files will becreated by default. Here s some sample code to get you started: <%@ Page Language="F#" CodeFile="Default.aspx.fs"Inherits="MyFSharpProject.Default" %>
And here s the code you ll use in the code-behind file: #lightnamespace MyFSharpProjectopen Systemopen System.Webopen System.Web.UI.WebControlstype Default() = inherit Page() [] val mutable btnClick :Button [] val mutable lblMessage :Label memberthis.btn_OnClicked(sender, e) = this.lblMessage.Text <- (printf "HelloWorld!")) You re done! When you execute the application, you ll seea Button control displayed in the Web page. Simply click on it to see themessage, Hello World! displayed in the Label control. Conclusion F# is a strongly typed functional programming languagetargeted 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-timesupport, libraries, interoperability, tools, and object model of .NET. Joydip Kanjilal isa Microsoft MVP in ASP.NET. He has more than 12 years of industry experience inIT with more than six years in Microsoft .NET and its related technologies. Hehas 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 thesearticles have been selected at http://www.asp.net,Microsoft s official site for ASP.NET. Joydip was also a community creditwinner at http://www.community-credit.coma number of times. He is currently working as a Lead Architect in a reputablecompany in Hyderabad, India. He has years of experience in designing andarchitecting 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/joydipand spends most of his time reading books and blogs, and writing books andarticles. His hobbies include watching cricket and soccer and playing chess.
Read more about:
MicrosoftAbout the Author
You May Also Like