<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://cs.hubfs.net/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>My F# Notes</title><link>http://cs.hubfs.net/blogs/tomasp/default.aspx</link><description /><dc:language>en-US</dc:language><generator>CommunityServer 2.0 (Build: 60217.2664)</generator><item><title>Introducing F# - Four part webcast series</title><link>http://cs.hubfs.net/blogs/tomasp/archive/2009/06/16/fsharp_webcast.aspx</link><pubDate>Tue, 16 Jun 2009 09:32:00 GMT</pubDate><guid isPermaLink="false">7372db05-f90c-40e3-82a2-789ed9f521c9:10966</guid><dc:creator>tomasp</dc:creator><slash:comments>1</slash:comments><comments>http://cs.hubfs.net/blogs/tomasp/comments/10966.aspx</comments><wfw:commentRss>http://cs.hubfs.net/blogs/tomasp/commentrss.aspx?PostID=10966</wfw:commentRss><description>&lt;P&gt;Now that &lt;A href="http://www.microsoft.com/visualstudio/en-us/products/2010/default.mspx"&gt;Visual Studio 2010 Beta 1&lt;/A&gt; is out, it is finally a good time to take a look at one of the (in my opinion) most interesting new features in the new release - &lt;A href="http://blogs.msdn.com/dsyme/archive/2009/05/20/visual-studio-2010-beta1-with-f-is-now-available-plus-matching-f-ctp-update-for-vs2008.aspx"&gt;the F# language&lt;/A&gt;. As all the regular hubFS readers know, F# existed for quite a long time now as Microsoft Research project, but is now becoming a real Microsoft product. Interestingly, F# is still available as a &lt;A href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;amp;FamilyID=7bb32f32-9fac-4f34-ad56-b0bda130cf00"&gt;plugin for Visual Studio 2008&lt;/A&gt;, so if you want to try it you don't have to install the whole new beta of 2010.&lt;/P&gt;
&lt;P&gt;There are already many resources for learning F# including my &lt;A href="http://tomasp.net/blog/functional-overview.aspx"&gt;functional programming overview&lt;/A&gt;, which is a Manning Greenpaper for the book &lt;A href="http://www.functional-programming.net/"&gt;Functional Programming for the Real World&lt;/A&gt; that I'm writing with Jon Skeet and my &lt;A href="http://tomasp.net/blog/fsharp-i-introduction.aspx"&gt;four-part F# introduction&lt;/A&gt;. There are also some useful links on the official &lt;A href="http://www.fsharp.net/"&gt;F# web site&lt;/A&gt; including some talk recordings. However, I haven't yet seen any good F# webcast focusing mainly on showing F# source code, starting from simple functional concepts to the real-world features like asynchronous workflows and object-oriented programming in F#, so I decided to create one.&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;(The whole webcast series is available in &lt;/EM&gt;&lt;A href="http://tomasp.net/blog"&gt;&lt;EM&gt;my other blog&lt;/EM&gt;&lt;/A&gt;&lt;EM&gt; and I'm cross-posting the links and brief descriptions of each of the parts here)&lt;/EM&gt;&lt;/P&gt;
&lt;H2&gt;The F# development process&lt;/H2&gt;
&lt;P&gt;The webcast&amp;nbsp;starts by experimenting with F# and by introducing some of the essential functional concepts and then follows the usual F# development process. It starts by creataing an empty F# script (&lt;EM&gt;New File... -&amp;nbsp;F# Script File &lt;/EM&gt;in Visual Studio), which is the easiest way to start coding in F#. We'll just create an empty script, start writing the code and run it interactively to see how it works. This way, we'll develop a very simple F# snippet for downloading content of a RSS feed and for searching for feeds that contain the specified keyword.&lt;/P&gt;
&lt;P&gt;Once we have a simple solution to the problem, we can start improving it and &lt;EM&gt;refactoring &lt;/EM&gt;it in various ways. I believe that refactoring is a very important part of the usual F# development process. In particular, we'll see how to wrap the code into asynchronous workflows to make it run asynchronously (rather than using blocking calls) and also potentially in parallel (rather than downloading and processing all the feeds in sequence).&lt;/P&gt;
&lt;P&gt;Finally, in the last part of the series, you'll see one more refactoring. We'll wrap the existing code into a simple&amp;nbsp;object oriented library and we'll also finally turn it from a simple F# script file into a standard .NET DLL library that can be used from C#. As you'll see in the webcast, this is again a relatively simple refactoring that includes only a couple of minor changes in the source code. Once we'll compile the library, we'll also look how to use it from C# and in particular, from a simple ASP.NET web application.&lt;/P&gt;
&lt;H2&gt;Introducing F# webcast&lt;/H2&gt;
&lt;P&gt;The links to the individual parts of the webcast are available here. Each of the articles also contains link for downloading the video (in case you wanted to view it offline) and also full source code that I wrote during the presentation:&lt;/P&gt;
&lt;UL&gt;
&lt;LI style="MARGIN-BOTTOM: 5px"&gt;&lt;STRONG&gt;&lt;A href="http://tomasp.net/blog/fsharp-webcast-functional.aspx"&gt;Part I. - Introducing functional concepts&lt;/A&gt;&lt;/STRONG&gt;&lt;BR&gt;The first part introduces functional programming principles such as immutability, recursion and functions that take other functions as parameter (&lt;EM&gt;higher order&lt;/EM&gt; functions). This can all be demonstrated in C# 3.0, so we start with C# and then look how the same concepts look in F#. Finally, the first part also shows functions for working with lists in F#. 
&lt;LI style="MARGIN-BOTTOM: 5px"&gt;&lt;STRONG&gt;&lt;A href="http://tomasp.net/blog/fsharp-webcast-dotnet.aspx"&gt;Part II. - Using standard .NET libraries&lt;/A&gt;&lt;/STRONG&gt;&lt;BR&gt;The second part demonstrates how we can use standard .NET libraries. It uses classes from &lt;CODE&gt;System.Net&lt;/CODE&gt; and &lt;CODE&gt;System.Xml&lt;/CODE&gt; to download content of a web page (RSS feed), load it into XML document and process it to find only posts that contain some specified keyword. 
&lt;LI style="MARGIN-BOTTOM: 5px"&gt;&lt;STRONG&gt;&lt;A href="http://tomasp.net/blog/fsharp-webcast-async.aspx"&gt;Part III. - Downloading web pages asynchronously&lt;/A&gt;&lt;/STRONG&gt;&lt;BR&gt;The third part shows how to make the code from the part II. better. It introduces F# &lt;EM&gt;asynchronous workflows&lt;/EM&gt; that can be used for writing code that doesn't block a thread when waiting for the completion of some I/O request. This part also shows how to modify the code to download and process multiple feeds in parallel. 
&lt;LI style="MARGIN-BOTTOM: 5px"&gt;&lt;STRONG&gt;&lt;A href="http://tomasp.net/blog/fsharp-webcast-objects.aspx"&gt;Part IV. - Developing standard .NET libraries&lt;/A&gt;&lt;/STRONG&gt;&lt;BR&gt;In the fourth part, we look how to encapsulate the functionality written in F# into classes. We'll create a project and we'll wrap the code we wrote into a .NET class. We'll also look how to compile the project into DLL and how to use it from a simple C# web application.&lt;/LI&gt;&lt;/UL&gt;&lt;img src="http://cs.hubfs.net/aggbug.aspx?PostID=10966" width="1" height="1"&gt;</description></item><item><title>My book: Real-world Functional Programming in .NET</title><link>http://cs.hubfs.net/blogs/tomasp/archive/2008/11/05/functional_book.aspx</link><pubDate>Wed, 05 Nov 2008 10:09:00 GMT</pubDate><guid isPermaLink="false">7372db05-f90c-40e3-82a2-789ed9f521c9:7614</guid><dc:creator>tomasp</dc:creator><slash:comments>1</slash:comments><comments>http://cs.hubfs.net/blogs/tomasp/comments/7614.aspx</comments><wfw:commentRss>http://cs.hubfs.net/blogs/tomasp/commentrss.aspx?PostID=7614</wfw:commentRss><description>&lt;P&gt;&lt;EM&gt;(This article is based on &lt;A href="http://tomasp.net/blog/functional-programming-book.aspx"&gt;two&lt;/A&gt; &lt;A href="http://tomasp.net/blog/functional-net-update.aspx"&gt;articles&lt;/A&gt; about the book that I posted to my &lt;A href="http://tomasp.net/blog"&gt;other blog&lt;/A&gt;).&lt;/EM&gt;&lt;/P&gt;&lt;IMG style="MARGIN: 10px; FLOAT: right" alt="Book cover" src="http://www.tomasp.net/img/cover_main.gif"&gt; 
&lt;P&gt;If you’ve been reading my blog or seen some my articles, you know that I’m a big fan of the F# language and functional programming style. I’m also often trying to present a bit different view of C# and LINQ – for me it is interesting mainly because it brings many functional features to a main-stream language and allows using of many of the functional patterns in a real-world. Elegant way for working with data, which is the most commonly used feature of C# 3.0, is just one example of this functional approach. Talking about real-world applications of functional programming, there is also fantastic news about F#. It was announced last year that F# will become fully supported Visual Studio language and the first September CTP version of F# was released recently! &lt;/P&gt;
&lt;P&gt;I always thought that the topics mentioned in the previous paragraph are really interesting and that functional programming will continue to become more and more important. That’s why I’m really excited that I have the privilege to write a book about these topics. The book is called &lt;A href="http://www.manning.com/petricek"&gt;Real-world Functional Programming in .NET&lt;/A&gt; [&lt;A target=_blank href="http://www.manning.com/petricek"&gt;^&lt;/A&gt;] and it’ll be published by &lt;A href="http://www.manning.com/"&gt;Manning Publications&lt;/A&gt; [&lt;A target=_blank href="http://www.manning.com/"&gt;^&lt;/A&gt;]. Even though there are some books about F# already (and more are probably comming soon), I believe that my book is unique in a way that it talks about both C# and F#. This is useful when understanding functional programming concepts, because you can see how would the same thing look in C#. Also, there are many functional techniques that can help you with daily C# programming and these are also discussed in the book. Here is a couple of reasons why you should have this book on your bookshelf:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;Ideas behind C# 3.0 and LINQ&lt;/STRONG&gt; - these main-stream technologies are inspired by functional programming and the new C# 3.0 features give us definitely much more than just a new way to query databases. The book explains the ideas behind these features and shows how to use them more efficiently in a wider range of scenarios. 
&lt;LI&gt;&lt;STRONG&gt;Learning the F# language&lt;/STRONG&gt; - &lt;A href="http://blogs.msdn.com/somasegar/archive/2007/10/17/f-a-functional-programming-language.aspx"&gt;F# is becoming a first-class citizen&lt;/A&gt; [&lt;A target=_blank href="http://blogs.msdn.com/somasegar/archive/2007/10/17/f-a-functional-programming-language.aspx"&gt;^&lt;/A&gt;] in the Visual Studio family of languages, which alone would be a good reason for learning it! Even if you're not going to use it for your next large .NET project, you'll find it useful for quick prototyping of ideas and testing how .NET libraries work thanks to the great interactive tools. 
&lt;LI&gt;&lt;STRONG&gt;Real world examples&lt;/STRONG&gt; - the book includes a large set of real-world examples that show how to develop real .NET applications in a functional way - both in F# and C#. Among other things, the examples show how to utilize multi-core CPUs, how to better obtain and process data and how to implement animations and GUI applications in a functional way.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;To summarize, the goal of the book is to explain the key ideas of functional programming to real world .NET developers in a way they can benefit from them. It actually very much follows my personal experience – I started seriously looking at functional programming in the F# language after using primarily C# for quite a long time. &lt;/P&gt;
&lt;H2&gt;What's new?&lt;/H2&gt;
&lt;P&gt;Here is a couple of links if you're interested in more information about the book. I started a companion web site for the book, so you'll find everything regarding the book, links to related articles and the source code here: &lt;A style="FONT-WEIGHT: bold" href="http://www.functional-programming.net/"&gt;www.functional-programming.net&lt;/A&gt;. There is a book page at Manning web site where you can find the direct links to the free content (Chapter 1 and the Greenpaper) and also the option to purchase the book via MEAP today:&lt;/P&gt;
&lt;UL style="MARGIN-TOP: 0px"&gt;
&lt;LI&gt;&lt;A href="http://www.manning.com/petricek/"&gt;First four chapters&lt;/A&gt; are already available via Manning Early Access program. 
&lt;LI&gt;&lt;A href="http://www.manning.com/petricek/"&gt;First chapter&lt;/A&gt; titled "Thinking differently about problems" is available for free. 
&lt;LI&gt;&lt;A href="http://www.manning.com/free/green_petricek.html"&gt;Greenpaper&lt;/A&gt; showing what you can expect to find in the book is available for free. 
&lt;LI&gt;&lt;A href="http://www.functional-programming.net/"&gt;www.functional-programming.net&lt;/A&gt; - My web site about the book.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;The MEAP program allows you to get the book now and start reading chapters as they become available. Once the whole book is completed, you'll get the final version (either electronic or both electronic and printed). This means that you'll be among the first people to see the draft and you also have a chance to influence it. I've been receiving a large number of great suggestions as well as corrections from the MEAP program via &lt;A href="http://www.manning-sandbox.com/forum.jspa?forumID=460"&gt;book forum at Manning&lt;/A&gt; [&lt;A target=_blank href="http://www.manning-sandbox.com/forum.jspa?forumID=460"&gt;^&lt;/A&gt;], so huge thanks to anyone who is reading the book and discussing it there!&lt;/P&gt;
&lt;P&gt;The MEAP version is updated every couple of weeks, so things has changed since I posted the announcement to my blog and I had to rewrite this paragraph. Currently, you can get first 10 chapters and I'm working on chapter 11, so you can see that there is very small delay (when I see some of the errors in the draft that readers report in the forum, I sometimes think that it is almost &lt;EM&gt;too small&lt;/EM&gt; - thanks again)!&lt;/P&gt;
&lt;P&gt;About a month ago, the MEAP version of the book was first in the list of Manning bestsellers (even before books on topics like ASP.NET "MVC" and Silverlight, which was a very nice surprise for me)! Click on the image to see the full-size screenshot:&lt;/P&gt;
&lt;DIV style="TEXT-ALIGN: center"&gt;&lt;A target=_blank href="http://tomasp.net/articles/functional-net-update/screen.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; MARGIN-LEFT: auto; BORDER-TOP: 0px; MARGIN-RIGHT: auto; BORDER-RIGHT: 0px" alt="MEAP books" src="http://tomasp.net/articles/functional-net-update/screen_sm.png"&gt;&lt;/A&gt;&lt;/DIV&gt;
&lt;H2&gt;What’s inside?&lt;/H2&gt;
&lt;P&gt;The most interesting thing for me about writing the book is that I really had to shape my ideas very clearly and find a good way to organize all the topics. One point of view that I found particularly useful (and it reflects how I write code in F#) is that F# gives you a way to start with a very simple, easy to write and clear code and later turn it into a robust .NET code. At the beginning you can write your code just as a script for F# interactive using only the basic functional features. Thanks to the type inference the code is also very succinct. In the later phase you can use features like abstract interface types and object expressions to organize the code and make it easily accessible for example from C#. The nice thing is that the transition is very smooth, so you don't have to rewrite almost any code. &lt;/P&gt;
&lt;P&gt;Since many of the functional features are now available in C# 3.0, it is possible to apply some of these ideas to C# too. In particular, we can simplify many object-oriented constructs (including some well known design patterns) and also use many effective functional techniques. This way of thinking is very suitable for a book, because we can start by looking at simple and clear ideas and then learn more sophisticated techniques and language features. The chapters that are currently written follow this organization. Here is a brief overview of the content of every chapter:&lt;/P&gt;
&lt;H4&gt;Part I. Introduction&lt;/H4&gt;
&lt;P&gt;To many of the readers, functional programming is a completely new idea. The basic principles of functional programming are also quite different to what you may be used to. The first part of the book (containing chapters 1 - 4) serves as an introduction and touches lightly some of the ideas that will later become essential. &lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;Chapter 1: Thinking differently about problems&lt;/STRONG&gt; starts with a little section about the long history of functional programming (By the way, would you believe that the first functional programming language celebrated its 50 years in 2008?). As a next thing, the chapter discusses some important reasons why functional concepts are important nowadays and shows successful real world applications of these concepts. The largest part is a tasting of what you'll find later in the book and finally, it contains a section showing the first example application in F#. 
&lt;UL&gt;
&lt;LI style="MARGIN: 5px"&gt;The chapter is available for free from the &lt;A href="http://www.manning.com/petricek/"&gt;Manning book page&lt;/A&gt; [&lt;A target=_blank href="http://www.manning.com/petricek/"&gt;^&lt;/A&gt;]. &lt;/LI&gt;&lt;/UL&gt;
&lt;LI&gt;&lt;STRONG&gt;Chapter 2: Functional concepts and programming languages&lt;/STRONG&gt; introduces the concepts behind functional programming. As I already mentioned many of the functional concepts are different then what you might be used to, so this chapter gives a brief overview (without any details) showing how functional concepts relate and what they mean for the program structure. 
&lt;LI&gt;&lt;STRONG&gt;Chapter 3: Meet tuples, lists and functions in F# and C#&lt;/STRONG&gt; finally shows some real functional code. In this chapter, we use both C# and F# when writing any code, which makes it easier to understand what the functional program does. It demonstrates some basic data types used in F# (such as tuples and lists) and also shows how they can be implemented in C#. This chapter also introduces the idea of using functions as values, which is one of the key functional concepts. 
&lt;LI&gt;&lt;STRONG&gt;Chapter 4: Exploring F# and .NET libraries by example&lt;/STRONG&gt; presents the first real world application implemented in F# and shows how to use various .NET and F# libraries from the F# language. We look at an application that loads data from a file and presents them in a graphical form using a pie chart. This chapter finishes the introductory part by presenting the F# language by example, using only basic features from Chapter 3.&lt;/LI&gt;&lt;/UL&gt;
&lt;H4&gt;Part II. Core functional techniques&lt;/H4&gt;
&lt;P&gt;The second part starts describing the "iterative development style" enabled by functional programming in F#. It discusses elementary ways used to construct functional programs. These give us a very clear and concise way to think about problems. Finally, in the later chapters it talks about functional applications from a higher perspective focusing more on architectural acpects.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;Chapter 5: Creating and using common functional values&lt;/STRONG&gt; - in functional programming, applications are described more like calculations that take some values as arguments and return a value as a result. This chapter explains what values actually are and talks about usual ways to construct them. This includes representing multiple values, alternative values and also functions (which are values too). 
&lt;LI&gt;&lt;STRONG&gt;Chapter 6: Working with values using high-order functions&lt;/STRONG&gt; introduces better way for working with basic values from Chapter 5. Instead of manipulating with them directly, we can factor out the changing part of some common operation and then use the single function for many varying tasks. This talks mainly about using functions as arguments, which is also very important for C# 3.0, which supports lambda functions. 
&lt;LI&gt;&lt;STRONG&gt;Chapter 7: Designing data-centric programs&lt;/STRONG&gt; - when designing an application in the functional style, we start by thinking about the data that it works with. This principle is very useful and clear independently of the language that you're actually using. In this chapter, we demonstrate this principle and also show how it relates to some well-known structural design patterns. 
&lt;LI&gt;&lt;STRONG&gt;Chapter 8: Designing behavior-centric programs&lt;/STRONG&gt; - not all applications are primarily focused at working with data as described in Chapter 7. Here we talk about the second types of applications. Using functional terminology, this chapter talks about functions in detail. In the object oriented terminology, we look at some behavioral design patterns like Strategy or Command.&lt;/LI&gt;&lt;/UL&gt;
&lt;H4&gt;Part III.&lt;/H4&gt;
&lt;P&gt;This section focuses on more advanced concepts that are used later in the "iterative development style". Once the application or a library grows larger, we want to use various .NET facilities to organize it intuitively, expose it to other .NET languages and document it. If the library is intended only for F# then we can leverage of some advanced F# features. Finally, we also want to make the code more efficient by using general functional and F# specific optimization techniques.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;Chapter 9: Turning values into F# object types with members&lt;/STRONG&gt; shows how to mix object-oriented and functional style of programming in F#. It shows how the functional concepts (like immutability) can be used well together with object oriented features like encapsulation and how the conceptually simple ideas from Chapter 7 and 8 can be wrapped into real-world .NET code. 
&lt;LI&gt;&lt;STRONG&gt;Chapter 10: Efficiency of data structures, tail-recursion and continuations&lt;/STRONG&gt; - in the earlier chapters we talked about recursion and data structures such as lists or trees. In this chapter, we look how to use them efficiently. This means how to avoid stack overflow errors when writing functional code and how to write fast code that processes lists and other collections. 
&lt;LI&gt;&lt;STRONG&gt;Chapter 11: Refactoring functional programs and using lazy values&lt;/STRONG&gt; Refactoring is a phase of development where we try to improve our code without doing any changes in its behavior. This is a very important step and functional programming gives us great tools to do it. Function values are useful when removing code duplication and thanks to immutablity it is much easier to ensure that a change will not affect the behavior. Finally, lazy values allow us to delay some computations until their results are actually needed, which is a topic very close to refactoring.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;These are all the chapters that I wrote already (Chapter 11 is not available yet, but I already know quite well what will be inside). I'll definitely blog about more chapters as I'll write them, so stay tuned for another update!&lt;/P&gt;
&lt;H3&gt;Looking for your feedback!&lt;/H3&gt;
&lt;P&gt;The book is still "work in progress", so I'm very glad to receive any feedback. I already got many useful comments and suggestions via the &lt;A href="http://www.manning-sandbox.com/forum.jspa?forumID=460"&gt;book forum at Manning&lt;/A&gt; [&lt;A target=_blank href="http://www.manning-sandbox.com/forum.jspa?forumID=460"&gt;^&lt;/A&gt;], from the reviewers of the first third of the book and also from my colleagues and friends. However, this is probably the first book about functional programming that aims to a very wide audience of .NET programmers, requiring only advanced C# background, so any comments are more than welcome! You can post them to the forum mentioned above as well as directly to me at &lt;A href="mailto:tomas@tomasp.net"&gt;tomas@tomasp.net&lt;/A&gt;.&lt;/P&gt;
&lt;H2&gt;More information&lt;/H2&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://www.functional-programming.net/"&gt;Real-world Functional Programming in .NET&lt;/A&gt; [&lt;A target=_blank href="http://www.functional-programming.net/"&gt;^&lt;/A&gt;] - book homepage 
&lt;LI&gt;&lt;A href="http://www.manning.com/petricek/"&gt;Manning: Real-world Functional Programming in .NET&lt;/A&gt; [&lt;A target=_blank href="http://www.manning.com/petricek/"&gt;^&lt;/A&gt;] - book page at Manning.Com 
&lt;LI&gt;&lt;A href="http://www.manning-sandbox.com/forum.jspa?forumID=460"&gt;Forum dedicated to the book&lt;/A&gt; [&lt;A target=_blank href="http://www.manning-sandbox.com/forum.jspa?forumID=460"&gt;^&lt;/A&gt;] - at Manning.Com&lt;/LI&gt;&lt;/UL&gt;&lt;img src="http://cs.hubfs.net/aggbug.aspx?PostID=7614" width="1" height="1"&gt;</description></item><item><title>F# Support for ASP.NET and Notes on Samples</title><link>http://cs.hubfs.net/blogs/tomasp/archive/2008/03/08/aspnet_in_fsharp.aspx</link><pubDate>Sun, 09 Mar 2008 07:36:00 GMT</pubDate><guid isPermaLink="false">7372db05-f90c-40e3-82a2-789ed9f521c9:5244</guid><dc:creator>tomasp</dc:creator><slash:comments>2</slash:comments><comments>http://cs.hubfs.net/blogs/tomasp/comments/5244.aspx</comments><wfw:commentRss>http://cs.hubfs.net/blogs/tomasp/commentrss.aspx?PostID=5244</wfw:commentRss><description>&lt;P&gt;&lt;EM&gt;(This article is cross-posted from &lt;/EM&gt;&lt;A href="http://tomasp.net/articles/aspnet-in-fsharp.aspx"&gt;&lt;EM&gt;http://tomasp.net/articles/aspnet-in-fsharp.aspx&lt;/EM&gt;&lt;/A&gt;&lt;EM&gt;)&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;As I mentioned earlier, I spent three months as an intern in Microsoft Research in Cambridge last year and I was working with Don Syme and James Margetson from the F# team. Most of the time I was working on the F# Web Toolkit, which I introduced on the blog some time ago [&lt;A&gt;1&lt;/A&gt;], but I also worked on a few additions that are now part of the F# release. Probably the most useful addition is a new implementation of the CodeDOM provider for the F# language which makes it possible to use ASP.NET smoothly from F# (but it can be used in some other scenarios as well) together with two ASP.NET sample applications that you can explore and use as a basis for your web sites. This was actually a part of the distribution for a few months now (I of course wanted to write this article much earlier...), so you may have already noticed, but anyway, I'd still like to write down a short description of these ASP.NET samples and also a few tips for those who're interested in writing web applications in F#. &lt;/P&gt;
&lt;H2&gt;F# and ASP.NET&lt;/H2&gt;
&lt;P&gt;Let's start by looking at the ASP.NET examples. You can find them in the &lt;CODE&gt;samples&lt;/CODE&gt; directory in your F# installation under the &lt;CODE&gt;Web/ASP.NET&lt;/CODE&gt; path. The directory also contains &lt;CODE&gt;html&lt;/CODE&gt; files with description of the projects and a guide to configuring them, but I'll describe both of these topics in this post. The distribution contains two sample projects: &lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;AspNetIntro&lt;/STRONG&gt; - this project is (almost) the simples possible F# web site, so it can be used as a template for your web sites. It shows how to configure the CodeDOM provider, how to write a simple page with code-behind and how to use the &lt;CODE&gt;App_Code&lt;/CODE&gt; directory and data-binding. &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;PersonalWebSite&lt;/STRONG&gt; - this is a more complex web site ported from the C# sample called &lt;STRONG&gt;Personal Web Site Starter Kit&lt;/STRONG&gt; [&lt;A&gt;2&lt;/A&gt;]. It demonstrates many of the standard ASP.NET 2.0 techniques including data access controls, master pages, membership and custom HTTP handlers.&lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;ASP.NET Introduction using F#&lt;/H2&gt;
&lt;P&gt;To start playing with ASP.NET you'll need to open the project (I recommend copying it to your working directory first). If you're using Visual Studio, you can select &lt;CODE&gt;File - Open - Web Site...&lt;/CODE&gt; in the menu and select the directory with your project as demonstrated at Figure 1 below. The organization of ASP.NET projects is different than organization of ordinary F# projects - in ASP.NET the project is just a directory and it contains all the files in the directory (this is also the reason why you have to open it using a different command). The Figure 2 shows how the files of the ASP.NET Introduction project are organized in the Solution Explorer: &lt;/P&gt;
&lt;DIV&gt;
&lt;DIV style="MARGIN-LEFT: auto; WIDTH: 650px; MARGIN-RIGHT: auto"&gt;
&lt;DIV style="FLOAT: left; MARGIN: 10px; TEXT-ALIGN: center"&gt;&lt;A href="http://tomasp.net/articles/aspnet-in-fsharp/openweb.png" target=_blank&gt;&lt;IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; MARGIN-LEFT: auto; BORDER-LEFT: 0px; MARGIN-RIGHT: auto; BORDER-BOTTOM: 0px" alt="Open Web Site in Visual Studio" src="http://tomasp.net/articles/aspnet-in-fsharp/openweb_sm.png"&gt;&lt;/A&gt;&lt;BR&gt;Figure 1: Open Web Site&lt;/DIV&gt;
&lt;DIV style="FLOAT: left; MARGIN: 10px; TEXT-ALIGN: center"&gt;&lt;IMG alt="Solution Explorer" src="http://tomasp.net/articles/aspnet-in-fsharp/solutionexp.png"&gt;&lt;BR&gt;Figure 2: Solution Explorer&lt;/DIV&gt;
&lt;DIV style="CLEAR: both"&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;As you can see, there are 6 files in the project. The &lt;CODE&gt;Default.aspx.fs&lt;/CODE&gt; and &lt;CODE&gt;Default.aspx&lt;/CODE&gt; together form one web page and the &lt;CODE&gt;DataBinding.aspx.fs&lt;/CODE&gt; with &lt;CODE&gt;DataBinding.aspx&lt;/CODE&gt; form the second web page. The &lt;CODE&gt;App_Code&lt;/CODE&gt; directory contains application logic that can be used from other pages in the project and in our sample project it contains only one file (&lt;CODE&gt;logic.fs&lt;/CODE&gt;). Finally, the &lt;CODE&gt;web.config&lt;/CODE&gt; file contains configuration of the whole application.&lt;/P&gt;
&lt;P&gt;Before we look at the pages you may want to check the &lt;CODE&gt;web.config&lt;/CODE&gt; file, because it needs to contain the correct reference to the CodeDOM provider implementation including a version of the current F# installation. At the time of writing this article, the latest version is &lt;CODE&gt;1.9.3.14&lt;/CODE&gt;, but if you're not sure what version you are using, you can just start the &lt;CODE&gt;fsi.exe&lt;/CODE&gt; from the F# installation which prints the version number. The &lt;CODE&gt;web.config&lt;/CODE&gt; file is an &lt;CODE&gt;xml&lt;/CODE&gt; file and it should contain the following content (with the right version number). The samples in the distribution should contain the correct version number, but the incorrect configuration is a common issue when working with ASP.NET in F#, so it is useful to know what the configuration should look like:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=asp&gt;&amp;lt;?&lt;/SPAN&gt;xml version=&lt;SPAN class=str&gt;"1.0"&lt;/SPAN&gt;&lt;SPAN class=asp&gt;?&amp;gt;&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;configuration&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;system.web&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;compilation&lt;/SPAN&gt; &lt;SPAN class=attr&gt;debug&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="true"&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;  
  &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;compilers&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;compiler&lt;/SPAN&gt; 
      &lt;SPAN class=attr&gt;language&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="F#;f#;fs;fsharp"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;extension&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;=".fs"&lt;/SPAN&gt; 
      &lt;SPAN class=attr&gt;type&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="Microsoft.FSharp.Compiler.CodeDom.FSharpAspNetCodeProvider, 
            FSharp.Compiler.CodeDom, Version=&amp;lt;strong style="&lt;/SPAN&gt;&lt;SPAN class=attr&gt;color:red&lt;/SPAN&gt;;"&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;1.9.3.14&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;strong&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;, 
            Culture=neutral, PublicKeyToken=a19089b1c74d0809"&lt;SPAN class=kwrd&gt;/&amp;gt;&lt;/SPAN&gt;
  &lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;compilers&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;compilation&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;system.web&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;configuration&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
&lt;/PRE&gt;
&lt;P&gt;Now, let's look at the &lt;CODE&gt;Default.aspx&lt;/CODE&gt; and &lt;CODE&gt;Default.aspx.fs&lt;/CODE&gt; files that together represent a simple page. The page contains one button and one label (a control that can display some text) and when the user clicks on the button, the result of some calculation is displayed in the label (the calculation is executed on the server-side). The following code is a (slightly simplified) content of the &lt;CODE&gt;Default.aspx&lt;/CODE&gt; file, which defines the HTML markup together with the ASP.NET controls that are on the page:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=asp&gt;&amp;lt;%@ Page Language="F#" 
  CodeFile="Default.aspx.fs" Inherits="FSharpWeb.Default" %&amp;gt;&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;html&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;body&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;form&lt;/SPAN&gt; &lt;SPAN class=attr&gt;runat&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="server"&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
  &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;asp:Button&lt;/SPAN&gt; 
    &lt;SPAN class=attr&gt;ID&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="btnTest"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;RunAt&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="server"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;Text&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="Click me!"&lt;/SPAN&gt; 
    &lt;SPAN class=attr&gt;OnClick&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="ButtonClicked"&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;/&amp;gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;br&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;/&amp;gt;&lt;/SPAN&gt;
  &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;asp:Label&lt;/SPAN&gt; &lt;SPAN class=attr&gt;ID&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="lblResult"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;RunAt&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="server"&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;/&amp;gt;&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;form&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;body&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;html&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
&lt;/PRE&gt;
&lt;P&gt;You can easily identify two server-side controls, because these are written using prefix &lt;CODE&gt;asp&lt;/CODE&gt; and also contain the &lt;CODE&gt;RunAt="server"&lt;/CODE&gt; attribute, which means that the control is processed on the server-side. You can use standard HTML notation for setting attributes of the controls. We set the &lt;CODE&gt;ID&lt;/CODE&gt; attribute to both of the controls, which is important because it allows us to use them in the code-behind code. The &lt;CODE&gt;OnClick&lt;/CODE&gt; attribute of the button sets an event handler - a member of the &lt;CODE&gt;FSharpWeb.Default&lt;/CODE&gt; type from the code-behind file that will be called on the server-side, when a user clicks on the button (which submits the HTML form and causes a page reload, so the event can be processed on the server). The file also contains the first special line, which tells the ASP.NET engine that the page is written in F# and it also tells what source file contains the code-behind code and what is the name of type declared in that file. The code-behind file (&lt;CODE&gt;Default.aspx.fs&lt;/CODE&gt;) has the following content:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=preproc&gt;#light&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;namespace&lt;/SPAN&gt; FSharpWeb
&lt;SPAN class=kwrd&gt;open&lt;/SPAN&gt; System
&lt;SPAN class=kwrd&gt;open&lt;/SPAN&gt; System.Web
&lt;SPAN class=kwrd&gt;open&lt;/SPAN&gt; System.Web.UI.WebControls

&lt;SPAN class=kwrd&gt;type&lt;/SPAN&gt; Default() =
  &lt;SPAN class=kwrd&gt;inherit&lt;/SPAN&gt; Page()
  
  &lt;SPAN class=op&gt;[&lt;/SPAN&gt;&amp;lt;DefaultValue&amp;gt;&lt;SPAN class=op&gt;]&lt;/SPAN&gt;
  &lt;SPAN class=kwrd&gt;val&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;mutable&lt;/SPAN&gt; btnTest : Button

  &lt;SPAN class=op&gt;[&lt;/SPAN&gt;&amp;lt;DefaultValue&amp;gt;&lt;SPAN class=op&gt;]&lt;/SPAN&gt;
  &lt;SPAN class=kwrd&gt;val&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;mutable&lt;/SPAN&gt; lblResult : Label

  &lt;SPAN class=kwrd&gt;member&lt;/SPAN&gt; this.ButtonClicked(sender, e) =
    this.lblResult.Text &lt;SPAN class=op&gt;&amp;lt;-&lt;/SPAN&gt; 
      (sprintf &lt;SPAN class=str&gt;"Factorial of 5 is: %d"&lt;/SPAN&gt; 
               (FSharpWeb.Logic.factorial 5))
&lt;/PRE&gt;
&lt;P&gt;As we can see, the file contains an F# object type (named &lt;CODE&gt;Default&lt;/CODE&gt;), inherited from the base ASP.NET &lt;CODE&gt;Page&lt;/CODE&gt; type. Thanks to the recent improvements in F# it is possible to write the type using the new implicit class syntax (note the parentheses after the type name), which means that you can place additional initialization code to the type declaration directly (following the &lt;CODE&gt;inherit&lt;/CODE&gt; clause and F# will treat this as a constructor.&lt;/P&gt;
&lt;P&gt;The class contains a mutable field for every control declared in the declarative markup with the name same as the &lt;CODE&gt;ID&lt;/CODE&gt; attribute in the markup file (&lt;CODE&gt;btnTest&lt;/CODE&gt; and &lt;CODE&gt;lblResult&lt;/CODE&gt;). The ASP.NET initializes these controls automatically when it creates the page (that's why the fields are marked using &lt;CODE&gt;mutable&lt;/CODE&gt;), so we don't need to initialize these fields in our code - F# doesn't usually allow uninitialized fields, but if you place the &lt;CODE&gt;DefaultValue&lt;/CODE&gt; before the field declaration, it will be initialized to the default value (&lt;CODE&gt;null&lt;/CODE&gt;), which is correct, because the field will be later initialized by the ASP.NET runtime. Finally, the type contains a member called &lt;CODE&gt;ButtonClicked&lt;/CODE&gt;, which is a same name we used in the markup for the &lt;CODE&gt;OnClick&lt;/CODE&gt; attribute of the button control. This is an event handler that will be called when user clicks on the button, it performs some calculation (the &lt;CODE&gt;factorial&lt;/CODE&gt; function is declared in the &lt;CODE&gt;logic.fs&lt;/CODE&gt; file) and sets a property of the other control that we have on the page. &lt;/P&gt;
&lt;H2&gt;Personal Web Site in F#&lt;/H2&gt;
&lt;DIV style="FLOAT: right; MARGIN: 10px; TEXT-ALIGN: center"&gt;&lt;A href="http://tomasp.net/articles/aspnet-in-fsharp/pws.png" target=_blank&gt;&lt;IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; MARGIN-LEFT: auto; BORDER-LEFT: 0px; MARGIN-RIGHT: auto; BORDER-BOTTOM: 0px" alt="Personal Web Site" src="http://tomasp.net/articles/aspnet-in-fsharp/pws_sm.png"&gt;&lt;/A&gt;&lt;BR&gt;Figure 3: Personal Web Site&lt;/DIV&gt;
&lt;P&gt;The second sample ASP.NET project is a port of quite a complex ASP.NET web site which also uses MS SQL database. The database can be created manually as described in &lt;CODE&gt;Welcome.html&lt;/CODE&gt; in the &lt;CODE&gt;PersonalWebSite&lt;/CODE&gt; directory, but I also uploaded the database to my web, so you can just download it and attach it in the SQL server (this is described below and the files for download can be found at the end of the article). The &lt;CODE&gt;PWS_AspNetDb&lt;/CODE&gt; is a database managed by ASP.NET that contains user information - the database below already contains the required role (called &lt;CODE&gt;Administrators&lt;/CODE&gt;) and one user (&lt;CODE&gt;admin&lt;/CODE&gt; with password &lt;CODE&gt;admin123!&lt;/CODE&gt;). The second database file (&lt;CODE&gt;PWS_WebPersonal&lt;/CODE&gt;) contains the application data, mainly information about photos and galleries (as you can see from the screenshot in Figure 3). &lt;/P&gt;
&lt;P&gt;The application uses many advanced ASP.NET features including the following:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;Master Pages&lt;/STRONG&gt; - the overall structure of the web site is stored in a master page (&lt;CODE&gt;Default.master&lt;/CODE&gt;) and the other pages use the &lt;CODE&gt;asp:Content&lt;/CODE&gt; control to fill the place holders in the master page.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Site Maps&lt;/STRONG&gt; - the map of the web site is defined in the &lt;CODE&gt;web.sitemap&lt;/CODE&gt; file and is used for generating the menu in the &lt;CODE&gt;Default.master&lt;/CODE&gt; file.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Themes&lt;/STRONG&gt; - the design of the page including CSS files and images are managed using ASP.NET Themes - there are two standard themes in the &lt;CODE&gt;App_Themes&lt;/CODE&gt; directory.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Membership&lt;/STRONG&gt; - the web uses standard ASP.NET technology for managing user accounts including the controls for manipulating with users (e.g. &lt;CODE&gt;asp:CreateUserWizard&lt;/CODE&gt; control is used in &lt;CODE&gt;Register.aspx&lt;/CODE&gt; file).&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Data Controls&lt;/STRONG&gt; - ASP.NET data-access controls are used for reading the data from the application logic layer (implemented as a module in F#)&lt;/LI&gt;&lt;/UL&gt;
&lt;H3&gt;Configuring the databases&lt;/H3&gt;
&lt;P&gt;To configure the databases required by the demo, you can either follow the steps described in the &lt;A href="http://tomasp.net/articles/aspnet-in-fsharp/Welcome.html"&gt;Welcome.html&lt;/A&gt; file (which is part of the sample project), or if you already have some version of SQL Server installed on your machine, you can download the sample databases below (a bunch of MDF and LDF files) and attach them to the SQL Server. I'll explain how to do this using full version of SQL Server, but the steps needed with Express edition are very similar.&lt;/P&gt;
&lt;P&gt;Once you downloaded the files, you'll need to launch SQL Server Management Studio and connect to the server instance. After doing that you should see an "Object Explorer" window, where you can right click on the "Databases" group and select "Attach..." from the pop-up menu. In the opened dialog window, you can add the databases (by selecting the MDF file) and click the "OK" button to attach the databases. Finally, you'll need to modify the &lt;CODE&gt;web.config&lt;/CODE&gt; file in the Personal WebSite sample to include the following section: &lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;connectionStrings&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
 &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;add&lt;/SPAN&gt; &lt;SPAN class=attr&gt;name&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="Personal"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;providerName&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="System.Data.SqlClient"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;connectionString&lt;/SPAN&gt;=
      &lt;SPAN class=kwrd&gt;"Data Source=.;Integrated Security=True;Database=PWS_WebPersonal"&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;/&amp;gt;&lt;/SPAN&gt;
 &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;remove&lt;/SPAN&gt; &lt;SPAN class=attr&gt;name&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="LocalSqlServer"&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;/&amp;gt;&lt;/SPAN&gt;
 &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;add&lt;/SPAN&gt; &lt;SPAN class=attr&gt;name&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="LocalSqlServer"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;connectionString&lt;/SPAN&gt;=
      &lt;SPAN class=kwrd&gt;"Data Source=.;Integrated Security=True;Database=PWS_AspNetDb"&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;/&amp;gt;&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;connectionStrings&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
&lt;/PRE&gt;
&lt;P&gt;The &lt;CODE&gt;Data Source&lt;/CODE&gt; parameter in the connection string specifies the instance of the SQL Server, where the databases are attached. By default this is the name of your computer (which can be written shortly using dot "."). When using the SQL Express the default instance name is ".\SQLExpress". The &lt;CODE&gt;Database&lt;/CODE&gt; parameter specifies the name of the database, which you can see in the "Object Explorer" in the SQL Server Management Studio. After following these steps, you should be able to run the demo application.&lt;/P&gt;
&lt;H3&gt;Data-access in F#&lt;/H3&gt;
&lt;P&gt;Most of the ASP.NET technologies used in the web site are used in almost the same way as in C#, so I will not discuss them in larger details, but the last item in the list - the use of ASP.NET data controls together with F# is I believe quite interesting, so I'll write a few notes about it. Let's look for example at the &lt;CODE&gt;Photos.aspx&lt;/CODE&gt; file, which displays a list of photos in a gallery. The most important parts of the file related to data access are shown in the following code snippet:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;asp:DataList&lt;/SPAN&gt; &lt;SPAN class=attr&gt;RunAt&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="server"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;DataSourceID&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="photoSource"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;EnableViewState&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="false"&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;ItemTemplate&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
  &lt;SPAN class=rem&gt;&amp;lt;!-- ... --&amp;gt;&lt;/SPAN&gt;
  &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;a&lt;/SPAN&gt; &lt;SPAN class=attr&gt;href&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;='Details.aspx?AlbumID=&amp;lt;%# base.Eval("PhotoAlbumID") %&amp;gt;'&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;img&lt;/SPAN&gt; &lt;SPAN class=attr&gt;src&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="Handler.ashx?PhotoID=&amp;lt;%# base.Eval("&lt;/SPAN&gt;&lt;SPAN class=attr&gt;PhotoID&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;") %&amp;gt;&amp;amp;Size=S"&lt;/SPAN&gt; 
      &lt;SPAN class=attr&gt;class&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="photo_198"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;style&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="border:4px solid white"&lt;/SPAN&gt; 
      &lt;SPAN class=attr&gt;alt&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;='Thumbnail of Photo Number &amp;lt;%# base.Eval("PhotoID") %&amp;gt;'&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;/&amp;gt;&lt;/SPAN&gt;
  &lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;a&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
  &lt;SPAN class=rem&gt;&amp;lt;!-- ... --&amp;gt;&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;ItemTemplate&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;asp:DataList&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;

&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;asp:ObjectDataSource&lt;/SPAN&gt; &lt;SPAN class=attr&gt;ID&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="photoSource"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;RunAt&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="server"&lt;/SPAN&gt; 
  &lt;SPAN class=attr&gt;TypeName&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="PersonalWebSite.PhotoManager"&lt;/SPAN&gt; 
  &lt;SPAN class=attr&gt;SelectMethod&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="GetPhotos"&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
  &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;SelectParameters&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;asp:QueryStringParameter&lt;/SPAN&gt; &lt;SPAN class=attr&gt;Name&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="AlbumID"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;Type&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="Int32"&lt;/SPAN&gt; 
      &lt;SPAN class=attr&gt;QueryStringField&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="albumID"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;DefaultValue&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="0"&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;/&amp;gt;&lt;/SPAN&gt;
  &lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;SelectParameters&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;asp:ObjectDataSource&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
&lt;/PRE&gt;
&lt;P&gt;The first ASP.NET control used in the code is &lt;CODE&gt;asp:DataList&lt;/CODE&gt;, which is a control that simply displays a collection of some items and uses a specified &lt;CODE&gt;ItemTemplate&lt;/CODE&gt; for displaying every single item. In our case we're working with collection of records and the record has several members (including &lt;CODE&gt;PhotoID&lt;/CODE&gt; and &lt;CODE&gt;PhotoAlbumID&lt;/CODE&gt;). To display a value of a record member in the ASP.NET page we have to use ASP.NET &lt;CODE&gt;Eval&lt;/CODE&gt; method as you can see in the sample. The &lt;CODE&gt;Eval&lt;/CODE&gt; method reads the specified member of the item in collection and writing the code in &lt;CODE&gt;&amp;lt;%# ... %&amp;gt;&lt;/CODE&gt; tells ASP.NET that we want to render the result of the expression as a part of the item template. The record that we're using in this example is very simple and has the following structure:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=kwrd&gt;type&lt;/SPAN&gt; Photo = 
  { PhotoID:int; 
    PhotoAlbumID:int; 
    PhotoCaption:string; }
&lt;/PRE&gt;
&lt;P&gt;This is actually one of the places where the F# version is shorter, because in C# version you have to write this as a class with properties, which makes the code quite longer.&lt;/P&gt;
&lt;P&gt;The second server-side control (&lt;CODE&gt;asp:ObjectDataSource&lt;/CODE&gt;) in the first code snippet isn't visual, which means that it will not produce any HTML output. It serves just as a declarative data-source for the data list and the properties of this control specify how the content for the data list should be loaded. There are various other data-source controls in ASP.NET (e.g. &lt;CODE&gt;asp:SqlDataSource&lt;/CODE&gt; which loads data directly from the database), but the one that we're using in the example uses a specified .NET/F# type and calls a method of the given object when it needs to retrieve the data. In F# this is even easier, because we can use a name of the module and the code that retrieves the data can be a function in the module.&lt;/P&gt;
&lt;P&gt;As you can see the property &lt;CODE&gt;TypeName&lt;/CODE&gt; is set to &lt;CODE&gt;PersonalWebSite.PhotoManager&lt;/CODE&gt;, which can be treated as a module called &lt;CODE&gt;PhotoManager&lt;/CODE&gt; in a namespace &lt;CODE&gt;PersonalWebSite&lt;/CODE&gt; and the &lt;CODE&gt;SelectMethod&lt;/CODE&gt; property, which specifies a name of the function that will be called when loading the data is set to &lt;CODE&gt;GetPhotos&lt;/CODE&gt;. Finally, the &lt;CODE&gt;SelectParameters&lt;/CODE&gt; specifies that the function expects one argument called &lt;CODE&gt;AlbumID&lt;/CODE&gt; and that the value of this argument should be retrieved from the query string (that is from the URL of form &lt;CODE&gt;photo.aspx?albumIdD=42&lt;/CODE&gt;).&lt;/P&gt;
&lt;P&gt;Now it is pretty easy to implement the module that matches this specifications and can be used as a data source (we could of course use FLinq for loading the data, but I wanted to keep things simple for now, so we're using standard &lt;CODE&gt;SqlDataReader&lt;/CODE&gt; object from .NET):&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=kwrd&gt;namespace&lt;/SPAN&gt; PersonalWebSite
&lt;SPAN class=rem&gt;// ...&lt;/SPAN&gt;

&lt;SPAN class=kwrd&gt;module&lt;/SPAN&gt; PhotoManager = 
  &lt;SPAN class=rem&gt;// Function will be called by ASP.NET when loading data&lt;/SPAN&gt;
  &lt;SPAN class=rem&gt;// for the asp:DataList control in the 'Photos.aspx' page&lt;/SPAN&gt;
  &lt;SPAN class=kwrd&gt;let&lt;/SPAN&gt; GetPhotos (albumID:int) =
    &lt;SPAN class=rem&gt;// Open connection &amp;amp; create command&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;use&lt;/SPAN&gt; conn = &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; SqlConnection(&lt;SPAN class=str&gt;" ... "&lt;/SPAN&gt;)
    &lt;SPAN class=kwrd&gt;use&lt;/SPAN&gt; cmd  = &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; SqlCommand(&lt;SPAN class=str&gt;"GetPhotos"&lt;/SPAN&gt;, conn)
    
    &lt;SPAN class=rem&gt;// Can the user see 'private' photos?&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;let&lt;/SPAN&gt; usr = HttpContext.Current.User
    &lt;SPAN class=kwrd&gt;let&lt;/SPAN&gt; filter = not (usr.IsInRole(&lt;SPAN class=str&gt;"Friends"&lt;/SPAN&gt;) &lt;SPAN class=op&gt;|&lt;/SPAN&gt;&lt;SPAN class=op&gt;|&lt;/SPAN&gt; usr.IsInRole(&lt;SPAN class=str&gt;"Administrators"&lt;/SPAN&gt;))
    cmd.CommandType &lt;SPAN class=op&gt;&amp;lt;-&lt;/SPAN&gt; CommandType.StoredProcedure)
    cmd.Parameters.Add(SqlParameter(&lt;SPAN class=str&gt;"@AlbumID"&lt;/SPAN&gt;, albumID)) &lt;SPAN class=op&gt;|&lt;/SPAN&gt;&amp;gt; ignore
    command.Parameters.Add(SqlParameter(&lt;SPAN class=str&gt;"@IsPublic"&lt;/SPAN&gt;, filter)) &lt;SPAN class=op&gt;|&lt;/SPAN&gt;&amp;gt; ignore
    conn.Open()
    
    &lt;SPAN class=rem&gt;// Read all photos into a .NET ResizeArray type&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;let&lt;/SPAN&gt; list = &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; ResizeArray&amp;lt;&lt;SPAN class=op&gt;_&lt;/SPAN&gt;&amp;gt;()
    &lt;SPAN class=kwrd&gt;use&lt;/SPAN&gt; reader = command.ExecuteReader()
    &lt;SPAN class=kwrd&gt;while&lt;/SPAN&gt; (reader.Read()) &lt;SPAN class=kwrd&gt;do&lt;/SPAN&gt;
      list.Add({ PhotoID = unbox (reader.get_Item(&lt;SPAN class=str&gt;"PhotoID"&lt;/SPAN&gt;)) 
                 PhotoAlbumID = unbox (reader.get_Item(&lt;SPAN class=str&gt;"AlbumID"&lt;/SPAN&gt;)) 
                 PhotoCaption = unbox (reader.get_Item(&lt;SPAN class=str&gt;"Caption"&lt;/SPAN&gt;)) }) 
                 
    &lt;SPAN class=rem&gt;// Return the created collection&lt;/SPAN&gt;
    list 
&lt;/PRE&gt;
&lt;P&gt;I hope this article explained some of the interesting things that you may find in the ASP.NET samples provided with the F# distribution and I think that the Personal WebSite Sample shows that F# can be used for writing quite complicated web applications as well. Of course, my initial motivation for writing web applications in F# is the possibility of using meta-programming to develop client-side code (the code that runs on the client as a JavaScript) in F# too, which makes development of modern "Ajax"-style applications easier, so if you're interested in this project you may want to look at F# Web Tools [&lt;A&gt;1&lt;/A&gt;].&lt;/P&gt;
&lt;H2&gt;Downloads &amp;amp; References &lt;A name=myfsstuff&gt;&lt;/H2&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://tomasp.net/articles/aspnet-in-fsharp/databases.zip"&gt;Download PWS Database Files&lt;/A&gt; (9.65 MB)&lt;/LI&gt;&lt;/UL&gt;
&lt;UL&gt;
&lt;LI&gt;[1] &lt;A href="http://tomasp.net/blog/fswebtools-intro.aspx"&gt;F# Web Toolkit: "Ajax" applications made simple&lt;/A&gt; [&lt;A href="http://tomasp.net/blog/fswebtools-intro.aspx" target=_blank&gt;^&lt;/A&gt;] - Blog | TomasP.Net&lt;/LI&gt;
&lt;LI&gt;[2] &lt;A href="http://msdn2.microsoft.com/en-us/express/aa700818.aspx#personal"&gt;Visual Web Developer Express: Samples and Starter Kits&lt;/A&gt; [&lt;A href="http://msdn2.microsoft.com/en-us/express/aa700818.aspx#personal" target=_blank&gt;^&lt;/A&gt;] - Microsoft.Com&lt;/LI&gt;&lt;/UL&gt;&lt;/DIV&gt;&lt;img src="http://cs.hubfs.net/aggbug.aspx?PostID=5244" width="1" height="1"&gt;</description></item><item><title>F# Overview - Introduction to the F# language</title><link>http://cs.hubfs.net/blogs/tomasp/archive/2007/11/03/fsharp_introduction.aspx</link><pubDate>Sat, 03 Nov 2007 10:36:00 GMT</pubDate><guid isPermaLink="false">7372db05-f90c-40e3-82a2-789ed9f521c9:3924</guid><dc:creator>tomasp</dc:creator><slash:comments>7</slash:comments><comments>http://cs.hubfs.net/blogs/tomasp/comments/3924.aspx</comments><wfw:commentRss>http://cs.hubfs.net/blogs/tomasp/commentrss.aspx?PostID=3924</wfw:commentRss><description>&lt;P&gt;In my bachelor thesis I included a short introduction that covered all of the important aspects of the F# programming language and I thought that it may be useful to extend it a little bit to cover also a topics that were not important for my thesis and post it as an article, so there is one and relatively short article that introduces all the interesting F# features. The article got however a bit longer than I expected, so I decided to split it into a three parts that would introduce three different &lt;EM&gt;paradigms&lt;/EM&gt; that are supported by F#. Of course, this series won’t teach you everything about F#, but it tries to cover the main F# design goals and (hopefully) presents all the features that make F# interesting and worth learning. In this first part I will shortly introduce F# and the supported paradigms that will be discussed in the upcoming articles.&lt;/P&gt;
&lt;H2&gt;Introducing F#&lt;/H2&gt;
&lt;P&gt;In one of the papers about F#, the F# designers gave the following description: &lt;EM&gt;"F# is a multi-paradigm .NET language explicitly designed to be an ML suited to the .NET environment. It is rooted in the Core ML design and in particular has a core language largely compatible with OCaml"&lt;/EM&gt;. In other words this means that the syntax of the F# language is similar to ML or OCaml (don’t worry if you don’t know these languages, we’ll look at some examples shortly), but the F# language targets .NET Framework, which means that it can natively work with other .NET components and also that it contains several language extensions to allow smooth integration with the .NET object system. &lt;/P&gt;
&lt;P&gt;Another important aspect mentioned in this description is that F# is &lt;EM&gt;multi-paradigm&lt;/EM&gt; language. This means that it tries to take the best from many programming languages from very different worlds. The first paradigm is &lt;EM&gt;functional programming&lt;/EM&gt; (the languages that largely influenced the design of F# in this area are ML, OCaml and other), which has a very long tradition and is becoming more important lately for some very appealing properties, including the fact that functional code tends to be easier to test and paralellize and is also extensible in a ways where object oriented code makes extending difficult. &lt;/P&gt;
&lt;P&gt;The second paradigm is widely adopted &lt;EM&gt;object oriented&lt;/EM&gt; programming, which enables interoperability with other .NET languages. In F# it is often used for implementing elementary data types (meaning that the operations on the type are well known and change very rarely), for grouping a set of elementary functions that are together used to perform some complicated operation (i.e. implementing an interface) and also when working with object oriented user interface frameworks. &lt;/P&gt;
&lt;P&gt;Finally, the third paradigm supported by F# is &lt;EM&gt;language oriented programming&lt;/EM&gt; (the design of F# in this area is largely influenced by ML, Haskell and also by LINQ). In general, language oriented programming is focused on developing executors for some code which has a structure of a language (be it a declarative language like XML, or a fully powerful language like some subset of F#). In this overview, I will focus on two techniques provided by F# that allow you to give a different meaning to a blocks of F# code. In a programming language theory, this is often called &lt;EM&gt;internal domain specific languages&lt;/EM&gt;, because the code is written in the host language, but is specifically designed as a way for solving problems from some specific &lt;EM&gt;domain&lt;/EM&gt;. An example of such language (and an associated executor) is a block of code that is written as a linear code, but is executed asynchronously (in F# this can be implemented using &lt;EM&gt;computation expressions&lt;/EM&gt;), or a query that is written in F#, but is executed as a SQL code by some database server (this can be implemented using F# &lt;EM&gt;quotations&lt;/EM&gt;).&lt;/P&gt;
&lt;H2&gt;F# Overview - Links&lt;/H2&gt;
&lt;P&gt;In the rest of this article series we will look at all these three paradigms supported by F# starting with functional programming and basic F# types used when writing code in a functional way, continuing with object oriented programming and the support for .NET interoperability which is closely related to the OOP in F#. Lastly, we will look at the language oriented programming paradigm including some of the most important .NET and F# library functions that make it possible. The rest of the articles from the series are available in &lt;A href="http://tomasp.net/blog"&gt;my other blog&lt;/A&gt;:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://tomasp.net/articles/fsharp-i-introduction.aspx"&gt;F# Overview (I.) - Introduction&lt;/A&gt; (at TomasP.Net) 
&lt;LI&gt;&lt;A href="http://tomasp.net/articles/fsharp-ii-functional.aspx"&gt;F# Overview (II.) - Functional Programming&lt;/A&gt; (at TomasP.Net) 
&lt;LI&gt;&lt;A href="http://tomasp.net/articles/fsharp-iii-oop.aspx"&gt;F# Overview (III.) - Object Oriented and Imperative Programming &lt;/A&gt;(at TomasP.Net) 
&lt;LI&gt;&lt;A href="http://tomasp.net/articles/fsharp-iv-lang.aspx"&gt;F# Overview (IV.) - Language Oriented Programming&lt;/A&gt;(at TomasP.Net)&lt;/LI&gt;&lt;/UL&gt;
&lt;UL&gt;
&lt;LI&gt;You can also download the complete article as &lt;A href="http://tomasp.net/articles/fsharp-i-introduction/article.pdf"&gt;a single document&lt;/A&gt; (PDF).&lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;Other F# Resources&lt;/H2&gt;
&lt;P&gt;There are many other places where you can find useful information about F#. First of all, there is an official F# web site [1] where you can find the language specification, documentation and other useful resources. There are also two books written about F# (one already published [2], second will be available soon [3]). Lastly, there are also a lot of community resources including an F# community web site with discussion board [4], wiki [5] and several blogs [6,7,8,9]. Finally, there are also some projects developed by the F# community that are available at CodePlex - the first one includes various F# code samples [9, 10] and the second is based on my thesis and tries to solve several web development problems [11].&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;[1] &lt;A href="http://research.microsoft.com/fsharp/"&gt;F# web site&lt;/A&gt; [&lt;A href="http://research.microsoft.com/fsharp/" target=_blank&gt;^&lt;/A&gt;] - Official F# homepage 
&lt;LI&gt;[2] &lt;A href="http://www.amazon.com/Expert-F-Don-Syme/dp/1590598504"&gt;Expert F#&lt;/A&gt; [&lt;A href="http://www.amazon.com/Expert-F-Don-Syme/dp/1590598504" target=_blank&gt;^&lt;/A&gt;] - Book by by Don Syme, Adam Granicz and Antonio Cisternino 
&lt;LI&gt;[3] &lt;A href="http://www.amazon.com/Foundations-F-Robert-Pickering/dp/1590597575"&gt;Foundations of F#&lt;/A&gt; [&lt;A href="http://www.amazon.com/Foundations-F-Robert-Pickering/dp/1590597575" target=_blank&gt;^&lt;/A&gt;] - Book by Robert Pickering 
&lt;LI&gt;[4] &lt;A href="/"&gt;hubFS: THE place for F#&lt;/A&gt; [&lt;A href="/" target=_blank&gt;^&lt;/A&gt;] - The F# community web site with blogs, forums, etc.. 
&lt;LI&gt;[5] &lt;A href="http://www.strangelights.com/fsharp/wiki/"&gt;F# Wiki Homepage&lt;/A&gt; [&lt;A href="http://www.strangelights.com/fsharp/wiki/" target=_blank&gt;^&lt;/A&gt;] - F# Wiki started by Robert Pickering 
&lt;LI&gt;[&amp;#54;] &lt;A href="http://blogs.msdn.com/dsyme/"&gt;Don Syme’s WebLog on F# and Other Research Projects&lt;/A&gt; [&lt;A href="http://blogs.msdn.com/dsyme/" target=_blank&gt;^&lt;/A&gt;] - Blog written by the F# language designer Don Syme 
&lt;LI&gt;[7] &lt;A href="http://www.strangelights.com/blog/"&gt;Robert Pickering’s Strange Blog&lt;/A&gt; [&lt;A href="http://www.strangelights.com/blog/" target=_blank&gt;^&lt;/A&gt;] - Blog of the "Foundations of F#" book author 
&lt;LI&gt;[&amp;#56;] &lt;A href="http://weblogs.asp.net/gbarnett/"&gt;Granville Barnett&lt;/A&gt; [&lt;A href="http://weblogs.asp.net/gbarnett/" target=_blank&gt;^&lt;/A&gt;] - Explorations in programming 
&lt;LI&gt;[9] &lt;A href="http://fsharpnews.blogspot.com/"&gt;F# News&lt;/A&gt; [&lt;A href="http://fsharpnews.blogspot.com/" target=_blank&gt;^&lt;/A&gt;] and &lt;A href="http://www.ffconsultancy.com/dotnet/fsharp/index.html"&gt;F#.NET Tutorials&lt;/A&gt; [&lt;A href="http://www.ffconsultancy.com/dotnet/fsharp/index.html" target=_blank&gt;^&lt;/A&gt;] by Jon Harrop 
&lt;LI&gt;[10] &lt;A href="http://www.codeplex.com/fsharpsamples"&gt;F# Samples&lt;/A&gt; [&lt;A href="http://www.codeplex.com/fsharpsamples" target=_blank&gt;^&lt;/A&gt;] - Contains code that demonstrate various F# language features 
&lt;LI&gt;[11] &lt;A href="http://www.codeplex.com/fswebtools"&gt;F# WebTools&lt;/A&gt; [&lt;A href="http://www.codeplex.com/fswebtools" target=_blank&gt;^&lt;/A&gt;] - Project that allows writing client/server Ajax web applications entirely in F#&lt;/LI&gt;&lt;/UL&gt;&lt;img src="http://cs.hubfs.net/aggbug.aspx?PostID=3924" width="1" height="1"&gt;</description></item><item><title>Building LINQ Queries in F# and C#</title><link>http://cs.hubfs.net/blogs/tomasp/archive/2007/10/15/dynamic_queries_csharp_and_fsharp.aspx</link><pubDate>Tue, 16 Oct 2007 04:49:00 GMT</pubDate><guid isPermaLink="false">7372db05-f90c-40e3-82a2-789ed9f521c9:3754</guid><dc:creator>tomasp</dc:creator><slash:comments>0</slash:comments><comments>http://cs.hubfs.net/blogs/tomasp/comments/3754.aspx</comments><wfw:commentRss>http://cs.hubfs.net/blogs/tomasp/commentrss.aspx?PostID=3754</wfw:commentRss><description>&lt;P&gt;&lt;EM&gt;My last post at the hubFS was an article that I wrote earlier to my personal blog and I wrote one more article some time ago that is related to F# and I wasn't posted here. The article is a part of a short series that discusses how the same LINQ-related problem can be solved in F# and in C# 3.0 (and in fact, my implementation of the C# version was largely motivated by the F# solution). Because I think that the articles are closely related, I'll write just a short summary here and add links to both articles available in my other blog.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;The articles deal with a problem how can one build a LINQ (database) query at runtime. A typical example of a query that developers want to build at runtime is a query where the user can modify the filtering condition (&lt;CODE&gt;where&lt;/CODE&gt; clause) - for example the application may want to allow combining several conditions, like &lt;CODE&gt;c.Country == &amp;lt;entered value&amp;gt;&lt;/CODE&gt; with &lt;CODE&gt;c.Name.StartsWith(&amp;lt;entered value&amp;gt;)&lt;/CODE&gt;. In addition you may want to allow combining these single conditions using various operators (at least &lt;CODE&gt;or&lt;/CODE&gt; and &lt;CODE&gt;and&lt;/CODE&gt;). &lt;/P&gt;
&lt;P&gt;The article about the F# version also shortly introduces the F# comprehensions and quotations that are used when writing LINQ queries using F#. Then it gives a few examples of using LINQ for database queries and finally, it fully describes the example that is shortly introduced below. In the article about the C# version, I described several extensions that are very useful when writing the queries in the F#-style as well as a solution that I use to allow a mechanism similar to F# holes (that are shortly introduced below). Finally, the F# article also contains a version of the FLINQ sample that is compatible with Visual Studio 2008 Beta 2.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://tomasp.net/articles/dynamic-flinq.aspx"&gt;Building LINQ Queries at Runtime in F#&lt;/A&gt; [&lt;A href="http://tomasp.net/articles/dynamic-flinq.aspx" target=_blank&gt;^&lt;/A&gt;] - Complete article at TomasP.Net 
&lt;LI&gt;&lt;A href="http://tomasp.net/articles/dynamic-linq-queries.aspx"&gt;Building LINQ Queries at Runtime in C#&lt;/A&gt; [&lt;A href="http://tomasp.net/articles/dynamic-linq-queries.aspx" target=_blank&gt;^&lt;/A&gt;] - Complete article at TomasP.Net&lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;Building LINQ Queries at Runtime in F#&lt;/H2&gt;
&lt;P&gt;In F#, the queries are represented using &lt;EM&gt;quotations&lt;/EM&gt; which can be easily combined thanks to the support for &lt;EM&gt;holes&lt;/EM&gt; (written as underscore). The following example first defines two basic conditions (one of them always returns &lt;CODE&gt;true&lt;/CODE&gt; and the second returns &lt;CODE&gt;false&lt;/CODE&gt;). Later, we define two combinators for combining conditions using &lt;CODE&gt;||&lt;/CODE&gt; and &lt;CODE&gt;&amp;amp;&amp;amp;&lt;/CODE&gt; operators. Finally, we build a condition that is composed from several elementary conditions (stored in a dictionary called&amp;nbsp;&lt;CODE&gt;dict&lt;/CODE&gt;). To do this we can use the &lt;CODE&gt;Fold&lt;/CODE&gt; function which takes an initial condition and combines it with all the conditions in the dictionary:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=rem&gt;// Basic conditions that always return true/false&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;let&lt;/SPAN&gt; falseCond = « &lt;SPAN class=kwrd&gt;fun&lt;/SPAN&gt; (c:Customer) &lt;SPAN class=op&gt;-&amp;gt;&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;false&lt;/SPAN&gt; »
&lt;SPAN class=kwrd&gt;let&lt;/SPAN&gt; trueCond  = « &lt;SPAN class=kwrd&gt;fun&lt;/SPAN&gt; (c:Customer) &lt;SPAN class=op&gt;-&amp;gt;&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;true&lt;/SPAN&gt;  »

&lt;SPAN class=rem&gt;// Condition combinators&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;let&lt;/SPAN&gt; (&lt;SPAN class=op&gt;|&lt;/SPAN&gt;&lt;SPAN class=op&gt;|&lt;/SPAN&gt;*) f g = « &lt;SPAN class=kwrd&gt;fun&lt;/SPAN&gt; (c:Customer) &lt;SPAN class=op&gt;-&amp;gt;&lt;/SPAN&gt; (&lt;SPAN class=op&gt;_&lt;/SPAN&gt; c) &lt;SPAN class=op&gt;|&lt;/SPAN&gt;&lt;SPAN class=op&gt;|&lt;/SPAN&gt; (&lt;SPAN class=op&gt;_&lt;/SPAN&gt; c) » f g
&lt;SPAN class=kwrd&gt;let&lt;/SPAN&gt; (&amp;amp;&amp;amp;*) f g = « &lt;SPAN class=kwrd&gt;fun&lt;/SPAN&gt; (c:Customer) &lt;SPAN class=op&gt;-&amp;gt;&lt;/SPAN&gt; (&lt;SPAN class=op&gt;_&lt;/SPAN&gt; c) &amp;amp;&amp;amp; (&lt;SPAN class=op&gt;_&lt;/SPAN&gt; c) » f g

&lt;SPAN class=rem&gt;// Which combinator will we use?&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;let&lt;/SPAN&gt; (^^) = &lt;SPAN class=kwrd&gt;if&lt;/SPAN&gt; generateOr &lt;SPAN class=kwrd&gt;then&lt;/SPAN&gt; (&lt;SPAN class=op&gt;|&lt;/SPAN&gt;&lt;SPAN class=op&gt;|&lt;/SPAN&gt;*) &lt;SPAN class=kwrd&gt;else&lt;/SPAN&gt; (&amp;amp;&amp;amp;*)
  
&lt;SPAN class=rem&gt;// Build the expression by folding all items in a dictionary  &lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;let&lt;/SPAN&gt; expr = 
  dict.Fold(&lt;SPAN class=kwrd&gt;fun&lt;/SPAN&gt; key propSelector e &lt;SPAN class=op&gt;-&amp;gt;&lt;/SPAN&gt;
      Console.Write(&lt;SPAN class=str&gt;"Enter value for '{0}':\n&amp;gt; "&lt;/SPAN&gt;, key);
      &lt;SPAN class=kwrd&gt;let&lt;/SPAN&gt; enteredVal = Console.ReadLine();
          
      &lt;SPAN class=kwrd&gt;let&lt;/SPAN&gt; currentCond = 
        « &lt;SPAN class=kwrd&gt;fun&lt;/SPAN&gt; (c:Customer) &lt;SPAN class=op&gt;-&amp;gt;&lt;/SPAN&gt; ((&lt;SPAN class=op&gt;_&lt;/SPAN&gt; c):string).IndexOf(&lt;SPAN class=op&gt;_&lt;/SPAN&gt;:string) &amp;lt;&amp;gt; -1 » 
          propSelector « §enteredVal »
      (currentExpr ^^ e)) 
    (&lt;SPAN class=kwrd&gt;if&lt;/SPAN&gt; generateOr &lt;SPAN class=kwrd&gt;then&lt;/SPAN&gt; falseCond &lt;SPAN class=kwrd&gt;else&lt;/SPAN&gt; trueCond)
&lt;/PRE&gt;
&lt;H2&gt;Building LINQ Queries at Runtime in C#&lt;/H2&gt;
&lt;P&gt;In C# the representation of the lambda expression can be either a delegate (&lt;CODE&gt;Func&amp;lt;...&amp;gt;&lt;/CODE&gt;) or an expression tree (&lt;CODE&gt;Expression&amp;lt;Func&amp;lt;...&amp;gt;&amp;gt;&lt;/CODE&gt;) and the decision whether to generate a delegate or an expression tree depends on the expected type, which means that the code needs to define a type alias.&amp;nbsp;This is possible thanks to the &lt;CODE&gt;using&lt;/CODE&gt; statement (though this can be avoided by using helper functions, but in this example I'll instead use aliases). Primitive conditions are easy to define once we have a type alias &lt;CODE&gt;CustomerCondition&lt;/CODE&gt; (which stands for an expression tree of a lambda expression taking a &lt;CODE&gt;Customer&lt;/CODE&gt; as an argument and returning a &lt;CODE&gt;bool&lt;/CODE&gt;). When defining a combinators, we need a way for calling an expression tree in another expression tree and this is done by the method &lt;CODE&gt;Expand&lt;/CODE&gt; (which is not part of LINQ and is further discussed in the article). Finally, the code that generates the result uses similar technique as the previous F# version:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=rem&gt;// Basic conditions that always return true/false&lt;/SPAN&gt;
CustomerCondition trueCond  = (c) =&amp;gt; &lt;SPAN class=kwrd&gt;true&lt;/SPAN&gt;;
CustomerCondition falseCond = (c) =&amp;gt; &lt;SPAN class=kwrd&gt;false&lt;/SPAN&gt;;

&lt;SPAN class=rem&gt;// Condition combinators&lt;/SPAN&gt;
CustomerConditionCombinator combineOr = 
  (f, g) =&amp;gt; (c) =&amp;gt; f.Expand(c) || g.Expand(c);
CustomerConditionCombinator combineAnd = 
  (f, g) =&amp;gt; (c) =&amp;gt; f.Expand(c) &amp;amp;&amp;amp; g.Expand(c);

&lt;SPAN class=rem&gt;// Which combinator will we use?&lt;/SPAN&gt;
CustomerConditionCombinator combinator = 
  generateOr ? combineOr : combineAnd;

&lt;SPAN class=rem&gt;// Build the expression by folding all items in a dictionary  &lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;var&lt;/SPAN&gt; expr = dict.Fold((e, item) =&amp;gt; {
    Console.Write(&lt;SPAN class=str&gt;"Enter value for '{0}':\n&amp;gt; "&lt;/SPAN&gt;, item.Key);
    &lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt; enteredVal = Console.ReadLine();
    CustomerPropSelector propSelector = item.Value;    
    
    CustomerCondition currentCond = (c) =&amp;gt; 
      propSelector.Expand(c).IndexOf(enteredVal) != -1;
      
    &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; combinator(e, currentCond); 
  }, generateOr ? falseCond : trueCond);
&lt;/PRE&gt;&lt;img src="http://cs.hubfs.net/aggbug.aspx?PostID=3754" width="1" height="1"&gt;</description></item><item><title>F# Web Tools: &amp;quot;Ajax&amp;quot; applications made simple</title><link>http://cs.hubfs.net/blogs/tomasp/archive/2007/10/13/fsharp_webtools_intro.aspx</link><pubDate>Sun, 14 Oct 2007 05:23:00 GMT</pubDate><guid isPermaLink="false">7372db05-f90c-40e3-82a2-789ed9f521c9:3744</guid><dc:creator>tomasp</dc:creator><slash:comments>6</slash:comments><comments>http://cs.hubfs.net/blogs/tomasp/comments/3744.aspx</comments><wfw:commentRss>http://cs.hubfs.net/blogs/tomasp/commentrss.aspx?PostID=3744</wfw:commentRss><description>I started thinking about working on "Ajax" framework quite a long time ago - the key thing I really wanted from the beginning was using the same language for writing both client and server side code and the integration between these two sides, so you could write an event handler and specify if it should be executed on the client or on the server side. About a year ago I visited Cambridge (thanks to the MVP program) and I had a chance to talk with Don Syme [^]. Don showed me a few things in F# and suggested using F# for this project, so when I was later selected to do an internship at MSR, this was one of the projects that I wanted to work on.

The original reason for using F# was its support for meta-programming ([8], which makes it extremely easy to translate part of the page code-behind code to JavaScript. During my internship, the F# team was also working on a feature called computational expressions, which proved to be extremely useful for the F# Web Tools as well - I bet you'll hear a lot about this from Don soon, so I'll describe only the aspects that are important for this project. Aside from these two key features that F# has, I also quite enjoyed programming in F# itself - I already used it for a few things during the last year, but I could finally work on a large project in F# (and discuss the solution with the real experts!) and I don't believe I would be able to finish the project of similar complexity during less than three months in any other language (but this is a different topic, which deserves separate blog post). ...(&lt;a href="http://cs.hubfs.net/blogs/tomasp/archive/2007/10/13/fsharp_webtools_intro.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://cs.hubfs.net/aggbug.aspx?PostID=3744" width="1" height="1"&gt;</description></item><item><title>Keep your multi-core CPU busy with F#</title><link>http://cs.hubfs.net/blogs/tomasp/archive/2007/03/25/fsharp_parallel_ops.aspx</link><pubDate>Sun, 25 Mar 2007 07:36:00 GMT</pubDate><guid isPermaLink="false">7372db05-f90c-40e3-82a2-789ed9f521c9:2458</guid><dc:creator>tomasp</dc:creator><slash:comments>4</slash:comments><comments>http://cs.hubfs.net/blogs/tomasp/comments/2458.aspx</comments><wfw:commentRss>http://cs.hubfs.net/blogs/tomasp/commentrss.aspx?PostID=2458</wfw:commentRss><description>&lt;P&gt;The growth of computer CPU speed is slowly being replaced by the growth of number of CPUs (or cores) in the computer at least for the close future. This causes a revolution in the way software is written, because traditional and most widely used way of writing concurrent applications using threads is difficult and brings several serious issues. Some predictions say that within a few years, almost every computer will have about 16 cores, so there is a huge need for programming paradigms or idioms that help developers write concurrent software easily (see also &lt;A href="http://www.gotw.ca/publications/concurrency-ddj.htm"&gt;The Free Lunch Is Over&lt;/A&gt; [&lt;A href="http://www.gotw.ca/publications/concurrency-ddj.htm" target=_blank&gt;^&lt;/A&gt;] written by Herb Sutter).&lt;/P&gt;
&lt;P&gt;Functional programming languages (especially pure functional languages) are interesting from this point of view, because the program doesn't have side-effects which makes it very easy to parallelize it (programs in pure functional languages can't have any side-effects by design, in other functional languages like F# the side-effects can be eliminated by following functional programming style). &lt;/P&gt;
&lt;H2&gt;Finding primes example&lt;/H2&gt;
&lt;P&gt;Let's say I want to find all prime numbers between 1 million and 1.1 million (this is example of an operation that can be nicely divided between more processors). First we will need function to test whether &lt;CODE&gt;n&lt;/CODE&gt; is a prime:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=rem&gt;// Tests whether n is prime - expects n &amp;gt; 1&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;let&lt;/SPAN&gt; is_prime n =
  &lt;SPAN class=rem&gt;// calculate how large divisors should we test..&lt;/SPAN&gt;
  &lt;SPAN class=kwrd&gt;let&lt;/SPAN&gt; max = int_of_float (Math.Sqrt( float_of_int n ))
  &lt;SPAN class=rem&gt;// try to divide n by 2..max (stops when divisor is found)&lt;/SPAN&gt;
  not ({ 2 .. max } &lt;SPAN class=op&gt;|&amp;gt;&lt;/SPAN&gt; Seq.filter ( &lt;SPAN class=kwrd&gt;fun&lt;/SPAN&gt; d &lt;SPAN class=op&gt;-&amp;gt;&lt;/SPAN&gt; n%d = 0) &lt;SPAN class=op&gt;|&amp;gt;&lt;/SPAN&gt; Seq.nonempty)
&lt;/PRE&gt;
&lt;P&gt;To find all primes in the specified range we could use the following code:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=kwrd&gt;let&lt;/SPAN&gt; primes = [1000000 .. 1100000] &lt;SPAN class=op&gt;|&amp;gt;&lt;/SPAN&gt; List.filter is_prime
&lt;/PRE&gt;
&lt;P&gt;This code subsequently executes the &lt;CODE&gt;is_prime&lt;/CODE&gt; function for all numbers that we want to test, but with multiple CPU cores it would be nice if the function divided the numbers into several parts and executed every part on different thread, so application would take benefit from multiple cores available in the system. The &lt;CODE&gt;is_prime&lt;/CODE&gt; function doesn't have any side-effects so executing it several times in parallel can't change the result of operation (if the order of primes in the returned list doesn't change). &lt;/P&gt;
&lt;P&gt;I wrote a function that does exactly what I described in the previous paragraph. To execute the operation in parallel, you can use the following code (the only difference is that I replaced &lt;CODE&gt;List&lt;/CODE&gt; module with my &lt;CODE&gt;ParallelList&lt;/CODE&gt; module):&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=kwrd&gt;let&lt;/SPAN&gt; primes = [1000000 .. 1100000] &lt;SPAN class=op&gt;|&amp;gt;&lt;/SPAN&gt; ParallelList.filter is_prime
&lt;/PRE&gt;
&lt;P&gt;On my notebook (with Intel Core Duo processor) the first code (using &lt;CODE&gt;List.filter&lt;/CODE&gt;) takes about &lt;STRONG&gt;2,3sec&lt;/STRONG&gt; and using &lt;CODE&gt;ParallelList.filter&lt;/CODE&gt; the operation takes only &lt;STRONG&gt;1.3sec&lt;/STRONG&gt;. The program isn't 2 times faster, because there is some overhead for creating and synchronizing threads, but in this case the speed increase is significant.&lt;/P&gt;
&lt;P&gt;Aside from the &lt;CODE&gt;filter&lt;/CODE&gt; function, the &lt;CODE&gt;ParallelList&lt;/CODE&gt; module contains also the &lt;CODE&gt;map&lt;/CODE&gt; function (which does the same thing as the &lt;CODE&gt;List.map&lt;/CODE&gt;). There is also a function &lt;CODE&gt;set_thread_count&lt;/CODE&gt; that you can use to configure how many threads should be used when executing parallel operations (the default value is &lt;CODE&gt;2&lt;/CODE&gt;).&lt;/P&gt;
&lt;H2&gt;Performance and future work&lt;/H2&gt;
&lt;P&gt;The performance of these functions is the key issue. Currently the &lt;CODE&gt;ParallelList&lt;/CODE&gt; functions work can't be used for small number of repetitions of simple function, because the overhead is larger than the profit from parallel execution. If the operation takes less than 0.01ms and the number of repetitions is less than 1000 the &lt;CODE&gt;List&lt;/CODE&gt; functions are usually better, but for operations taking longer time the results of &lt;CODE&gt;ParallelList&lt;/CODE&gt; are better even with smaller number of repetitions. For operation taking about 0.1ms the &lt;CODE&gt;ParallelList&lt;/CODE&gt; gives better results for more than 200 repetitions and for operation taking more than 1ms the number of repetitions is not very important (&lt;CODE&gt;ParallelList&lt;/CODE&gt; is better even for 10 repetitions). These are inaccurate results that I got on my notebook, so if you're thinking of using the &lt;CODE&gt;ParallelList&lt;/CODE&gt;, be sure to do some tests in your scenario! You can see some tests that I did in the demo project in &lt;CODE&gt;stats.fs&lt;/CODE&gt; source file.&lt;/P&gt;
&lt;P&gt;As I said earlier, &lt;CODE&gt;ParallelList&lt;/CODE&gt; supports only &lt;CODE&gt;filter&lt;/CODE&gt; and &lt;CODE&gt;map&lt;/CODE&gt; functions, so implementing more functions would be useful. It would be also useful to provide some alternatives for functions that can't be executed in parallel (like &lt;CODE&gt;fold_left&lt;/CODE&gt;) that could be used in some situations. I'd also like to implement functions for working with other collection types like &lt;CODE&gt;Seq&lt;/CODE&gt; (&lt;CODE&gt;IEnumerable&lt;/CODE&gt;) and &lt;CODE&gt;array&lt;/CODE&gt; in the future.&lt;/P&gt;
&lt;P&gt;I'm interested in your ideas and suggestions, so if you find something that could be improved in the code, or if you have any other idea, let me know!&lt;/P&gt;
&lt;H2&gt;Downloads&lt;/H2&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://tomasp.net/articles/fsparallelops/demo.zip"&gt;Download demo application with source code&lt;/A&gt; (14 kB)&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://tomasp.net/articles/fsparallelops/parallel.zip"&gt;Download only ParallelList source&lt;/A&gt; (2 kB)&lt;/LI&gt;&lt;/UL&gt;&lt;img src="http://cs.hubfs.net/aggbug.aspx?PostID=2458" width="1" height="1"&gt;</description></item><item><title>F# presentation at Czech .NET Group meeting</title><link>http://cs.hubfs.net/blogs/tomasp/archive/2006/11/14/dng_fsharp_presentation.aspx</link><pubDate>Tue, 14 Nov 2006 08:54:00 GMT</pubDate><guid isPermaLink="false">7372db05-f90c-40e3-82a2-789ed9f521c9:839</guid><dc:creator>tomasp</dc:creator><slash:comments>0</slash:comments><comments>http://cs.hubfs.net/blogs/tomasp/comments/839.aspx</comments><wfw:commentRss>http://cs.hubfs.net/blogs/tomasp/commentrss.aspx?PostID=839</wfw:commentRss><description>&lt;p&gt;On 2nd of November I did a presentation on F# and functional programming at the Czech .NET User Group meeting.
  Because I spent quite a lot of time with puting the presentation together I wanted to make it available to wider 
  audience, so I translated the slides and examples to English (anyway, translating the content took me only a few minutes :-)).
  In case that some of the readers prefer Czech version, I attached the original documents too.&lt;/p&gt;
&lt;p&gt;In the presentation I tried to introduce some basic concepts of functional programming (immutable values,
  lazy evaluation) to the audience with no experience with functional programming, as well as present some
  of the most interesting features of F# (like strict type system based on type inference, .NET interoperability and 
  metaprogramming). The whole contents of the presentation is following:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Functional programming in F#&lt;/strong&gt; - Introduction to the F# type system&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Some useful functional idioms&lt;/strong&gt; - How to do Foldl/Map/Filter functions and Lazy evaluation in C#&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Interactive scripting&lt;/strong&gt; - What is important for scripting, mathematical simulation&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Interoperability between F# and other .NET languages&lt;/strong&gt; - How to use .NET libraries from F# and F# libraries from ohter .NET languages&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;F# as a language for ASP.NET&lt;/strong&gt; - How to use F# as a language for ASP.NET development&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Meta-programming in F#&lt;/strong&gt; - Meta-programming features in F# and the FLINQ project&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Downloads&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;Presentation - &lt;a href="http://www.tomasp.net/articles/fsharp-presentation/fsharp-intro.ppt"&gt;Czech&lt;/a&gt; (889 kB), &lt;a href="http://www.tomasp.net/articles/fsharp-presentation/fsharp-intro-en.ppt"&gt;English&lt;/a&gt; (894 kB)&lt;/li&gt;
  &lt;li&gt;Demos - &lt;a href="http://www.tomasp.net/articles/fsharp-presentation/demos.zip"&gt;Czech&lt;/a&gt; (500 kB), &lt;a href="http://www.tomasp.net/articles/fsharp-presentation/demos-en.zip"&gt;English&lt;/a&gt; (501 kB)&lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://cs.hubfs.net/aggbug.aspx?PostID=839" width="1" height="1"&gt;</description></item><item><title>F# metaprogramming and classes</title><link>http://cs.hubfs.net/blogs/tomasp/archive/2006/10/14/metaprogramming_and_classes.aspx</link><pubDate>Sat, 14 Oct 2006 08:48:00 GMT</pubDate><guid isPermaLink="false">7372db05-f90c-40e3-82a2-789ed9f521c9:782</guid><dc:creator>tomasp</dc:creator><slash:comments>1</slash:comments><comments>http://cs.hubfs.net/blogs/tomasp/comments/782.aspx</comments><wfw:commentRss>http://cs.hubfs.net/blogs/tomasp/commentrss.aspx?PostID=782</wfw:commentRss><description>

&lt;h2&gt;Introduction&lt;/h2&gt;
&lt;p&gt;F# quotations allows you to easily write programs that manipulate with data representation
  of program source code. If you're not familiar with quotations I recommend reading my 
  previous article [&lt;a&gt;1&lt;/a&gt;] that contains short introduction
  to this topic first. Quotations can be used for example for translating subset of the F# language to 
  another code representation or another language.&lt;/p&gt;
  
&lt;p&gt;To get the quotation data of the expression you can either use &lt;code&gt;&amp;lt;@ .. @&amp;gt;&lt;/code&gt; 
  operator or &lt;code&gt;resolveTopDef&lt;/code&gt; function. In the first case the code written
  between the "&lt;code&gt;&amp;lt;@&lt;/code&gt;" and "&lt;code&gt;@&amp;gt;&lt;/code&gt;" is converted to data during the compilation.
  The &lt;code&gt;resolveTopDef&lt;/code&gt; function allows you to get quotation data of top-level 
  definition (function) from compiled library at runtime (you have to use &lt;code&gt;--quotation-data&lt;/code&gt; 
  command line switch while compiling the library). I mentioned that quotations can be used to represent 
  only subset of the F# language. Currently, one of the quotation limitations is that it's not possible to 
  enclose the whole class in the quotation operators. It is also not possible to get the representation 
  of the whole class at runtime nor the representation of class members (for example methods). &lt;/p&gt;

&lt;h2&gt;Class quotations&lt;/h2&gt;

&lt;p&gt;In one my project I wanted to be able to translate the class written in F# to another language,
  so I wanted to make this possible. This option will be probably implemented in the future versions
  of F#, so it will be possible to do this using more elegant method, but if you want to experiment
  with quotations and use classes, you can use my solution to implement the prototype.&lt;/p&gt;

&lt;p&gt;In the prototype I implemented, you have to write the class and one module that contains 
  the actual code for the methods. This makes it possible to get the structure of the class using
  standard .NET reflection classes and to extract quotation data for the class members using
  F# &lt;code&gt;resolveTopDef&lt;/code&gt; function. Of course, you can't create instance of module, but for metaprogramming
  we only need to be able to get the quoted code and unless you want to use the class from code, you can leave
  the implementation of the class methods empty.&lt;/p&gt;

&lt;p&gt;First I had to design the data structures for representing the class. If I were adding this feature to the F# 
  metaprogramming, I would probably extended the &lt;code&gt;expr&lt;/code&gt; data type to make this possible in
  consistent way. However I didn't want to modify the F# source code, so I designed the following types
  that are similar to the &lt;code&gt;expr&lt;/code&gt; type (I reversed the order of type declarations, so it 
  is easier to understand):&lt;/p&gt;
&lt;pre&gt;
&lt;span class="c"&gt;/// Structure that contains information about class -
/// consists of name, name of base class and members&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; classInfo = {
  name:string;
  baseName:string option;
  members:classMember list;
};;

&lt;span class="c"&gt;/// Member of the class 
/// For working with this type use cmfCtor, cmfMethod, cmfField, cmfProp..&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; classMember;;

&lt;span class="c"&gt;/// Type for working with class members
/// Query function tests whether member is of specified type
/// Make function creates member&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; 'a classMemberFamily &lt;span class="k"&gt;with&lt;/span&gt;
  &lt;span class="k"&gt;member&lt;/span&gt; Query : classMember -&gt; 'a option
  &lt;span class="k"&gt;member&lt;/span&gt; Make : 'a -&gt; classMember
&lt;span class="k"&gt;end&lt;/span&gt;;;

&lt;span class="c"&gt;/// Constructor declaration 
/// - parameter should be lambda expression&lt;/span&gt;
&lt;span class="k"&gt;val&lt;/span&gt; cmfCtor : (expr) classMemberFamily;;
	
&lt;span class="c"&gt;/// Method delcaration
/// - method name, expression (should be lambda) and return type
///   (types of parameters are stored in lambda expression)&lt;/span&gt;
&lt;span class="k"&gt;val&lt;/span&gt; cmfMethod : (string*expr*Type option) classMemberFamily;;
	
&lt;span class="c"&gt;/// Field declaration
/// - field name, type and init expression&lt;/span&gt;
&lt;span class="k"&gt;val&lt;/span&gt; cmfField : (string*Type*expr option) classMemberFamily;;
	
&lt;span class="c"&gt;/// Property declaration
/// - property name, getter and setter (both should be lambda expr) and type&lt;/span&gt;
&lt;span class="k"&gt;val&lt;/span&gt; cmfProp : (string*expr*expr*Type) classMemberFamily;;
&lt;/pre&gt;

&lt;p&gt;This representation is still very limited - for example it isn't possible to represent
  polymorphic methods. In the previous code, the &lt;code&gt;classMember&lt;/code&gt; can be used to 
  represent any member (similarly to the F# &lt;code&gt;expr&lt;/code&gt; that can represent any expression).
  The &lt;code&gt;cmf[Something]&lt;/code&gt; values are equivalent to the 
  &lt;code&gt;ef[Something]&lt;/code&gt; from F# quotation library and allows you to work with
  &lt;code&gt;classMember&lt;/code&gt; type.&lt;/p&gt;
  
&lt;p&gt;The following example shows how to write simple class with module that can be used for extracting 
  the quotation data:&lt;/p&gt;
&lt;pre&gt;
#light

&lt;span class="c"&gt;// Module with implementation of methods&lt;/span&gt;
&lt;span class="k"&gt;module&lt;/span&gt; Person_Meta = &lt;span class="k"&gt;begin&lt;/span&gt;
  &lt;span class="c"&gt;// Simulates field of the class&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; name = &lt;span class="k"&gt;ref&lt;/span&gt; ""
  
  &lt;span class="c"&gt;// Represents constructor&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; ctor (n) =
    name := n
    
  &lt;span class="c"&gt;// Represents method&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; Say (pre:string) =
    let s = pre^", my name is "^(!name)^"." in
    print_string s
  
  &lt;span class="c"&gt;// Represents property  &lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; get_Name () =
    (!name)
  &lt;span class="k"&gt;let&lt;/span&gt; set_Name (value) =
    name:=value
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c"&gt;// The real class
// Members are just a placeholders and the quotations
// are extracted from the previous module&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; Person = &lt;span class="k"&gt;class &lt;/span&gt;
  &lt;span class="k"&gt;val mutable&lt;/span&gt; name : string;
  &lt;span class="k"&gt;new&lt;/span&gt;((n:string)) = { name = ""; }
  &lt;span class="k"&gt;member&lt;/span&gt; this.Say(pre:string) = ()
  &lt;span class="k"&gt;member&lt;/span&gt; this.Name with get() = "" &lt;span class="k"&gt;and&lt;/span&gt; set((v:string)) = ()
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;  

&lt;p&gt;In the last code sample, I'll show how the functions I wrote can be used for working 
  with the previous piece of code. To get the representation that I mentioned earlier of
  the class &lt;code&gt;Person&lt;/code&gt;, you can use function &lt;code&gt;getClassFromType&lt;/code&gt;. This function
  extracts structure of the class (from the class itself) and quoted code from the module
  with the &lt;code&gt;_Meta&lt;/code&gt; suffix. The following example is quite simple. It prints
  basic class information (name and base name) and then iterates through all the members and
  prints all available information for every member. For printing the &lt;code&gt;expr&lt;/code&gt; type
  (which represents the quoted code) I used &lt;code&gt;printf&lt;/code&gt; function with the &lt;code&gt;output_any&lt;/code&gt; formatter.&lt;/p&gt;

&lt;pre&gt;
#light
&lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="c"&gt;// Get the class info for class 'Person'&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; clsInfo = getClassFromType ((typeof() : Person typ).result)
  
  &lt;span class="c"&gt;// Print name and optional base name&lt;/span&gt;
  Console.WriteLine(&lt;span class="s"&gt;"Class '{0}':"&lt;/span&gt;, clsInfo.name)
  &lt;span class="k"&gt;if&lt;/span&gt; (clsInfo.baseName &amp;lt;&amp;gt; None) &lt;span class="k"&gt;then&lt;/span&gt;
    Console.WriteLine(&lt;span class="s"&gt;" base: {0}"&lt;/span&gt;, clsInfo.baseName)
  
  &lt;span class="c"&gt;// Iterate through class members&lt;/span&gt;
  clsInfo.members |&gt; List.iter ( &lt;span class="k"&gt;fun&lt;/span&gt; m -&gt;
      &lt;span class="k"&gt;match&lt;/span&gt; cmfMethod.Query m &lt;span class="k"&gt;with&lt;/span&gt;
        | Some (name, expr, ret) -&gt;
            Method &lt;span class="s"&gt;"printf ( %s : %a ) = "&lt;/span&gt; name output_any ret
        | _ -&gt;
      &lt;span class="k"&gt;match&lt;/span&gt; cmfField.Query m &lt;span class="k"&gt;with&lt;/span&gt;
        | Some (name, typ, init) -&gt;
            printf &lt;span class="s"&gt;"Field ( %s : %a) "&lt;/span&gt; name output_any typ
        | _ -&gt;
      &lt;span class="k"&gt;match&lt;/span&gt; cmfProp.Query m &lt;span class="k"&gt;with&lt;/span&gt;
        | Some (name, getter, setter, typ) -&gt;
            printf &lt;span class="s"&gt;"Property ( %s : %a )\n"&lt;/span&gt; name output_any typ
            printf &lt;span class="s"&gt;"get = %a\n"&lt;/span&gt; output_any getter
            printf &lt;span class="s"&gt;"set = %a\n\n"&lt;/span&gt; output_any setter
        | _ -&gt;
      &lt;span class="k"&gt;match&lt;/span&gt; cmfCtor.Query m &lt;span class="k"&gt;with&lt;/span&gt;
        | Some (expr) -&gt;
            printf &lt;span class="s"&gt;"Ctor = %a\n\n"&lt;/span&gt; output_any expr
        | _ -&gt;
            failwith &lt;span class="s"&gt;"Error!"&lt;/span&gt;
    ) 
&lt;/pre&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;The aim of this article isn't to present fully working solution, but to suggest a few enhancements
  to the F# metaprogramming that I think would be useful. You can use the source code attached to this 
  article to find (and experiment with) some interesting use cases of metaprogramming that require working 
  with classes, for example translating classes written in F# to another language. The biggest limitation
  of the solution I presented is, that you have to write every implementation twice - it occurs in the
  body of the class and in the module used for metaprogramming too (however the code looks very similar).
  If you want to translate F# code, you don't need to implement methods/properties of the real class, but
  if the program creates instances of the class at runtime (and uses the quoted code to perform some analysis etc.),
  you'll need to write both implementations.&lt;/p&gt;

&lt;h2&gt;Downloads&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://www.tomasp.net/articles/fsclassmeta/source.zip"&gt;Download the source code and examples&lt;/a&gt; (6.77kB)&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://www.tomasp.net/articles/fsclassmeta/article.pdf"&gt;Download the article&lt;/a&gt; (pdf, 204kB)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Links and references&lt;a name="fsclassmeta-links"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;[1] &lt;a href="/blogs/tomasp/archive/2006/07/07/413.aspx"&gt;F# - Simple quotations transformation&lt;/a&gt;
    [&lt;a href="/blogs/tomasp/archive/2006/07/07/413.aspx" target="_blank"&gt;^&lt;/a&gt;] - My F# Notes&lt;/li&gt;  
&lt;/ul&gt;&lt;img src="http://cs.hubfs.net/aggbug.aspx?PostID=782" width="1" height="1"&gt;</description></item><item><title>F# quotations visualizer - reloaded!</title><link>http://cs.hubfs.net/blogs/tomasp/archive/2006/10/01/724.aspx</link><pubDate>Mon, 02 Oct 2006 05:02:00 GMT</pubDate><guid isPermaLink="false">7372db05-f90c-40e3-82a2-789ed9f521c9:724</guid><dc:creator>tomasp</dc:creator><slash:comments>4</slash:comments><comments>http://cs.hubfs.net/blogs/tomasp/comments/724.aspx</comments><wfw:commentRss>http://cs.hubfs.net/blogs/tomasp/commentrss.aspx?PostID=724</wfw:commentRss><description>&lt;DIV style="FLOAT: right; MARGIN-LEFT: 10px"&gt;&lt;A href="http://www.tomasp.net/articles/quotvis-reloaded/screen.png" target=_blank&gt;&lt;IMG style="MARGIN: 10px; BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BORDER-BOTTOM-STYLE: none" alt="Quotation Visualizer" src="http://tomasp.net/articles/quotvis-reloaded/screen-sm.png"&gt;&lt;/A&gt; &lt;/DIV&gt;
&lt;P&gt;Some time ago, I wrote an article about useful utility called &lt;A href="http://www.tomasp.net/blog/quotvis.aspx"&gt;F# quotations visualizer&lt;/A&gt;. This utility can be used to show visual representation of F# quotations, which can represent (subset of) source code written in F#. There are two ways that you can use to get F# quotations - first is using operators &lt;CODE&gt;&amp;lt;@@ ... @@&amp;gt;&lt;/CODE&gt; (this returns quotation of the code written inside the operator), second method is to get quotation of top level definition from compiled F# assembly (you have to explicitly enable this using command line switch &lt;CODE&gt;--quotation-data&lt;/CODE&gt; while compiling assembly). &lt;/P&gt;
&lt;P&gt;Because I added several new features to the original Quotations visualizer, I decided to publish the latest version - here is the list of main improvements:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Application is rewritten using active patterns (new F# language feature) 
&lt;LI&gt;It is possible to extract quotations from compiled F# assembly (if it contains quotation data) 
&lt;LI&gt;Added support for several missing language constructs&lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;Active patterns&lt;/H2&gt;
&lt;P&gt;Active patterns is a new (experimental) feature in the F# language. You can find some information about this feature in Don Syme's article [&lt;A&gt;1&lt;/A&gt;]. In simple words, active patterns allows you to write "switch" consisting of functions (patterns) that return &lt;CODE&gt;'a option&lt;/CODE&gt; type. When the code is executed, it finds first pattern whose query returned a value (&lt;CODE&gt;Some(...)&lt;/CODE&gt;) and the body of selected pattern is executed.&lt;/P&gt;
&lt;P&gt;This feature makes it very easy to work with F# quotations because all &lt;CODE&gt;ef[Something]&lt;/CODE&gt; values that are used for querying quotations have the required signature (the &lt;CODE&gt;Query&lt;/CODE&gt; function that returns &lt;CODE&gt;'a option&lt;/CODE&gt; type), so they can be used with the active patterns feature.&lt;/P&gt;
&lt;P&gt;In the quotations visualizer, we need to match the expression (type &lt;CODE&gt;expr&lt;/CODE&gt;) with all possible expression families (&lt;CODE&gt;ef[Something]&lt;/CODE&gt;) and choose the first one that matches. To see how active patterns work, you can look at the following part of the function that does this. First, without active patterns:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=k&gt;match&lt;/SPAN&gt; efForLoop.Query(expr) &lt;SPAN class=k&gt;with&lt;/SPAN&gt;
  | Some(nfrom,nto,body) -&amp;gt;
      &lt;SPAN class=c&gt;// Statement: for i=start to end do body; done;
      // .. create tree node ..&lt;/SPAN&gt;
  | _ -&amp;gt;
&lt;SPAN class=k&gt;match&lt;/SPAN&gt; efWhileLoop.Query(cond,body) &lt;SPAN class=k&gt;with&lt;/SPAN&gt;
      &lt;SPAN class=c&gt;// Statement: while condition do body; done;
      // .. create tree node ..&lt;/SPAN&gt;
  | _ -&amp;gt;
&lt;SPAN class=k&gt;match&lt;/SPAN&gt; efCond.Query(t,(cond,trbody,flbody)) &lt;SPAN class=k&gt;with&lt;/SPAN&gt;
      &lt;SPAN class=c&gt;// if (cond) then trbody; else flbody;
      // .. create tree node ..&lt;/SPAN&gt;
  | _ -&amp;gt;
      &lt;SPAN class=c&gt;// unknown expression&lt;/SPAN&gt;
&lt;/PRE&gt;
&lt;P&gt;And with active patterns the source looks like this:&lt;/P&gt;&lt;PRE&gt;#light
&lt;SPAN class=k&gt;let&lt;/SPAN&gt; EFForLoop = efForLoop
&lt;SPAN class=k&gt;let&lt;/SPAN&gt; EFWhileLoop = efWhileLoop
&lt;SPAN class=k&gt;let&lt;/SPAN&gt; EFCond = efCond

&lt;SPAN class=k&gt;match&lt;/SPAN&gt; expr &lt;SPAN class=k&gt;with&lt;/SPAN&gt;
  | EFForLoop(nfrom,nto,body) -&amp;gt;
      &lt;SPAN class=c&gt;// Statement: for i=start to end do body; done;
      // .. create tree node ..&lt;/SPAN&gt;
  | EFWhileLoop(cond,body) -&amp;gt;
      &lt;SPAN class=c&gt;// Statement: while condition do body; done;
      // .. create tree node ..&lt;/SPAN&gt;
  | EFCond(t,(cond,trbody,flbody)) -&amp;gt;
      &lt;SPAN class=c&gt;// if (cond) then trbody; else flbody;
      // .. create tree node ..&lt;/SPAN&gt;
  | _ -&amp;gt;
      &lt;SPAN class=c&gt;// unknown expression&lt;/SPAN&gt;
&lt;/PRE&gt;
&lt;P&gt;This is very simple example and there are many situations where active patterns are even more helpful. You may have also noticed that the code doesn't contain any semicolons. This is the result of another new feature called lightweight syntax (it is turned on by the #light directive) - if you turn it on the whitespace becomes significant and compiler can understand structure of the code using whitespace instead of semicolons, parentheses and &lt;CODE&gt;begin&lt;/CODE&gt;/&lt;CODE&gt;end&lt;/CODE&gt; keywords. This feature is described in the F# manual [&lt;A&gt;2&lt;/A&gt;].&lt;/P&gt;
&lt;H2&gt;Extracting quotations from assembly&lt;/H2&gt;
&lt;P&gt;When you specify &lt;CODE&gt;--enable-quotation-data&lt;/CODE&gt; switch to the F# compiler, it stores quotation of every top level definition (functions in modules) in the assembly. This quotation can be later retrieved using &lt;CODE&gt;resolveTopDef&lt;/CODE&gt; function, however to use the function for loading top-level definitions from another assembly, you first have to load quotation data from the assembly. The following snippet shows how to do this.&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=k&gt;let&lt;/SPAN&gt; asm = Assembly.LoadFile(name) &lt;SPAN class=k&gt;in&lt;/SPAN&gt;
  asm.GetManifestResourceNames() |&amp;gt; Array.to_list 
    |&amp;gt; List.filter (&lt;SPAN class=k&gt;fun&lt;/SPAN&gt; rn -&amp;gt; 
        rn.StartsWith(pickledDefinitionsResourceNameBase)) 
    |&amp;gt; List.iter (&lt;SPAN class=k&gt;fun&lt;/SPAN&gt; rn -&amp;gt; 
        explicitlyRegisterTopDefs asm rn (readToEnd (asm.GetManifestResourceStream(rn))))
&lt;/PRE&gt;
&lt;P&gt;This code first loads the assembly (using &lt;CODE&gt;LoadFile&lt;/CODE&gt; method which accepts assembly file path). Than it gets all managed resources of the assembly and selects only those resources, whose name starts with &lt;CODE&gt;pickledDefinitionsResourceNameBase&lt;/CODE&gt; (this is a constant declared in &lt;CODE&gt;Microsoft.FSharp.Quotations.Raw&lt;/CODE&gt; module). Now we have all resources containing F# quotation data, and we can use &lt;CODE&gt;explicitlyRegisterTopDefs&lt;/CODE&gt; function to load quoted top level definitions. The &lt;CODE&gt;explicitlyRegisterTopDefs&lt;/CODE&gt; method takes three parameters - assembly, name of the resource and resource data (byte array). When the top level definitions are registered using this method, it is possible to load quotations of functions declared in loaded assembly - and this is exactly what happens when you click on the "Open F# assembly" link in the application. If you are interested in the complete code, look at the attached source code of quotations visualizer application.&lt;/P&gt;
&lt;H2&gt;Links&lt;A name=qv-reload-links&gt;&lt;/A&gt;&lt;/H2&gt;
&lt;UL&gt;
&lt;LI&gt;[1] &lt;A href="http://blogs.msdn.com/dsyme/archive/2006/08/16/ActivePatterns.aspx"&gt;An upcoming experimental feature: Active Patterns in F#&lt;/A&gt; [&lt;A href="http://blogs.msdn.com/dsyme/archive/2006/08/16/ActivePatterns.aspx" target=_blank&gt;^&lt;/A&gt;] - Don Syme's WebLog on F# and Other Research Projects 
&lt;LI&gt;[2] &lt;A href="http://research.microsoft.com/fsharp/manual/lexyacc.aspx#LightSyntax"&gt;Optional lightweight syntax&lt;/A&gt; [&lt;A href="http://research.microsoft.com/fsharp/manual/lexyacc.aspx#LightSyntax" target=_blank&gt;^&lt;/A&gt;] - F# manual&lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;Downloads&lt;/H2&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://tomasp.net/articles/quotvis-reloaded/quotvis.zip"&gt;Download the application&lt;/A&gt; (50.3kB) 
&lt;LI&gt;&lt;A href="http://tomasp.net/articles/quotvis-reloaded/quotvis-src.zip"&gt;Download the application - source code&lt;/A&gt; (39.3kB)&lt;/LI&gt;&lt;/UL&gt;&lt;img src="http://cs.hubfs.net/aggbug.aspx?PostID=724" width="1" height="1"&gt;</description></item><item><title>F# CodeDOM Provider on the CodePlex</title><link>http://cs.hubfs.net/blogs/tomasp/archive/2006/08/22/549.aspx</link><pubDate>Wed, 23 Aug 2006 06:43:00 GMT</pubDate><guid isPermaLink="false">7372db05-f90c-40e3-82a2-789ed9f521c9:549</guid><dc:creator>tomasp</dc:creator><slash:comments>1</slash:comments><comments>http://cs.hubfs.net/blogs/tomasp/comments/549.aspx</comments><wfw:commentRss>http://cs.hubfs.net/blogs/tomasp/commentrss.aspx?PostID=549</wfw:commentRss><description>&lt;P&gt;You probably already saw my post regarding CodeDOM generator for the F# language and how to use it with ASP.NET. To make it more accessible for everyone, I created project at the new Microsoft community site called &lt;A href="http://www.codeplex.com/"&gt;CodePlex&lt;/A&gt; [&lt;A href="http://www.codeplex.com/" target=_blank&gt;^&lt;/A&gt;].&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;To learn more about the project, visit &lt;A href="http://www.codeplex.com/Wiki/View.aspx?ProjectName=fscodedom"&gt;Project Home Page&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;If you want to send some comments, go to &lt;A href="http://www.codeplex.com/Project/ListForums.aspx?ProjectName=fscodedom"&gt;Discussion Board&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;You can also download latest source code - &lt;A href="http://www.codeplex.com/SourceControl/ListDownloadableCommits.aspx?ProjectName=fscodedom"&gt;Latest Check-Ins&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;And finally, the latest releases can be found at the&amp;nbsp;&lt;A href="http://www.codeplex.com/Release/ProjectReleases.aspx?ProjectName=fscodedom"&gt;Releases Page&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;BTW: CodePlex looks like a really good site. It is based on Visual Studio Team System (which means that developers of the project can do most of the work directly from Visual Studio). It provides management of "Work Items" (TODO list), source control and many other useful things! For example if you have any feature requests or bug requests, send them to the discussion and I can easilly create work item from the message in the discussions.&lt;/P&gt;
&lt;P&gt;If you are interested in this project and you want to help with developing of some parts, or if you are working on a project that is related to CodeDOM and F#, please let me know. Any help or feedback is kindly welcome!&lt;/P&gt;&lt;img src="http://cs.hubfs.net/aggbug.aspx?PostID=549" width="1" height="1"&gt;</description></item><item><title>ASP.NET web applications in F#</title><link>http://cs.hubfs.net/blogs/tomasp/archive/2006/08/13/475.aspx</link><pubDate>Sun, 13 Aug 2006 07:03:00 GMT</pubDate><guid isPermaLink="false">7372db05-f90c-40e3-82a2-789ed9f521c9:475</guid><dc:creator>tomasp</dc:creator><slash:comments>6</slash:comments><comments>http://cs.hubfs.net/blogs/tomasp/comments/475.aspx</comments><wfw:commentRss>http://cs.hubfs.net/blogs/tomasp/commentrss.aspx?PostID=475</wfw:commentRss><description>&lt;H2&gt;CodeDOM and providers&lt;/H2&gt;
&lt;P&gt;CodeDOM (Code Document Object Model) is set of objects (located in &lt;CODE&gt;System.CodeDom&lt;/CODE&gt; namespace) that can be used for representing logical structure of .NET source code. For example you can use &lt;CODE&gt;CodeTypeDeclaration&lt;/CODE&gt; class to represent class declaration or &lt;CODE&gt;CodeAssignStatement&lt;/CODE&gt; to represent assignment in the body of method. CodeDom is language independent and CodeDOM structure can be translated to source code in specified language using code generator (class that implements &lt;CODE&gt;ICodeGenerator&lt;/CODE&gt; namespace). The CodeDOM structure can be also compiled to assembly using code compiler (implementation of &lt;CODE&gt;ICodeCompiler&lt;/CODE&gt; interface). Microsoft provides several code providers with .NET Framework (for C#, VB.Net, JScript and C++), but you can add your own by by implementing previously mentioned interfaces.&lt;/P&gt;
&lt;P&gt;In the .NET Framework, CodeDOM is used for automatic code generation. For example when you add web reference to your project, the wrapper class that calls web service is generated using CodeDom. Some more examples are typed dataset (generated from XSD files) and generator for data layer in the upcoming LINQ project, but the most interesting use for CodeDOM for me is in ASP.NET 2.0. &lt;/P&gt;
&lt;P&gt;Typical ASP.NET web page consists of several &lt;CODE&gt;aspx&lt;/CODE&gt;/&lt;CODE&gt;ascx&lt;/CODE&gt; files and associated code behind files written in one of the .NET languages. When building web page, ASP.NET uses CodeDOM for building &lt;CODE&gt;aspx&lt;/CODE&gt;/&lt;CODE&gt;ascx&lt;/CODE&gt; files. It parses these files and generates according CodeDOM structure. For example &lt;CODE&gt;&amp;lt;asp:Button id="btn" runat="server" /&amp;gt;&lt;/CODE&gt; declaration is translated to declaration of &lt;CODE&gt;btn&lt;/CODE&gt; member and code that assigns new instance of &lt;CODE&gt;Button&lt;/CODE&gt; object to this member. This generated structure is compiled to .NET assemblies using code compiler and the web page is ready to use. If you use any in-line code in ASP.NET page, it is included in the CodeDOM structure as literal, which means that if the generated CodeDOM is compiled using C# code compiler, it can contain literals only in C# language (because you can't mix languages in one source file).&lt;/P&gt;
&lt;H2&gt;ASP.NET and F#&lt;/H2&gt;
&lt;P&gt;In ASP.NET 1.1 you could compile code behind files using different compiler than the rest of the page (source generated from &lt;CODE&gt;aspx&lt;/CODE&gt;/&lt;CODE&gt;ascx&lt;/CODE&gt;). This method is used by ASP.NET in F# demo by Robert Pickering [&lt;A&gt;1&lt;/A&gt;]. The more complex approach that is available in ASP.NET 2 is to write &lt;CODE&gt;CodeDomProvider&lt;/CODE&gt; implementation for F#. This makes it possible to write F# in-line code in web pages and this method allows you to fully benefit of ASP.NET compilation model (for example you can use Publish Web Site command in Visual Studio). &lt;/P&gt;
&lt;H3&gt;FSharpCodeProvider &amp;amp; AspNetFsharpCodeProvider&lt;/H3&gt;
&lt;P&gt;I wanted to implement CodeDomProvider for the F# language, for the reasons mentioned above. Currently, I'm presenting version of provider that is able to compile ASP.NET pages quite well, however I implemented only features required by ASP.NET, so the provider is not complete. There are also some problems that has to be handled specially (for example, F# doesn't support partial classes), so apart from standard provider (&lt;CODE&gt;FsharpCodeProvider&lt;/CODE&gt;), I also implemented &lt;CODE&gt;AspNetFsharpCodeProvider&lt;/CODE&gt; that contains several 'hacks' for ASP.NET.&lt;/P&gt;
&lt;H3&gt;How to use it?&lt;/H3&gt;
&lt;P&gt;&lt;STRONG&gt;Important:&lt;/STRONG&gt; For correct functionality of F# CodeDOM compiler, you have to modify your system variable PATH to include 'bin' directory of your F# installation folder. This is because, code compiler uses &lt;CODE&gt;fsc.exe&lt;/CODE&gt; for compilation and there is no simple way for finding where the F# is installed. &lt;/P&gt;
&lt;P&gt;The most simple way to start creating ASP.NET web sites in F# is to use VS.NET project template that you can find in the downloads section. Using this template, you can create web page that is already configured to use F# language. After installation, select &lt;I&gt;File&lt;/I&gt; - &lt;I&gt;New&lt;/I&gt; - &lt;I&gt;WebSite..&lt;/I&gt; and in the dialog select language 'Visual C#' (it is not possible to add another language AFAIK) and than select 'F# WebSite' template.&lt;/P&gt;
&lt;P&gt;If you want to configure ASP.NET web site to use F# code provider manually, you have to do the following steps. First modify the &lt;CODE&gt;web.config&lt;/CODE&gt; file to include the following elements in the &lt;CODE&gt;compilation&lt;/CODE&gt; section:&lt;/P&gt;&lt;PRE&gt;&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;configuration&amp;gt;
  &amp;lt;system.web&amp;gt;
    &amp;lt;compilation debug="true"&amp;gt;
      &amp;lt;compilers&amp;gt;
        &amp;lt;compiler language="F#;f#;fs;fsharp" extension=".fs" 
          type="EeekSoft.FSharp.CodeDom.AspNetFSharpCodeProvider, fscodeprovider"/&amp;gt;
      &amp;lt;/compilers&amp;gt;
    &amp;lt;/compilation&amp;gt;
  &amp;lt;/system.web&amp;gt;
&amp;lt;/configuration&amp;gt;&lt;/PRE&gt;
&lt;P&gt;This adds reference to the assembly that contains the &lt;CODE&gt;AspNetFShaprCodeProvider&lt;/CODE&gt; class and it configures ASP.NET to use F# code generator for &lt;CODE&gt;aspx&lt;/CODE&gt; files which &lt;CODE&gt;Language&lt;/CODE&gt; attribute is set to "F#". When this is done, you can add F# page by adding &lt;CODE&gt;aspx&lt;/CODE&gt; and code behind &lt;CODE&gt;fs&lt;/CODE&gt; files. In the &lt;CODE&gt;Page&lt;/CODE&gt; directive, set the &lt;CODE&gt;Language&lt;/CODE&gt; attribute to "F#" and define the code behind class in the &lt;CODE&gt;fs&lt;/CODE&gt; file.&lt;/P&gt;
&lt;P&gt;The following example shows code behind class for simple web page (that contains one button with ID set to &lt;CODE&gt;btnOk&lt;/CODE&gt;):&lt;/P&gt;&lt;PRE&gt;namespace FSharpWeb

open System;;
open System.Web.UI.WebControls;;

&lt;SPAN class=c&gt;&lt;FONT color=#006400&gt;// Code behind class declaration&lt;/FONT&gt;&lt;/SPAN&gt;
type Default = class inherit System.Web.UI.Page as base
  &lt;SPAN class=c&gt;&lt;FONT color=#006400&gt;// Declaration of button control&lt;/FONT&gt;&lt;/SPAN&gt;
  val mutable btnOk : Button;
  &lt;SPAN class=c&gt;&lt;FONT color=#006400&gt;// All fields must be initialized in constructor&lt;/FONT&gt;&lt;/SPAN&gt;
  new() = { btnOk = null; }
  &lt;FONT color=#006400&gt;&lt;SPAN class=c&gt;// Set text of button in onload&lt;/SPAN&gt;
&lt;/FONT&gt;  override this.OnLoad (e) =
    btnOk.Text &amp;lt;- "Hello world!";
    base.OnLoad (e);
end;;&lt;/PRE&gt;
&lt;P&gt;There is still one little difference when compared with C#, which is that it is required to manually declare all controls (with ID) that are on the page (&lt;CODE&gt;btnOk&lt;/CODE&gt; in this example). This is because F# doesn't (currently) support partial classes and so the partial class with control declarations that is generated by ASP.NET is dropped by the CodeDOM provider. The rest is very similar to how you develop ASP.NET pages in other languages; you can see more examples if you download attached example application. More information regarding object oriented programming in F# can be found in F# manual available at F# homepage [&lt;A&gt;3&lt;/A&gt;]. &lt;/P&gt;
&lt;H2&gt;Other CodeDom provider notes&lt;/H2&gt;
&lt;P&gt;As I mentioned earlier, the CodeDOM generator I created is still very limited and it is tested only with ASP.NET. You can find list of not implemented features in the release notes (see downloads). In the future, I'd like to extend it to support generating code for accessing web services too (code is generated using &lt;CODE&gt;wsdl&lt;/CODE&gt; tool). If you have any interesting idea, how it could be used or if you have any other comments, ideas, bug fixes, etc., please let me know (at &lt;A href="mailto:tomas@tomas.net"&gt;tomas@tomasp.net&lt;/A&gt;).&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;H2&gt;Downloads &amp;amp; links&lt;A name=aspfsintrol&gt;&lt;/A&gt;&lt;/H2&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://tomasp.net/projects/aspnetfsharp/sources.zip"&gt;F# CodeGenerator including sources and sample F# Web application&lt;/A&gt; (ZIP, 174kB)&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://tomasp.net/projects/aspnetfsharp/vstemplate.vsi"&gt;F# WebSite template for Visual Studio 2005&lt;/A&gt; (VSI, 65.7kB)&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://tomasp.net/projects/aspnetfsharp/notes.txt"&gt;Release notes (known issues and not implemented features)&lt;/A&gt; [&lt;A href="http://tomasp.net/projects/aspnetfsharp/notes.txt" target=_blank&gt;^&lt;/A&gt;]&lt;/LI&gt;&lt;/UL&gt;
&lt;UL&gt;
&lt;LI&gt;[1] &lt;A href="http://strangelights.com/fsharp/AspfDemo/default.aspx"&gt;F# Resources - Aspf Demo&lt;/A&gt; [&lt;A href="http://strangelights.com/fsharp/AspfDemo/default.aspx" target=_blank&gt;^&lt;/A&gt;] - StrangeLights.Com&lt;/LI&gt;
&lt;LI&gt;[2] &lt;A href="http://kahu.zoot.net.nz/codedom/"&gt;Sample CodeDom Provider&lt;/A&gt; [&lt;A href="http://kahu.zoot.net.nz/codedom/" target=_blank&gt;^&lt;/A&gt;] - Reference CodeDomProvider implementation&lt;/LI&gt;
&lt;LI&gt;[3] &lt;A href="http://research.microsoft.com/fsharp/fsharp.aspx"&gt;F#&lt;/A&gt; [&lt;A href="http://research.microsoft.com/fsharp/fsharp.aspx" target=_blank&gt;^&lt;/A&gt;] - Microsoft Research&lt;/LI&gt;&lt;/UL&gt;&lt;img src="http://cs.hubfs.net/aggbug.aspx?PostID=475" width="1" height="1"&gt;</description></item><item><title>F# - Simple quotations transformation</title><link>http://cs.hubfs.net/blogs/tomasp/archive/2006/07/07/413.aspx</link><pubDate>Fri, 07 Jul 2006 09:32:00 GMT</pubDate><guid isPermaLink="false">7372db05-f90c-40e3-82a2-789ed9f521c9:413</guid><dc:creator>tomasp</dc:creator><slash:comments>13</slash:comments><comments>http://cs.hubfs.net/blogs/tomasp/comments/413.aspx</comments><wfw:commentRss>http://cs.hubfs.net/blogs/tomasp/commentrss.aspx?PostID=413</wfw:commentRss><description>&lt;P&gt;This article describes very simple code that I wrote while learning how to work with the F# quotations library. Using the F# quotations you can get tree representation of the quoted expression. This allows you to write code that takes code written in F# as data and performs some code analysis or compiles/translates that code to different language. This is very similar to the new C# 3.0 expression trees where you can get expression tree from lambda expression and translate this tree for example to SQL (using DLINQ). However expression trees in C# 3.0 are very limited when compared with F# quotations, so that's one of the many reasons why F# is interesting language.&lt;/P&gt;
&lt;H2&gt;Introduction to F# quotations&lt;/H2&gt;
&lt;P&gt;If you want to see how F# represents some expressions you can try it in the FSI (F# interactive). First you need to open three modules that contain functions for working with quotations:&lt;/P&gt;&lt;PRE&gt;open Microsoft.FSharp.Quotations;;
open Microsoft.FSharp.Quotations.Raw;;
open Microsoft.FSharp.Quotations.Typed;;
&lt;/PRE&gt;
&lt;P&gt;Now you can see the internal representation of quotations by writing quoted expression (In VS.Net you can select code and hit Alt+Enter to sent selected code to F# interactive).&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=a&gt;&amp;lt;@ 1 + 2 @&amp;gt;&lt;/SPAN&gt;

val it : int expr
  = &amp;lt;@ 
      Microsoft.FSharp.MLLib.Pervasives.op_Addition (Int32 1) (Int32 2)
     @&amp;gt;  
&lt;/PRE&gt;
&lt;P&gt;One more example with two operators:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=a&gt;&amp;lt;@ 1 + 10/5 @&amp;gt;&lt;/SPAN&gt;

val it : int expr
  = &amp;lt;@ 
      Microsoft.FSharp.MLLib.Pervasives.op_Addition (Int32 1)
        (Microsoft.FSharp.MLLib.Pervasives.op_Division (Int32 10) (Int32 5))
     @&amp;gt;
&lt;/PRE&gt;
&lt;P&gt;It is not difficult to understand that addition in the first example is represented as function call (function application in the terminology of functional languages) where the function is &lt;CODE&gt;op_Addition&lt;/CODE&gt; operator and parameters are two integers. This is actually little simplification, because in functional programming you should look at this code as two function applications. The first application binds first parameter of &lt;CODE&gt;op_Addition&lt;/CODE&gt; to &lt;CODE&gt;1&lt;/CODE&gt; and the result is again function. The second application uses the function returned by the first application and passes &lt;CODE&gt;2&lt;/CODE&gt; as parameter. You can look at F# quotations using both approaches and in the rest of the article I will use the first one.&lt;/P&gt;
&lt;P&gt;To demonstrate how to work with F# quotations I decided to write simple transformation of limited mathematical expressions from F# quotations to my data type. The discriminated union that I'm using as target of transformation is similar to the union in F# source file VS.Net template (as you can see it supports only four most basic mathematical operations and all values are stored as integers):&lt;/P&gt;&lt;PRE&gt;type simple_expr =
  | Int of int
  | Add of simple_expr * simple_expr
  | Sub of simple_expr * simple_expr
  | Mul of simple_expr * simple_expr
  | Div of simple_expr * simple_expr;;
&lt;/PRE&gt;
&lt;P&gt;Printing and evaluation of this structure is simple, so I won't describe these here. If you want to look at these functions see attached source code. (function for evaluation is part of VS.Net template too).&lt;/P&gt;
&lt;H2&gt;Working with quotations&lt;/H2&gt;
&lt;P&gt;Finally, let's do the interesting part :-). As I mentioned you can get quotations using &lt;CODE&gt;&amp;lt;@ ... @&amp;gt;&lt;/CODE&gt;. When you want to perform analysis of quotations you need to get the underlying raw representation using the &lt;CODE&gt;&amp;lt;@@ ... @@&amp;gt;&lt;/CODE&gt; operators. For decomposing the quoted expression we can use &lt;CODE&gt;Query&lt;/CODE&gt; functions on the expression families defined in the &lt;CODE&gt;Raw&lt;/CODE&gt; module. This may sound a bit confusing, but in the simple words - the &lt;CODE&gt;Raw&lt;/CODE&gt; module contains several values that define language constructs (like constants, variables, function application and others) and you can use these values for matching and decomposing quoted expression. Following code should clear this a bit:&lt;/P&gt;&lt;PRE&gt;let what_is x =
  match Raw.efInt32.Query x with  
    | Some (_) -&amp;gt; "number";
    | _ -&amp;gt; 
  match Raw.efApps.Query x with  
    | Some (_) -&amp;gt; "application";
    | _ -&amp;gt; 
      "something else...";
	    
&lt;SPAN class=c&gt;// Prints "number"&lt;/SPAN&gt;	    
print_string (what_is &amp;lt;@@ 1 @@&amp;gt;);

&lt;SPAN class=c&gt;// Prints "application"&lt;/SPAN&gt;	    
print_string (what_is &amp;lt;@@ 1 + 2 @@&amp;gt;);
&lt;/PRE&gt;
&lt;P&gt;Now we can start writing function that converts mathematical expression quotation to &lt;CODE&gt;simple_expr&lt;/CODE&gt; structure. It will be recursive function with similar structure as previous. The first match for conversion of numbers will be simple. In the second match we'll have to do two things. First we'll need to determine what function is applied (whether it is addition, subtraction, etc..) and than we'll have to loop through parameters passed to this function and call the function recursively each parameter.&lt;/P&gt;&lt;PRE&gt;let rec parse x =
  match Raw.efInt32.Query x with  
    &lt;SPAN class=c&gt;// x contains the number so we can simply return Int(x)&lt;/SPAN&gt;
    | Some (x) -&amp;gt; Int(x); 
    | _ -&amp;gt; 
  match Raw.efApps.Query x with  
    | Some (op,args) -&amp;gt; 
    
      &lt;SPAN class=c&gt;// op contains quoted reference to function (operator)
      // so we need to extract name of applied function from it&lt;/SPAN&gt;
      let opname = (
        match Raw.efAnyTopDefn.Query op with  
          &lt;SPAN class=c&gt;// operators are top-level definitions so we extract name
          // from 'a' that contains info about the definition&lt;/SPAN&gt;
          | Some (a,_) -&amp;gt; let t1,t2 = a.topDefPath in t2
          | _ -&amp;gt; failwith ("Function is not a top level definition.");
        ) in
      
      &lt;SPAN class=c&gt;// Recursively translate arguments to simple_expr 
      // and convert returned list to array&lt;/SPAN&gt;
      let av = List.to_array(List.map (fun arg -&amp;gt; parse arg) args) in
      
      &lt;SPAN class=c&gt;// Finally, match the operation name with basic 
      // operator names and return according simple_expr value&lt;/SPAN&gt;
      (match opname with
        | "op_Addition"    -&amp;gt; Add(av.(0),av.(1))
        | "op_Subtraction" -&amp;gt; Sub(av.(0),av.(1))
        | "op_Multiply"    -&amp;gt; Mul(av.(0),av.(1))
        | "op_Division"    -&amp;gt; Div(av.(0),av.(1))
        | _ -&amp;gt;
          &lt;SPAN class=c&gt;// some other operation - error&lt;/SPAN&gt;
          failwith "Not supported operation");
    | _ -&amp;gt; 
      &lt;SPAN class=c&gt;// something else than efApps and efInt32 - error&lt;/SPAN&gt;
      failwith "Not supported construction in expression.";;
&lt;/PRE&gt;
&lt;P&gt;As I mentioned, the more interesting part of the code is when the quotation matches with &lt;CODE&gt;efApps&lt;/CODE&gt; which in our case means that the expression is mathematical operation. From the functional point of view &lt;CODE&gt;efApps&lt;/CODE&gt; means series of function applications. The result of &lt;CODE&gt;Query&lt;/CODE&gt; function of &lt;CODE&gt;efApps&lt;/CODE&gt; is tuple containing expression with information about applied function (operator in our case) and parameters of this application.&lt;/P&gt;
&lt;P&gt;For extracting operator name we match the first value with &lt;CODE&gt;efAnyTopDef&lt;/CODE&gt; which returns information about top-level definitions. Returned structure contains assembly name as well as the function name that we need. In the second step we recursively call &lt;CODE&gt;parse&lt;/CODE&gt; function to all arguments of operator. Once we know the function name and we have arguments translated to &lt;CODE&gt;simple_expr&lt;/CODE&gt;, we can return the according &lt;CODE&gt;simple_expr&lt;/CODE&gt; value.&lt;/P&gt;
&lt;P&gt;(Cross-posted from: &lt;A href="http://tomasp.net/blog/fsquotations.aspx"&gt;http://tomasp.net/blog/fsquotations.aspx&lt;/A&gt;)&lt;/P&gt;&lt;img src="http://cs.hubfs.net/aggbug.aspx?PostID=413" width="1" height="1"&gt;</description><enclosure url="http://cs.hubfs.net/blogs/tomasp/attachment/413.ashx" length="5464" type="application/x-zip-compressed" /></item><item><title>Welcome post</title><link>http://cs.hubfs.net/blogs/tomasp/archive/2006/07/01/399.aspx</link><pubDate>Sat, 01 Jul 2006 10:24:00 GMT</pubDate><guid isPermaLink="false">7372db05-f90c-40e3-82a2-789ed9f521c9:399</guid><dc:creator>tomasp</dc:creator><slash:comments>2</slash:comments><comments>http://cs.hubfs.net/blogs/tomasp/comments/399.aspx</comments><wfw:commentRss>http://cs.hubfs.net/blogs/tomasp/commentrss.aspx?PostID=399</wfw:commentRss><description>&lt;P&gt;So this is my first post to hubFS :-). First I'd like to give&amp;nbsp;thanks to&amp;nbsp;optionsScalper for creating blog for me. Before writing some more interesting posts on F# I'd like to introduce myself...&lt;/P&gt;
&lt;P&gt;I'm student of computer science from Czech Republic (studying at Charles University of Prague). I'm also Microsoft C# MVP and I'm student consultant at our university. As student consultant I'm organizing presentations on several (mostly) Microsoft&amp;nbsp;technologies and I'm also often talking there. Aside from studying I worked on many .NET projects including mostly ASP.NET development.&lt;/P&gt;
&lt;P&gt;I started learning F# some time ago (but I'm still very far from knowing it well) and I was impressed with so many interesting possibilities of this language. So far, my favorite F# feature is meta-programming (also&amp;nbsp;known as F# quotations), so my first posts will be focused on this topic.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My homepage is &lt;A href="http://tomasp.net/"&gt;http://tomasp.net&lt;/A&gt;&amp;nbsp;and most of the content that I publish somewhere else is also available at my homepage. You can contact me at &lt;A href="mailto:tomas@tomasp.net"&gt;tomas@tomasp.net&lt;/A&gt; &lt;/P&gt;&lt;img src="http://cs.hubfs.net/aggbug.aspx?PostID=399" width="1" height="1"&gt;</description></item></channel></rss>