Blog | TomasP.Nethttp://tomasp.net/blogBlog2008, Tomas PetricekBlogDynamic Lookup in F#http://tomasp.net/blog/fsharp-dynamic-lookup.aspxWed, 04 Jun 2008 01:50:08 GMT<p>Many people view <em>dynamic</em> and <em>statically-typed</em> languages as two distinct groups (and this is often a reason for never-ending discussions). In this article, I'll try to show one interesting example, which demonstrates that these two groups are not in fact that distinct and that you can implement a common <em>dynamic</em> language feature in F#, which is undoubtedly <em>statically-typed</em>. The feature that I'm talking about is dynamic invoke using a symbolic representation of the member (this is something that can be done using <em>symbols</em> in Ruby, but I'll shortly explain what exactly I mean).</p> <p>I intentionally wrote <em>statically-typed</em> and <em>dynamic</em> instead of <em>dynamically-typed</em>. In my understanding <em>dynamic</em> is a broader term while <em>dynamically-typed</em> and <em>statically-typed</em> are of course two distinct groups. On the other side <em>dynamic</em> refers to language features that are usually available in <em>dynamically-typed</em> languages, just because it is easy to support them in a nice way. This doesn't mean that having a <em>dynamic</em> feature in a <em>statically-typed</em> language would be impossible - it is just more difficult to implement it in a way that would be similarly elegant. </p> Thesis: Client-side Scripting using Meta-programminghttp://tomasp.net/blog/webtools-thesis.aspxMon, 17 Mar 2008 10:07:04 GMT<p>I realized that I haven’t yet posted a link to my Bachelor Thesis, which I partially worked on during my visit in Microsoft Research and which I successfully defended last year. The thesis is about a client/server web framework for F# called F# WebTools, which I already <a href="http://tomasp.net/blog/fswebtools-intro.aspx" type="external">mentioned here</a> and its abstract is following:</p> <p style="padding-left:40px;padding-right:40px;font-style:italic;">“Ajax” programming is becoming a de-facto standard for certain types of web applications, but unfortunately developing this kind of application is a difficult task. Developers have to deal with problems like a language impedance mismatch, limited execution runtime in web browser on the client-side and no integration between client and server-side parts that are developed as a two independent applications, but typically form a single and homogenous application. In this work we present the first project that deals with all three mentioned problems but which still integrates with existing web technologies such as ASP.NET on the server and JavaScript on the client. We use the F# language for writing both client and server-side part of the web application, which makes it possible to develop client-side code in a type-safe programming language using a subset of the F# library, and we provide a way to write both server-side and client-side code as a part of single homogeneous type defining the web page logic. The code is executed heterogeneously, part as JavaScript on the client, and part as native code on the server. Finally we use monadic syntax for the separation of client and server-side code, tracking this separation through the F# type system.</p> <p style="text-indent:0px">The full text is available here: <a href="/articles/webtools-thesis/fswebtools.pdf">Client side scripting using meta-programming</a> (PDF, 1.31MB) </p>New Version of Phalanger with Silverlight and VS 2008 Supporthttp://tomasp.net/blog/phalanger-vs2008.aspxTue, 11 Mar 2008 01:14:21 GMT<p>At the <a href="http://langnetsymposium.com/talks.asp" type="external">Lang.NET Symposium</a> I presented several new features in Phalanger. I already posted the presentation and samples on the blog (<a href="http://tomasp.net/blog/phalanger-at-langnet.aspx" type="external">here</a>) and the talk is also available as a video on the Lang.NET web site (<a href="http://langnetsymposium.com/talks.asp" type="external">here</a> - actually, due to the huge demand the site is down now, but I hope it gets back soon!) Anyway, the most interesting thing is that I've used Phalanger Integration for <strong>Visual Studio 2008</strong> during the talk, which wasn't available in any Phalanger release until now.</p> <p>I have to mention one more thing before talking more about the new release - Phalanger Integration for VS 2008 works with Visual Studio Shell, which means that you can get <strong>Visual Studio 2008 with Phalanger Support for free!</strong> Yes, that's right. Microsoft offers so called Visual Studio 2008 Shell, which is an "empty" IDE with no integrated languages and you can install Phalanger Integration into this version, which means that you can get very good Phalanger IDE (for Windows) for no cost at all!</p> <p>If you can't wait to install this new version, you can skip the overview article and <a href="/blog/phalanger-vs2008.aspx#phpm08down">go directly to the installation links</a>.</p> F# Support for ASP.NET and Notes on Sampleshttp://tomasp.net/blog/aspnet-in-fsharp.aspxSat, 08 Mar 2008 23:07:29 GMT<p>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 [<a href="#myfsstuff">1</a>], 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#. </p> Phalanger at Lang.NET Symposium http://tomasp.net/blog/phalanger-at-langnet.aspxSat, 02 Feb 2008 11:51:38 GMT<p>Last week I attended the <a href="http://www.langnetsymposium.com/" type="external">Lang.NET Symposium</a> and I also did a short presentation about Phalanger (below you can find the demos and slides from my talk). By the way – during the trip from Prague to Seattle I missed a connecting flight from Paris, so I had one day to visit Paris and since I was traveling on Saturday I actually quite enjoyed it :-), so that’s where the picture comes from.</p> <p>Overall it was really a fantastic event with many great talks from many interesting people. I hope that there will be recordings as a last year, so I won’t comment every topic that I found interesting (that would be really a long post). But just quickly – people from Microsoft often talked about DLR (dynamic language runtime) related topics, which was interesting as we’re planning to look at DLR in Phalanger. Luckily, <a href="http://netevil.org/" type="external">Wez Furlong</a> from the PHP community was there too, so we discussed how we could work on “PHP on DLR” project together (see also our <a href="http://news.php.net/php.on.dlr" type="external">mailing list</a>).</p> <a target="_blank" href="/articles/phalanger-at-langnet/photo.jpg"><img style="float:right;margin:10px;border:0px" alt="Trip to Seattle" src="/articles/phalanger-at-langnet/photo_sm.jpg" /></a> <p>There were also two talks about F# (from <a type="external" href="http://blogs.msdn.com/lukeh">Luke Hoban</a> and <a href="http://www.devhawk.net/" type="external">Harry Pierson</a>), and it was a lot of fun to hang out with them. Finally, Erik Meijer presented the <a href="http://labs.live.com/volta" type="external">Volta</a> project, which I was particularly interested in as it shares many goals and ideas with my F# Web Tools.</p> <p>Non-Microsoft talks covered wider range of topics including Mono and Moonlight project (by <a href="http://tirania.org/blog" type="external">Miguel de Icaza</a>) and I actually had a chance to talk with Miguel about Phalanger and we even tried running the Helicopter sample on Moonlight – there were some issues, but it seems quite promising, so I hope to have the sample running on Moonlight on Linux quite soon! Miguel mentioned that the installation of Moonlight is currently a bit painful (due to some issues with media codecs), but it should be fixed in next few weeks, so I hope to be able to try it on my machine too!</p>Writing Silverlight applications in PHPhttp://tomasp.net/blog/php-in-silverlight.aspxFri, 07 Dec 2007 17:16:14 GMT<p>In my <a href="http://tomasp.net/blog/phalanger-future-notes.aspx">last post</a> about Phalanger I mentioned that our important goal is to support the Silverlight (2.0) platform. Shortly Silverlight is a cross-browser platform that can be used for developing client-side components that run in the web browser and contain rich media, graphics and can interactively communicate with the user. The language that can be used for writing Silverlight code can be in general any .NET language, so our goal is to allow using PHP by making Phalanger compatible with Silverlight.</p> <img src="/articles/php-in-silverlight/car_intro_sm.png" alt="Simple Silverlight App in PHP" style="margin:10px; float:left;" /> <p>First steps were already made and it is becoming possible to write some very interesting things in Silverlight using PHP, there is of course still a lot of work to do and we're discussing the future development with PHP development team (you can join the mailing list <a href="http://news.php.net/php.on.dlr" type="external">PHP on DLR</a> for more info). In this article we will first show a very basic Silverlight example that uses PHP and later I will shortly comment more complicated example - a game (quite addicting, so be careful :-)!) where you have to fly with helicopter and avoid the walls. The source code for helicopter game is also attached, so feel free to modify it or create similar games!</p> <p>If you can't wait to try the demos before looking at the sources, here are the links:</p> <ul> <li><a href="http://tomasp.net/articles/php-in-silverlight/simplegui.html" type="external">Simple car demo</a> - Click on the car and it will move!</li> <li><a href="http://tomasp.net/articles/php-in-silverlight/game.html" type="external">Helicopter game demo</a> - Click left button to fly up!</li> </ul>A few notes about Phalanger futurehttp://tomasp.net/blog/phalanger-future-notes.aspxSun, 02 Dec 2007 23:50:53 GMT<p>First, I'd like to aplogoize for the lack of new about the Phalanger project recently. I spent some time working as an intern with the F# team in MSR Cambridge and I also had final bachelor exam this autumn. Anyway, this doesn't mean that there isn't anything new and interesting regarding the Phalanger project comming soon - actually (as I believe) the opposite is true! The topics that I'm going to further discuss in this article is our upcomming support for Silverlight platform, the relation between Phalanger and DLR and also our desire to make Phalanger more open to the community, so stay tuned!</p> Infinite Cheese Fractal using WPF 3D and F#http://tomasp.net/blog/infinite-cheese.aspxSat, 24 Nov 2007 23:22:04 GMT<p>I always liked fractals, because they look like objects from another world, but on the other side if you look at some things in our world you can see many similarities with fractals (but not quite as ideal with the infinite level of precision). One of my favorite fractals is 3D version of Sierpinski carpet [<a href="#wpffractref">1</a>], which itself is based on very famous Cantor set. Quite long time ago I thought that it would be nice to implement animation of flying through this fractal, but I was never good in 3D graphics and it looked like a lot of work, so I never get to doing it. Luckily, now with F#, which makes it very easy to write the code to generate the fractal and with WPF 3D, which can be easily used to animate the fractal, I finally had everything I needed to do it, so here it is! </p> <div style="text-align:center;padding:10px;margin-left:auto;margin-right:auto;"> <embed src="http://images.soapbox.msn.com/flash/soapbox1_1.swf" quality="high" width="412" height="362" wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://macromedia.com/go/getflashplayer" flashvars="c=v&amp;v=e19faaa5-bc85-4d95-8408-6cb6b04aeb16"></embed><br /> </div> Asynchronous Programming in C# using Iteratorshttp://tomasp.net/blog/csharp-async.aspxThu, 15 Nov 2007 03:08:35 GMT<p>In this article we will look how to write programs that perform asynchronous operations without the typical inversion of control. To briefly introduce what I mean by 'asynchronous' and 'inversion of control' - asynchronous refers to programs that perform some long running operations that don't necessary block a calling thread, for example accessing the network, calling web services or performing any other I/O operation in general. The inversion of control refers to the code structure that you have to use when writing a code that explicitly passes a C# delegate as a callback to the asynchronous method (typically called <code>Begin<em>Something</em></code> in .NET). The asynchronous method calls the delegate when the operation completes, which reverses the way you write the code - instead of encoding the control flow using typical language constructs (e.g. <code>while</code> loop) you have to use global variables and write your own control mechanism.</p> <p>The funny thing about this article is that it could have been written at least 3 years ago when a beta version of Visual Studio 2005 and C# 2.0 became first available, but it is using iterators in a slightly bizarre way, so it is not easy to realize that this is possible. Actually, I will use some C# 3.0 methods in the article as well, but only extension methods and mainly just to keep the code nicer. As with my earlier article about building LINQ queries at runtime, I realized that it can be done in C# when I was playing with the F# solution (called F# Asynchronous Workflows), where this approach is very natural, so I will shortly mention the F# implementation as well.</p> F# Overview (IV.) - Language Oriented Programminghttp://tomasp.net/blog/fsharp-iv-lang.aspxSat, 03 Nov 2007 00:00:04 GMT<p>In the fourth article of the F# overview series, I will shortly describe how I understad the language oriented paradigm and how the F# language can be used for developing libraries using this paradigm. We will look how discriminated unions relate to this paradigm and at three specific features that support this paradigm, namely <em>active patterns</em>, <em>computation expressions</em> and <em>quotations</em>.</p> <p>Defining precisely what the term <em>language oriented programming</em> means in context of the F# language would be difficult, so I will instead explain a few examples that will demonstrate how I understand it. In general, the goal of language oriented programming is to develop a <em>language</em> that would be suitable for some (more specific) class of tasks and use this language for solving these tasks. Of course, developing a real programming language is extremely complex problem, so there are several ways for making it easier. As the most elementary example, you can look at XML files (with certain schema) as language that are processed by your program and solve some specific problem (for example configuring the application). As a side note, I should mention that I'm not particularly happy with the term ‘language’ in this context, because the term can be used for describing a wide range of techniques from very trivial constructs to a complex object-oriented class libraries, but I have not seen any better term for the class of techniques that I’m going to talk about.</p>F# Overview (III.) - Imperative and Object-Oriented Programminghttp://tomasp.net/blog/fsharp-iii-oop.aspxSat, 03 Nov 2007 00:00:03 GMT<p>In the third part of the F# Overview article series, we will look at language features that are mostly well known, because they are present in most of the currently used programming languages. Indeed, I'm talking about imperative programming, which is a common way for storing and manipulating application data and about object oriented programming which is used for structuring complex programs.</p> <p>In general, F# tries to make using them together with the functional constructs described in the <a href="fsharp-ii-functional.aspx">previous part</a> [<a href="fsharp-ii-functional.aspx" target="_blank">^</a>] as natural as possible, which yields several very powerful language constructs.</p>F# Overview (II.) - Functional Programminghttp://tomasp.net/blog/fsharp-ii-functional.aspxSat, 03 Nov 2007 00:00:02 GMT<p>In the second part of the F# overview we will look at functional programming, which is probably the most important paradigm used with the F# language, because F# is built on the same grounds as many functional languages. We will first examine the standard F# data types, which are useful to allow the functional programming style and we will also look at a few functional tricks.</p> <p>As already mentioned in the <a href="fsharp-i-introduction.aspx">Introduction</a> for this article series, F# is a typed functional language, by which I mean that types of all values are determined during the compile-time. However, thanks to the use of a type inference, the types are explicitly specified in the code very rarely as we will see in the following examples. Basic data types (aside from a standard set of primitive numeric and textual types that are present in any .NET language) available in F# are tuple, discriminated union, record, array, list, function and object. In the following quick overview, we will use the F# interactive, which is a tool that compiles and executes the entered text on the fly.</p> F# Overview (I.) - Introductionhttp://tomasp.net/blog/fsharp-i-introduction.aspxSat, 03 Nov 2007 00:00:01 GMT<p>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 <em>paradigms</em> 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.</p> Lazy Computation in C# on MSDNhttp://tomasp.net/blog/lazy-computation-on-msdn.aspxSat, 06 Oct 2007 01:29:59 GMT<p>I think that one of the interesting things about C# 3.0 is that it gives you the ability to use many techniques known from functional languages (like Haskell or F#). Most of the articles about C# 3.0 and LINQ focus on the queries and LINQ to SQL, but I believe that using these functional techniques deserve some attention as well. This is why I'm very happy that my article about one of these techniques - representing lazy computations - is now available at the C# Developer Center. I would like to thank to <a href="http://blogs.msdn.com/charlie/default.aspx">Charlie Calvert</a> [<a href="http://blogs.msdn.com/charlie/default.aspx" target="_blank">^</a>], who is the Community Program Manager for C# and who edited and published my article there. Here is the annotation:</p> <p style="padding-left:40px;padding-right:40px;font-style:italic;">Most of the programming languages used in practice (including for example C#, VB.NET, C++, Python or Java) employ so called eager evaluation, which means that the program evaluates all expression and statements in the order in which they are written, so all the preceding statements and expressions are evaluated before executing the next piece of code. This, for example, means that all arguments to a method call are evaluated before calling the method. Sometimes it may be useful to delay an execution of some code until the result is actually needed, either because the result may not be needed at all (but we can’t tell that before executing some computation) or because we don’t want to block the program for a long time by executing all computations in advance and instead we want to execute the computations later, when we will actually need the result. </p> <p style="padding-left:40px;margin-bottom:20px;padding-right:40px;font-style:italic;">In this article we will look how these lazy computations can be written in C# (using some of the new language features from version 3.0). We will first implement a Lazy class to represent this kind of computation, then look at a few simple examples to demonstrate how the class can be used, and finally we will examine one slightly more complicated, but practically useful application. </p> <p style="text-indent:0px">You can read the complete article here: <a href="http://msdn2.microsoft.com/en-us/vcsharp/bb870976.aspx">Lazy Computation in C# </a> [<a href="http://msdn2.microsoft.com/en-us/vcsharp/bb870976.aspx" target="_blank">^</a>]</p>F# Quotations Samples on CodePlexhttp://tomasp.net/blog/fsharp-quotation-samples.aspxThu, 20 Sep 2007 04:33:49 GMT<p>Some time ago, Granville Barnett (see his <a href="http://gbarnett.org/">homepage and old blog</a> [<a href="http://gbarnett.org/" target="_blank">^</a>] or a <a href="http://weblogs.asp.net/gbarnett/">new blog</a> [<a href="http://weblogs.asp.net/gbarnett/" target="_blank">^</a>]) had a great idea and started a CodePlex project called <a href="http://www.codeplex.com/fsharpsamples">F# Samples</a> [<a href="http://www.codeplex.com/fsharpsamples" target="_blank">^</a>] to host various samples written in F# to demonstrate the most important concepts of both functional programming and F#. I quite like this idea, so I asked Granville if I could join and add some samples that I wrote and today I finally found a time to update what I wanted to upload to the latest version of F# and put it online.</p> Building LINQ Queries at Runtime in F#http://tomasp.net/blog/dynamic-flinq.aspxSat, 18 Aug 2007 02:38:11 GMT<p>In an article about building LINQ queries at runtime in C# 3.0, I described how you can build a LINQ query dynamically, for example by combining a set of conditions using the 'or' operator in the where clause. I mentioned that the way I implemented it is largely influenced by the F# language, which provides very natural way for manipulations with code like this. In this article I will first shortly introduce FLINQ sample, which is an F# library implementing LINQ support and than I will implement the same examples I presented in the earlier article in F#.</p> Building LINQ Queries at Runtime in C#http://tomasp.net/blog/dynamic-linq-queries.aspxMon, 30 Jul 2007 02:10:17 GMT<p>Since the first beta versions of LINQ we could hear comments that it is perfect for queries known at compile-time, however it is not possible to use it for building queries dynamically at runtime. In this article I show that this can be actually done very well for most of the common cases. The solution offered by Microsoft (mentioned in [<a href="#dynqlinks">1</a>]) is to build query from a string, however this has many limitations and it in fact goes completely against what LINQ tries to achieve, which is writing queries in a type-safe way with full compile-time checking. In this article I will first show a few support functions to make the life a bit easier and then we will use them for building two sample applications that allows user to build a query dynamically. The solution is largely motivated by my previous use of F#, where working with “expressions” is possible at more advanced level, however I’ll write about F# later and now let’s get back to C# 3.0...</p>F# Web Tools: "Ajax" applications made simplehttp://tomasp.net/blog/fswebtools-intro.aspxFri, 13 Jul 2007 04:32:29 GMT<p>Traditional "Ajax" application consists of the server-side code and the client-side part written in JavaScript (the more dynamicity you want, the larger JS files you have to write), which exchanges some data with the server-side code using XmlHttpRequest, typically in JSON format. I think this approach has 3 main problems, which we tried to solve in F# Web Toolkit. There are a few projects that try to solve some of them already - the most interesting projects are Volta from Microsoft [<a href="#fswtintrolinks">1</a>], Links language [<a href="#fswtintrolinks">3</a>] from the University of Edinburgh and Google Web Toolkit [<a href="#fswtintrolinks">2</a>], but none of the projects solve all three problems at once. </p> <ol> <li>Limited client-side environment</li> <li>Discontinuity between server and client side</li> <li>Components in web frameworks are only server-side</li> </ol> <p>The aim of the F# Web Toolkit is to solve all these three problems...</p>Using PHP objects from C# in a type-safe wayhttp://tomasp.net/blog/ducktyping-in-phalaner.aspxMon, 30 Apr 2007 01:26:24 GMT<p>When you want to call PHP scripts from mainstream .NET languages, like C# you can follow two different ways. First you can use the pure mode as I demonstrated in one of the earlier articles on PHP application called Texy!. This approach can be used only for some specific applications, because pure mode has several restrictions - the two most important restrictions are that no global code or inclusions are allowed (you have to specify all source files during the compilation), but thanks to this restrictions Phalanger is able to produce classes that are compatible with .NET and can be called from C#. Second option is to create object dynamically by its name and perform all method invocations by name too. This approach can be used with any PHP scripts, but it isn't very convenient. In this article we present new features in the Phalanger beta 4 which extend the second approach and make it possible to use objects from any PHP script in C# using type-safe way.</p> Phalanger 2.0 Beta 4 available!http://tomasp.net/blog/phalanger-beta4.aspxSun, 22 Apr 2007 16:24:09 GMT<p>This week we finished new release of Phalanger (the PHP compiler for .NET platform). The goal in this release was to fix many minor bugs preventing us from running some of the famous and most often deployed open-source PHP applications, so with the new release you'll be able to run for example <strong>MediaWiki</strong>, the wiki application that evolved from system used on Wikipedia or probably the best-known PHP blogging and publishing system <strong>WordPress</strong>. Other applications that we tested can be found at Phalanger website in <a href="http://php-compiler.net/doku.php?id=apps">the list of tested PHP applications</a> [<a href="http://php-compiler.net/doku.php?id=apps" target="_blank">^</a>]. Compatibility is very important for us, so if you have troubles running any open-source PHP application, let us know and we'll include it in our list!</p> <p>Another thing that we focused on is interoperability between PHP scripts compiled using Phalanger and other .NET languages. Using .NET objects from Phalanger is very intuitive and has almost no limitations, however the other way is a bit difficult because of the dynamic nature of PHP language. If your PHP application can be compiled in the Phalanger <strong>pure</strong> mode, than the PHP objects can be exported and made available to C# and other languages. The methods of objects exported using this method take <code>object</code> (the base class for every .NET object) as a parameter, because PHP checks types at runtime, so this may be a bit confusing for the users. In the Beta 4 we tried to target these two issues - first, the fact that you have to use pure mode and second, the fact that exported objects can't contain any information about accepted types. This is a topic for entire article, so I'll write about it soon... Stay tuned :-)!</p> <p>Here are the links for Phalanger 2.0 Beta 4 release:</p> <ul> <li><a href="http://www.codeplex.com/Phalanger/Release/ProjectReleases.aspx?ReleaseId=3375"> Phalanger 2.0 Beta 4 release page</a> [<a href="http://www.codeplex.com/Phalanger/Release/ProjectReleases.aspx?ReleaseId=3375" target="_blank">^</a>] - CodePlex</li> <li><a href="http://www.codeplex.com/PhalangerMySQL/Release/ProjectReleases.aspx?ReleaseId=3502"> Phalanger MySQL extension Beta 4</a> [<a href="http://www.codeplex.com/PhalangerMySQL/Release/ProjectReleases.aspx?ReleaseId=3502" target="_blank">^</a>] - CodePlex</li> </ul> Keep your multi-core CPU busy with F#http://tomasp.net/blog/fsparallelops.aspxSat, 24 Mar 2007 23:13:48 GMT<p>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 <a href="http://www.gotw.ca/publications/concurrency-ddj.htm">The Free Lunch Is Over</a> [<a href="http://www.gotw.ca/publications/concurrency-ddj.htm" target="_blank">^</a>] written by Herb Sutter).</p> <p>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). </p> <p>This article describes the code that makes it possible to parallelize some common F# constructs like the <code>List.map</code> and <code>List.filter</code>...</p>CLinq - LINQ support for the C++/CLI languagehttp://tomasp.net/blog/clinq-project.aspxFri, 02 Mar 2007 17:11:05 GMT<p>I started working on this project, because I attended C++ class at our university and I had to do some application in C++. Because I hate doing useless projects I wanted to work on something interesting and so I started thinking whether it would be possible to enable LINQ support in C++/CLI...</p> <p>C++/CLI is a very flexible language and the following example proves that enabling LINQ support in C++/CLI isn't impossible. The following database query returns name of contact and company for all customers living in London:</p> <pre lang="c++"> // create connection to database NorthwindData db(".. connection string .."); // declare database query Expr&lt;Customers^&gt; cvar = Var&lt;Customers^&gt;("c"); CQuery&lt;String^&gt;^ q = db.QCustomers -&gt;Where(clq::fun(cvar, cvar.City == "London")) -&gt;Select(clq::fun(cvar, cvar.ContactName + Expr&lt;String^&gt;(", ") + cvar.CompanyName)); // execute query and output results for each(String^ s in q-&gt;Query) Console::WriteLine(s); </pre> <p>If you are interested in more information about CLinq project you can...</p> <ul> <li><a href="/articles/clinq-project.aspx">.. continue and read the entire article</a></li> <li><a href="http://www.codeplex.com/linqextensions">Visit the CodePlex project homepage</a> [<a href="http://www.codeplex.com/linqextensions" target="_blank">^</a>]</li> </ul>Overload resolution in Phalangerhttp://tomasp.net/blog/phalanger-overload-resolution.aspxThu, 15 Feb 2007 21:46:45 GMT<p>PHP language itself doesn't method support overloading (having two methods with same name, but different number or types of parameters). This brings an interesting problem to Phalanger, because most of .NET languages support this and if we want to be able to call any .NET object from PHP we need to add support (at least) for calling of overloaded methods. The latest Phalanger release contains overload resolution described in the <em>Integrating PHP with CLR</em> document [<a href="#phpoverloadlnk">1</a>].</p> <p>For example, when calling the <code>Console::WriteLine</code> method (which has a lot of overloads), Phalanger dynamically generates a piece of code that we call dynamic stub, which is responsible for choosing the most appropriate overload depending on the actual parameter types. This stub is generated only once for every method, which makes this implementation very efficient. The difficult part of overload resolution is, how can the stub determine what is the best overload? PHP language has a lot of implicit conversions, so when you pass the string <code>"10.2 Little Piggies"</code> to a method it can be implicitly converted to float (<code>10.2</code>) (For more details see [<a href="#phpoverloadlnk">2</a>]). Another example of implicit conversion is that any boolean value can be converted to string (empty string or string <code>"0"</code> are converted to <code>false</code>, every other string is converted to <code>true</code>).</p> <p>In this article I'll describe how does the dynamic stub look like in current version of Phalanger, what problems can it cause and how are we going to fix it in the future version!</p>Quotations Visualizer for F# 1.1.13.8http://tomasp.net/blog/quotvis-1.1.13.8.aspxWed, 14 Feb 2007 23:35:03 GMT<p>Quotation Visualizer is my tool that shows visual representation of F# quotations. Stephen noticed (Thanks!) that Quotation Visualizer doesn't compile with the latest version of F# (1.1.13.8), so here is the updated version:</p> <ul> <li><a href="http://tomasp.net/articles/quotvis-update/quotvis_bin.zip">Download executable</a> (51kB)</li> <li><a href="http://tomasp.net/articles/quotvis-update/quotvis_src.zip">Download sources</a> (77kB)</li> </ul> <p class="noindent">If you want to know more about this tool, here are links to previous articles about it:</p> <ul> <li><a href="http://tomasp.net/blog/quotvis.aspx">F# quotations visualizer</a></li> <li><a href="http://tomasp.net/blog/quotvis-reloaded.aspx">F# quotations visualizer - reloaded!</a></li> </ul> <h2>What was wrong?</h2> <p>This tool is written using F# light syntax, which means that you don't have to write all the semicolons and F# compiler uses whitespace to determine end of the blocks (and some other constructs). This compiler option is relatively new so it is still evolving. First issue that I had is with class declaration - the following code is not valid with the latest F# version:</p> <pre lang="fsharp"> type MyForm = class inherit Form as base // ... end </pre> <p>I used it because it looks more familiar to C# developers :-). Instead of it you have to write the following:</p> <pre lang="fsharp"> type MyForm = class inherit Form as base // ... end </pre> <p>The second issue was with the pipeline operator which is useful when working with lists (or other sequences). The correct syntax for using pipeline operator (for expressions that are longer than one line) is following:</p> <pre lang="fsharp"> let filteredList = oldArray |&gt; Array.to_list |&gt; List.map ( fun m -&gt; /* ... */ ) |&gt; List.filter ( fun m -&gt; /* ... */ ) </pre> Compiling Texy! with Phalangerhttp://tomasp.net/blog/aspnettexy.aspxMon, 12 Feb 2007 00:45:45 GMT<p>Texy! [<a href="#texyphallinks">1</a>] is a convertor from text format (similar to formats used in some wiki applications) to valid XHTML code written in PHP. The syntax is described at Texy! web page [<a href="#texyphallinks">2</a>]. Unfortunately, it is only in Czech language, but the syntax is very straightforward, so you can understand it without learning Czech :-).</p><p> In this article, we'll examine how to compile Texy! using Phalanger in pure mode. In this mode it is possible to use objects from PHP like any other .NET objects, so we can later used the compiled assembly for example in the following C# code:</p> <pre> <span class="c">// Create instance of Texy! parser</span> Texy t = <span class="k">new</span> Texy(); <span class="c">// Call the 'process' method and cast result to string</span> <span class="k">string</span> parsed = (<span class="k">string</span>)t.process(txtTexy.Text); <span class="c">// Display parsed text using literal</span> ltrOutput.Text = parsed; </pre>Can't return anonymous type from method? Really?http://tomasp.net/blog/cannot-return-anonymous-type-from-method.aspxTue, 23 Jan 2007 23:54:31 GMT<p>One of the new features introduced in C# 3.0 which will be available in Visual Studio "Orcas" (currently in CTP version) is anonymous type. Anonymous type is something very similar to tuple type from Cω [<a href="#returnanonymous">1</a>] (which is based on tuple types known from many functional programming languages including F#). Anonymous types are extremely useful in LINQ queries, because it allows you to construct type with several properties without declaring the type (with all the properties). Example of query with anonymous type looks like this:</p> <pre> <span class="k">var</span> q = <span class="k">from</span> c <span class="k">in</span> db.Customers <span class="k">where</span> c.Country = <span class="s">"Czech Republic"</span> <span class="k">select new</span> { FullName=c.Name+<span class="s">" "</span>+c.Surname, Address=c.Address }; </pre> <p>Ok, it's probabbly not the best example, but it demonstrates the point - you want to return some information from query and you don't need to declare type that contains <code>FullName</code> and <code>Address</code> properties before, because you need it only for this single query (and you want to return only these two fields, so you don't transfer additional data that you don't need from database).</p><p>Now let's get to the second point... </p> Phalanger has a new website!http://tomasp.net/blog/new-phalanger-website.aspxSun, 14 Jan 2007 00:49:42 GMT<p>As you may know, I became new project lead for the <a href="http://php-compiler.net/">Phalanger project</a> [<a href="http://php-compiler.net/" target="_blank">^</a>]. Phalanger was started as a project at Charles University (where I'm studying) by Tomas Matousek, Ladislav Prosek and 4 other guys, the first version was very successful and they started wokring on the second version which introduces PHP in the family of first-class .NET languages (which makes it fully interoperable with the rest of .NET world). After releasing the second version, Tomas and Ladislav moved to Microsoft and convinced me to continue in the Phalanger development. Most of the work on the 2.0 version was already done, so we're currently working on implementing the rest of the PHP/CLR language extensions, making Phalanger more compatible with existing PHP apps and so on. For more information about the history of Phalanger visit the <a href="http://www.php-compiler.net/doku.php?id=core%3ahistory">project history page</a> [<a href="http://www.php-compiler.net/doku.php?id=core%3ahistory" target="_blank">^</a>].</p> <p>I think that Phalanger is in the phase when it can be very interesting for both PHP and .NET communities, but it never got greater publicity and there are not very much examples and articles to demonstrate the interesting Phalanger features. To improve this we started working on the new Phalanger website where users could share their experiences, tips etc. We used PHP wiki called <a href="http://wiki.splitbrain.org/wiki:dokuwiki">DokuWiki</a> [<a href="http://wiki.splitbrain.org/wiki:dokuwiki" target="_blank">^</a>] (running, of course on Phalanger) and you can find the new website here:</p> <ul> <li><a href="http://www.php-compiler.net">Phalanger - Homepage</a> [<a href="http://www.php-compiler.net" target="_blank">^</a>] (the new project homepage)</li> <li><a href="http://www.php-compiler.net/doku.php?id=user-wiki">Phalanger - User wiki</a> [<a href="http://www.php-compiler.net/doku.php?id=user-wiki" target="_blank">^</a>] (you can edit pages in user wiki after logging in)</li> </ul> <p>I also wrote two articles about Phalanger (and I'm working on more :-)) to explain the most important and interesting Phalanger features and concepts. You can find the articles at our web:</p> <ul> <li> <a href="http://www.php-compiler.net/doku.php?id=core%3aphalanger_for_.net_developers">Phalanger for .NET developers</a> [<a href="http://www.php-compiler.net/doku.php?id=core%3aphalanger_for_.net_developers" target="_blank">^</a>] - focused on .NET interoperability and features interesting for C# developers </li> <li> <a href="http://www.php-compiler.net/doku.php?id=core%3aphalanger_for_mono_users">Phalanger for Mono users</a> [<a href="http://www.php-compiler.net/doku.php?id=core%3aphalanger_for_mono_users" target="_blank">^</a>] - Phalanger 2.0 supports Mono, so here is an introduction for Mono users</li> </ul> <p>I'll also continue blogging about Phalanger, so you can expect more information about Phalanger, problems that we're currently solving as well as some other tips, tricks in this blog. If you're interested only in Phalanger you can use <a href="http://tomasp.net/blog/rss.aspx?30">this RSS feed</a> to monitor new articles in this blog (it shows only articles with the Phalanger tag).</p>F# presentation at Czech .NET Group meetinghttp://tomasp.net/blog/fsharp-presentation.aspxTue, 14 Nov 2006 00:52:59 GMT<p>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.</p><p>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:</p><ul><li><strong>Functional programming in F#</strong> - Introduction to the F# type system</li><li><strong>Some useful functional idioms</strong> - How to do Foldl/Map/Filter functions and Lazy evaluation in C#</li><li><strong>Interactive scripting</strong> - What is important for scripting, mathematical simulation</li><li><strong>Interoperability between F# and other .NET languages</strong> - How to use .NET libraries from F# and F# libraries from ohter .NET languages</li><li><strong>F# as a language for ASP.NET</strong> - How to use F# as a language for ASP.NET development</li><li><strong>Meta-programming in F#</strong> - Meta-programming features in F# and the FLINQ project</li></ul><h2>Downloads</h2><ul><li>Presentation - <a href="http://www.tomasp.net/articles/fsharp-presentation/fsharp-intro.ppt">Czech</a> (889 kB), <a href="http://www.tomasp.net/articles/fsharp-presentation/fsharp-intro-en.ppt">English</a> (894 kB)</li><li>Demos - <a href="http://www.tomasp.net/articles/fsharp-presentation/demos.zip">Czech</a> (500 kB), <a href="http://www.tomasp.net/articles/fsharp-presentation/demos-en.zip">English</a> (501 kB)</li></ul>Concepts behind the C# 3.0 languagehttp://tomasp.net/blog/csharp3-concepts.aspxSun, 15 Oct 2006 15:19:03 GMT<p>One of the lectures that I attended last year was <a href="http://www.mff.cuni.cz/toISO-8859-2.en/vnitro/is/sis/predmety/kod.php?kod=PRG003">Programming Methodology and Philosophy of Programming Languages</a>. The lecture was mostly about history of programming languages and how several features evolved, disappeared and than after many years appeared again in another programming language.</p><p>As I final work I decided to write an article that describes ideas that influenced the design of the C# 3.0 language. Some of these features are known from functional languages (for example from LISP or Haskell), some other were developed at Microsoft Research and appeared in the F# language or Cω. I also wanted to show in what ways are these features limited in the C# 3.0. I think that thanks to these limitation, the C# 3.0 is still a simple (or at least not difficult) to understand which is very important for mainstream language, but I find it interesting to know what is possible in other (less limited) languages.</p><ul><li>You can also <a href="http://tomasp.net/articles/csharp3-concepts/csharp3-concepts.pdf">download the article in PDF</a> (404kB)</li><li>The article is also available at <a href="http://www.codeproject.com/useritems/csharp3-concepts.asp">CodeProject.com</a></li></ul>F# metaprogramming and classeshttp://tomasp.net/blog/fsclassmeta.aspxSat, 14 Oct 2006 01:36:10 GMT<p>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 [<a href="#fsclassmeta-links">1</a>] 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.</p><p>To get the quotation data of the expression you can either use <code>&lt;@ .. @&gt;</code> operator or <code>resolveTopDef</code> function. In the first case the code written between the "<code>&lt;@</code>" and "<code>@&gt;</code>" is converted to data during the compilation. The <code>resolveTopDef</code> function allows you to get quotation data of top-level definition (function) from compiled library at runtime (you have to use <code>--quotation-data</code> 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). </p><p>In this article I'll present a simple method that makes it possible to use F# quotations for working with classes as well, however this is rather a prototype that can be used for experimenting and discovering areas where working with classes might be useful, not a fully working solution.</p>F# quotations visualizer - reloaded!http://tomasp.net/blog/quotvis-reloaded.aspxSun, 01 Oct 2006 21:39:06 GMT<div style="float:right;margin-left:10px;"><a href="http://www.tomasp.net/articles/quotvis-reloaded/screen.png" target="_blank"><img style="margin:10px;border-style:none;" src="http://tomasp.net/articles/quotvis-reloaded/screen-sm-sm.jpg" alt="Quotation Visualizer" /></a></div><p>Some time ago, I wrote an article about useful utility called <a href="http://www.tomasp.net/blog/quotvis.aspx">F# quotations visualizer</a>. This utility can be used to show visual representation of F# quotations, that 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 <code>&lt;@@ ... @@&gt;</code> (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 <code>--enable-quotation-data</code> while compiling assembly). </p><p>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:</p><ul><li>Rewritten using active patterns (new F# language feature)</li><li>It is possible to extract quotations from compiled F# assembly (if it contains quotation data)</li><li>Added support for several missing language constructs</li></ul>F# CodeDOM Provider on the CodePlexhttp://tomasp.net/blog/codeplex-fsharp-codedom.aspxWed, 23 Aug 2006 00:13:46 GMT<p>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 <a href="http://www.codeplex.com">CodePlex</a> [<a href="http://www.codeplex.com" target="_blank">^</a>].</p><ul><li>To learn more about the project, visit <a href="http://www.codeplex.com/Wiki/View.aspx?ProjectName=fscodedom">Project Home Page</a></li><li>If you want to send some comments, go to <a href="http://www.codeplex.com/Project/ListForums.aspx?ProjectName=fscodedom">Discussion Board</a></li><li>You can also download latest source code - <a href="http://www.codeplex.com/SourceControl/ListDownloadableCommits.aspx?ProjectName=fscodedom">Latest Check-Ins</a></li><li>And finally, the latest releases can be found at the <a href="http://www.codeplex.com/Release/ProjectReleases.aspx?ProjectName=fscodedom">Releases Page</a></li></ul><p> 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. </p><p> 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! </p>ASP.NET web applications in F#http://tomasp.net/blog/aspnet-fsharp-intro.aspxSun, 13 Aug 2006 21:06:33 GMT<p>CodeDOM (Code Document Object Model) is set of objects (located in <code>System.CodeDom</code> namespace) that can be used for representing logical structure of .NET source code. These classes are used for generating Web service references (using <code>wsdl.exe</code> tool), for generating typed datasets and in many other situations. The most interesting use of CodeDOM classes is in ASP.NET where ASP.NET generates code from <code>aspx</code>/<code>ascx</code> files and compiles this code into web site assemblies (together with the code written in code behind files).</p><p>This means, that you can use any language for developing ASP.NET web sites, as long as you implement CodeDOM provider that generates source code from CodeDOM structure and can compile these source files (this can be simply done by executing command line compiler). I was recently working on CodeProvider for the F# language, and finally it supports everything what is needed by ASP.NET (however it is complete and it doesn't work for example with <code>wsdl.exe</code>). Using this CodeDOM provider you can write ENTIRE web site in F# (including in-line code enclosed in &lt;% ... source code ... %&gt;). I also created project template for F# web site that can be imported to Visual Studio 2005, so you can easilly try writing web pages in F#...</p>LINQ extensions - Simplified keyword searchhttp://tomasp.net/blog/linq-expand-update.aspxFri, 28 Jul 2006 03:57:38 GMT<p>Recently, I came across interesting question at LINQ Forums (Dynamic conditions: How to achieve multiple "OR" conditions with LINQ? [<a href="#updlq">1</a>]). The question is whether LINQ (and especially LINQ to SQL) provides any simple way to return only records that contain one or more of specified keywords in the name. The question looks simple, but it is simple only if you know the number of keywords that you want to look for. In this case you can write following LINQ query:</p><pre><span class="c">// Products that contain "kwd1" or "kwd2" in the name </span><br /><span class="k">var</span> q = <span class="k">from</span> p <span class="k">in</span> db.Products <span class="k">where</span> p.ProductName.Contains("kwd1") || p.ProductName.Contains("kwd2") <span class="k">select</span> p; </pre><p>The problem with previous code is that you can't use it if the list of keywords is dynamically entered by user (and so its length may vary). Of course, if you want to run query on in-memory data, you can get very nice results by writing extension method called <code>ContainsAny</code> that performs test for keyword array, but if you want to be able to translate query to SQL, the situation is a bit complicated.</p>F# quotations visualizerhttp://tomasp.net/blog/quotvis.aspxWed, 21 Jun 2006 02:20:03 GMT<p>I already <a href="http://tomasp.net/blog/fsquotations.aspx">explained</a> what F# quotations are and I explained how you can do some simple manipulations with it. In this article I'd like to present an application that I wrote and that can be helpful when working with quotations. It displays clear graphical representation of given F# quotation (using Windows Forms TreeView control). </p><div style="text-align:center;"><a href="http://tomasp.net/articles/quotvis/qv_apps.gif" target="_blank"><img style="margin:10px;border-style:none;" src="http://tomasp.net/articles/quotvis/qv_apps_sm.gif" alt="Quotation Visualizer" /></a></div>Calling functions in LINQ querieshttp://tomasp.net/blog/linq-expand.aspxSat, 10 Jun 2006 14:26:52 GMT<p>The <a href="http://msdn.microsoft.com/data/ref/linq/">LINQ Project</a> [<a href="http://msdn.microsoft.com/data/ref/linq/" target="_blank">^</a>] is an extension to .NET Framework and most important .NET languages (C# and VB.Net) that extends these languages with query operators and some language features that make it possible to integrate queries in the languages. Thanks to LINQ you can write queries that read data from database (or any other data source). For example, imagine that you want to write set of queries for eshop and you need to perform a price calculation in more queries. The problem with LINQ queries is that you can't simply call a function written in C# that calculates price. The following example is NOT WORKING for this reason:</p><pre><span class="c">// function used in filter</span><br /><span class="k">static</span> <span class="k">decimal</span> CalcPrice(Nwind.Product p) { <span class="k">return</span> p.UnitPrice * 1.19m; }</pre><pre><span class="c">// query that uses MyFunc</span><br /><span class="k">var</span> q = <span class="k">from</span> p <span class="k">in</span> db.Products <span class="k">where</span> CalcPrice(p) &gt; 30m <span class="k">select</span> p </pre><p>I think that this is a big limitation, because when you want to keep some more complex logic in the data access layer you should be able to reuse parts of queries that are similar across more queries. The good thing is that with latest release, LINQ became extensible so it is possible to write a extensions that allow this scenario...</p><ul><li>You can also <a href="http://tomasp.net/articles/linq-expand/linq-expand.pdf">download the article in PDF</a> (80kB)</li></ul>Slides and demos from F# presentationhttp://tomasp.net/blog/fsharp-slides-and-demos.aspxFri, 09 Jun 2006 21:14:39 GMT<p>This semester I attended Advanced .NET Seminar that was led by <a href="http://tmd.havit.cz/">Tomas Matousek</a> [<a href="http://tmd.havit.cz/" target="_blank">^</a>] who is one of the authors of <a href="http://www.php-compiler.net">Phalanger project</a> [<a href="http://www.php-compiler.net" target="_blank">^</a>] (Which is an amazing project by the way. It takes PHP source code and compiles it without any modification to .NET). Seminar was mostly focused on Rotor and .NET internals, so if you want to learn more about these topics you can look at <a href="http://tmd.havit.cz/Teaching/CLR.htm">Advanced .NET programming</a> [<a href="http://tmd.havit.cz/Teaching/CLR.htm" target="_blank">^</a>] slides (by Tomas Matousek).</p><p>I did one presentation at this seminar too. It was about the F# language developed at Microsoft Research. It was just a quick overview of F# features, because F# is very rich topic, so it coveres only the language (functional vs. imperative behavior), F# type system, compilation of F# constructs to .NET and interoperability with .NET (for example how to create windows forms application in F#). At the end, I also mentioned F# meta-programming that allows you to look at F# code as data.</p><ul><li><a href="http://tomasp.net/academic/files/fsharp.pdf">The F# language</a> (67kB) - presentation slides in PDF</li><li><a href="http://tomasp.net/academic/files/fsharp-samples.zip">F# Samples</a> (54kB) - zipped VS 2005 solution with samples</li></ul>F# - Simple quotations transofrationhttp://tomasp.net/blog/fsquotations.aspxSun, 28 May 2006 15:58:20 GMT<p>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.</p>Aho-Corasick string matching in C#http://tomasp.net/blog/ahocorasick.aspxSun, 04 Dec 2005 00:18:30 GMT<p>I implemented this algorithm because I worked on one project where we needed to filter bad language in comments submited by users (You wouldn't believe what anonymous users sometimes write). First I tried simple solution using <code>String.IndexOf</code> and using <code>Regex</code>, but none of these solutions was very suitable for this problem, so I decided to implement Aho-Corasick algorithm which is probabbly the best algorithm for this purpose.</p><p>Article (published here an on CodeProject.com) describes implementation of this algorithm for pattern matching. In simple words this algorithm can be used for searching text for specified keywords. This implementation is usefull when you have a set of keywords and you want to find all occurences in text or check if any of the keywords is present in the text. You should use this algorithm especially if you have large number of keywords that don't change often, because in this case it is much more efficient than other algorithms that can be simply implemented using .NET class library.</p><p>Aho-Corasick search algorithm is very efficient if you want to find large number of keywords in the text, but if you want to search only for a few keywords it is better to use simple method using <code>String.IndexOf</code>. </p>