<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>Sector 0</title>
	<atom:link href="http://sector0.dk/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://sector0.dk</link>
	<description>Random and incoherent thoughts from Franks cubicle</description>
	<pubDate>Sat, 14 Mar 2009 17:56:08 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<item>
		<title>Is IronErlang (or Erlang.NET) a Good Idea?</title>
		<link>http://sector0.dk/?p=104</link>
		<comments>http://sector0.dk/?p=104#comments</comments>
		<pubDate>Sat, 15 Nov 2008 13:54:20 +0000</pubDate>
		<dc:creator>frank</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[Distributed computing]]></category>

		<category><![CDATA[F#]]></category>

		<category><![CDATA[Functional Programming]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[erlang]]></category>

		<guid isPermaLink="false">http://sector0.dk/?p=104</guid>
		<description><![CDATA[I am kind of between projects right now so I am wondering what I should do next. I really like functional programming, and in particular languages like Erlang, Haskell and F#. So I started to wonder if I should make an Erlang compiler for the .NET runtime.
I like Erlang for a number of reasons: It [...]]]></description>
			<content:encoded><![CDATA[<p>I am kind of between projects right now so I am wondering what I should do next. I really like functional programming, and in particular languages like Erlang, Haskell and F#. So I started to wonder if I should make an Erlang compiler for the .NET runtime.</p>
<p>I like Erlang for a number of reasons: It is simple, it is a functional programming language, the language is designed around the actor-model for interprocess (Erlang processes that is) communication, a system written in Erlang is very robust, and creating computing clusters and high-availability servers is almost too easy.</p>
<p>So if I am about to start making an implementation of Erlang for the .NET platform there are a few obstacles and questions that needs to be addressed first:</p>
<p><span id="more-104"></span></p>
<ol>
<li>Should it be pure Erlang on the .NET runtime, or should there be interoperability with the rest of .NET? While the first is easier than the latter, I am a <em>big</em> fan of programmers having several tools available - all of which have their justification in different parts of a software project. Giving programmers more tools will only improve their performance as well as have an impact on the number of errors in software - something that is skyrocketing in these times where software systems grow more and more complex, and development teams become larger and larger. And we <span style="text-decoration: underline;">will</span> see a rise in different technologies in the near future; The DLR (Dynamic Language Runtime) is right around the corner, and F# is becoming more and more mainstream and will be included in the official Microsoft .NET language stack.</li>
<li>In Erlang a process (abstractly speaking the equivalent of a thread) is extremely light weight. As a consequence a system written in Erlang can easily have 10.000 processes running concurrently with no discernible impact on performance. This is not possible with regular .NET threads. I have been experimenting with making such light weight threads and have been successful as a proof-of-concept as well as some running code. So that is no big problem - it just needs some hard work.</li>
<li>The actor model that Erlang uses to communicate between processes is wonderfully simple to use and understand, and the same goes for implementing the framework behind it; I have written a framework for message passing in C# on distributed nodes, and I use the actor model heavily in F# so I understand it thouroughly.</li>
<li>Should a compiler compile directly to IL code? Writing a compiler that outputs IL code is hard work, and the complexity is perhaps too much to handle. But I have also been experimenting with compiling from one (simple) functional language to F# and then using the F# compiler itself to produce IL code. Much easier.</li>
</ol>
<p>So it seems that I have at least some of the obstacles covered. What I need to address is how to bridge Erlang and .NET since the former is strictly functional and the latter focuses on object orientation.</p>
<p>I have also been searching the net for something similar, but I have only found some propositions and small articles about the benefits of having Erlang for the .NET platform. I think I will start this project as an academic exercise and see where it takes me. Maybe I can even find someone who are willing to help on such a project..? If you have any input whatsoever you are welcome to comment below or send me an email.</p>
<p>Is Erlang on the .NET platform desirable and feasible? Are there any Erlang programmers out there with comments or ideas?</p>
]]></content:encoded>
			<wfw:commentRss>http://sector0.dk/?feed=rss2&amp;p=104</wfw:commentRss>
		</item>
		<item>
		<title>Data Parallelism in Functional Programming</title>
		<link>http://sector0.dk/?p=79</link>
		<comments>http://sector0.dk/?p=79#comments</comments>
		<pubDate>Fri, 14 Nov 2008 23:19:39 +0000</pubDate>
		<dc:creator>frank</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[F#]]></category>

		<category><![CDATA[Functional Programming]]></category>

		<category><![CDATA[Parallel computing]]></category>

		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://sector0.dk/?p=79</guid>
		<description><![CDATA[In this small article I will show you how you can use the power of functional programming to write data structures for parallel processing that scales well on any number of cores.
What is one of the primary concerns for programmers right now? Writing high performance code that scales well regardless of whether the software is [...]]]></description>
			<content:encoded><![CDATA[<p>In this small article I will show you how you can use the power of functional programming to write data structures for parallel processing that scales well on any number of cores.</p>
<p>What is one of the primary concerns for programmers right now? Writing high performance code that scales well regardless of whether the software is running on a CPU with one, two or 1000 cores.</p>
<p>Todays software does not scale well, and one of the major obstacles is making it run efficiently on multiple cores without encountering the problems that are bound to arise when programming with multiple threads. When dealing with parallel processing data parallelism is a great tool to accomplish the goal of writing highly scalable software.</p>
<p>In functional programming a list is one of the most fundamental data structures you can find. Furthermore it is also great for - say - iterating, and it is very easy to make parallel. I will show you a few examples written in F# - a functional programming language for the .NET platform with roots in ML and OCaml. I will demonstrate the principles with a simple data type (PList) that can be used in a wide variety of ways for processing data, and which utilizes the number of cores in your computer without you having to worry about it.</p>
<p>Finally I will endulge in a bit of theory for extending this data structure to span multiple machines connected in a computing cluster.</p>
<h4><span id="more-79"></span></h4>
<h3>Constructing the Data Structure</h3>
<p>A list is, as I mentioned earlier, a singly linked list. Each node contains an element e of some generic type &#8216;a, and it also has a pointer to the next node in the list.</p>
<div><img src="/public_files/Data_parallelism/figure1.JPG" alt="A singly linked list" /></div>
<p>In order to parallelize this data structure we need to break it up into two or more bits depending on the number of cores. There is another aspect to consider here and that is whether or not we risk performing some I/O or other stuff on each element that potentially will pause the thread. A rule of thumb says to allocate twice as many threads as there are cores in the CPU(s), but for this example we will just use the number of cores in the machine.</p>
<p>So how do we decide on the scheme on which we base the data structure of the parallel list? Consider this: We have a list of integers between 1 and 10 and we want to construct a new list by mapping the function x*x on each element. In F# it might look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp"><span style="color: #0000FF;">let</span> sqrlist <span style="color: #000000;">=</span> List<span style="color: #000000;">.</span><span style="color: #008080;">map</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">fun</span> x <span style="color: #000000;">-&gt;</span> x<span style="color: #000000;">*</span>x<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#91;</span><span style="color: #800000;">1</span><span style="color: #000000;">..</span><span style="color: #800000;">10</span><span style="color: #000000;">&#93;</span></pre></div></div>

<p>The List.map function iterates through the entire list, applies the (here anonymous) function to each element, and adds the result to the new list.</p>
<p>This is all very well but it is strictly sequential. If we split the list into two separate lists, each containing half the elements of the original list, we can apply List.map to two smaller lists in parallel. But splitting the list is not feasible – the list needs to be partitioned beforehand. And to do this I use a queue to act as the placeholder for each of the sequential sublists. Each time I want to add an element to the parallel list I simply dequeues the first element (which is a sequential list), adds the element to that list, and enqueues the result. This ensures that the list is always balanced, and adding the values 1..10 in a parallel list with two sublist will result in the following:</p>
<p>The first 3 insertions yields the following results</p>
<div><img src="/public_files/Data_parallelism/figure2.JPG" alt="" width="70%" height="70%" /></div>
<p>And continuing with all 10 insertions will yield this:</p>
<div><img src="/public_files/Data_parallelism/figure3.JPG" alt="" width="25%" height="25%" /></div>
<p>So the first order of business is to define the queue. In the .NET generic collections library there is an implementation, but it is purely imperative and I wanted to use a purely functional one. So I implemented one following the template from the book <strong><em>Purely Functional Data Structures</em></strong> by Chris Okasaki.</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp"><span style="color: #0000FF;">type</span> Queue<span style="color: #000000;">&lt;'</span>a<span style="color: #000000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">=</span>
    <span style="color: #0000FF;">let</span> <span style="color: #0000FF;">mutable</span> outlist<span style="color: #000000;">:'</span>a list <span style="color: #000000;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0000FF;">let</span> <span style="color: #0000FF;">mutable</span> inlist<span style="color: #000000;">:'</span>a list <span style="color: #000000;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span>
&nbsp;
    <span style="color: #0000FF;">member</span> q<span style="color: #000000;">.</span><span style="color: #008080;">Enqueue</span> v <span style="color: #000000;">=</span> inlist <span style="color: #000000;">&lt;-</span> v<span style="color: #000000;">::</span>inlist
    <span style="color: #0000FF;">member</span> q<span style="color: #000000;">.</span><span style="color: #008080;">Dequeue</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">=</span>
        <span style="color: #0000FF;">match</span> outlist <span style="color: #0000FF;">with</span>
        <span style="color: #000000;">|</span> h<span style="color: #000000;">::</span>t  <span style="color: #000000;">-&gt;</span>  outlist <span style="color: #000000;">&lt;-</span> t
                    h
        <span style="color: #000000;">|</span> <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span>    <span style="color: #000000;">-&gt;</span>  <span style="color: #0000FF;">match</span> List<span style="color: #000000;">.</span><span style="color: #008080;">rev</span> inlist <span style="color: #0000FF;">with</span>
                    <span style="color: #000000;">|</span> <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span>    <span style="color: #000000;">-&gt;</span> failwith <span style="color: #800000;">&quot;Empty queue&quot;</span>
                    <span style="color: #000000;">|</span> h<span style="color: #000000;">::</span>t  <span style="color: #000000;">-&gt;</span>  outlist <span style="color: #000000;">&lt;-</span> t
                                inlist <span style="color: #000000;">&lt;-</span> <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span>
                                h
    <span style="color: #0000FF;">member</span> q<span style="color: #000000;">.</span><span style="color: #008080;">to_list</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">=</span> List<span style="color: #000000;">.</span><span style="color: #008080;">rev</span> inlist <span style="color: #000000;">|&gt;</span> List<span style="color: #000000;">.</span><span style="color: #008080;">append</span> outlist
    <span style="color: #0000FF;">member</span> q<span style="color: #000000;">.</span><span style="color: #008080;">Clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">=</span>
        outlist <span style="color: #000000;">&lt;-</span> <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span>
        inlist <span style="color: #000000;">&lt;-</span> <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0000FF;">member</span> q<span style="color: #000000;">.</span><span style="color: #008080;">Length</span> <span style="color: #0000FF;">with</span> get<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">=</span> <span style="color: #000000;">&#40;</span>List<span style="color: #000000;">.</span><span style="color: #008080;">length</span> outlist<span style="color: #000000;">&#41;</span><span style="color: #000000;">+</span><span style="color: #000000;">&#40;</span>List<span style="color: #000000;">.</span><span style="color: #008080;">length</span> inlist<span style="color: #000000;">&#41;</span></pre></div></div>

<p>This data structure does not perform quite as well as the imperative one from the .NET library but it has the benefit of being purely functional, enabling you to implement it in your language of choice.</p>
<p>The next line of code is just to create a type alias between a queue of lists of type &#8216;a with the type name &#8216;a plist.</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp"><span style="color: #0000FF;">type</span> <span style="color: #000000;">'</span>a plist <span style="color: #000000;">=</span> Queue<span style="color: #000000;">&lt;</span>’a list<span style="color: #000000;">&gt;</span></pre></div></div>

<p>Next comes the module that holds the interesting functions. In the standard list this module is called List so I will call this module PList. This module contains a few functions that are not part of the original List module, but remember that this is just for playing around and showing you how it works.</p>
<p>The first of these functions is <strong>create_empty</strong> which – like the name implies – creates an empty plist. The signature is</p>
<p><code>create_empty : unit -> 'a plist</code></p>
<p>and the implementation is</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp"><span style="color: #008000; font-style: italic;">/// Create an empty plist</span>
<span style="color: #0000FF;">let</span> create_empty<span style="color: #000000;">&lt;'</span>a<span style="color: #000000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">=</span>
    <span style="color: #0000FF;">let</span> plist <span style="color: #000000;">=</span> <span style="color: #0000FF;">new</span> Queue<span style="color: #000000;">&lt;'</span>a list<span style="color: #000000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#91;</span><span style="color: #800000;">1</span><span style="color: #000000;">..</span><span style="color: #008080;">Environment</span><span style="color: #000000;">.</span><span style="color: #008080;">ProcessorCount</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">|&gt;</span> List<span style="color: #000000;">.</span><span style="color: #008080;">iter</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">fun</span> <span style="color: #0000FF;">_</span> <span style="color: #000000;">-&gt;</span> plist<span style="color: #000000;">.</span><span style="color: #008080;">Enqueue</span> <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>
    plist</pre></div></div>

<p>The next function is <strong>add </strong>which adds a single element to the list. It is similar to the cons (&#8217;::’) operator on regular lists. As described earlier it dequeues the first list in the queue, adds the element to this list and enqueues it in the list again. This is done in order to preserve order as well as keeping the parallel list balanced. The signature is</p>
<p><code>add : 'a plist -> 'a -> 'a plist</code></p>
<p>and the implementation is</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp"><span style="color: #008000; font-style: italic;">/// Create a new list (in order to preserve immutable state) by adding</span>
<span style="color: #008000; font-style: italic;">/// an element to the original. Rotate the sequential lists in order</span>
<span style="color: #008000; font-style: italic;">/// to balance the data.</span>
<span style="color: #0000FF;">let</span> add <span style="color: #000000;">&#40;</span>plist<span style="color: #000000;">:'</span>a plist<span style="color: #000000;">&#41;</span> e <span style="color: #000000;">=</span>
    <span style="color: #0000FF;">let</span> newList <span style="color: #000000;">=</span> <span style="color: #0000FF;">new</span> Queue<span style="color: #000000;">&lt;'</span>a list<span style="color: #000000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    List<span style="color: #000000;">.</span><span style="color: #008080;">iter</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">fun</span> lst <span style="color: #000000;">-&gt;</span> plist<span style="color: #000000;">.</span><span style="color: #008080;">Dequeue</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">|&gt;</span> newList<span style="color: #000000;">.</span><span style="color: #008080;">Enqueue</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#40;</span>plist<span style="color: #000000;">.</span><span style="color: #008080;">to_list</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0000FF;">let</span> lst <span style="color: #000000;">=</span> newList<span style="color: #000000;">.</span><span style="color: #008080;">Dequeue</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    newList<span style="color: #000000;">.</span><span style="color: #008080;">Enqueue</span> <span style="color: #000000;">&#40;</span>e<span style="color: #000000;">::</span>lst<span style="color: #000000;">&#41;</span>
    newList</pre></div></div>

<p>Then there is the function <strong>from_list</strong> which serves no other purpose than providing you with a single function to add a whole list of elements in one go. The signature is</p>
<p><code>from_list : 'a list -> 'a plist</code></p>
<p>and the implementation looks like this</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp"><span style="color: #008000; font-style: italic;">/// Create a new plist from the sequential list lst</span>
<span style="color: #0000FF;">let</span> from_list <span style="color: #000000;">&#40;</span>lst<span style="color: #000000;">:'</span>a list<span style="color: #000000;">&#41;</span> <span style="color: #000000;">=</span>
    <span style="color: #0000FF;">let</span> newList <span style="color: #000000;">=</span> create_empty<span style="color: #000000;">&lt;'</span>a<span style="color: #000000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    List<span style="color: #000000;">.</span><span style="color: #008080;">iter</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">fun</span> e <span style="color: #000000;">-&gt;</span> newList<span style="color: #000000;">.</span><span style="color: #008080;">Enqueue</span> <span style="color: #000000;">&#40;</span>e<span style="color: #000000;">::</span>newList<span style="color: #000000;">.</span><span style="color: #008080;">Dequeue</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> lst
    newList</pre></div></div>

<p>Finally there are a few interesting functions that do exist in the original List module. The first one is <strong>map</strong>. As you may – or may not – know this function takes a list of elements as well as a function and returns a new list. The elements of the new list is the result of applying the function to each element of the first list and it has the signature</p>
<p><code>map : ('a -> 'b) -> 'a plist -> 'b plist</code></p>
<p>What this parallel version does is simply to use a thread for each sequential list in the queue, here using the async monad from the F# library. The async monad does not spawn a thread per se, but puts a job on the .NET thread pool. The thread pool hold a number of idle threads and you can use them for anything asynchronous. This is just so the CLR and the operating system won’t have to go through the hard work of creating and destroying threads all the time.</p>
<p>Lets get back on track again. Each thread (or asynchronous task) uses the standard List.map, and the results from all the threads are compiled into a new parallel list by enqueueing the new sequential lists in a new queue. The implementation is</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp"><span style="color: #008000; font-style: italic;">/// Asynchronously map the function to all elements and return a new</span>
<span style="color: #008000; font-style: italic;">/// parallel list with the result.</span>
<span style="color: #0000FF;">let</span> map f <span style="color: #000000;">&#40;</span>plist<span style="color: #000000;">:'</span>a plist<span style="color: #000000;">&#41;</span> <span style="color: #000000;">=</span>
    <span style="color: #0000FF;">let</span> map_async f lst <span style="color: #000000;">=</span> async <span style="color: #000000;">&#123;</span>  <span style="color: #0000FF;">return</span> List<span style="color: #000000;">.</span><span style="color: #008080;">map</span> f lst <span style="color: #000000;">&#125;</span>
    <span style="color: #0000FF;">let</span> tasks <span style="color: #000000;">=</span> plist<span style="color: #000000;">.</span><span style="color: #008080;">to_list</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">|&gt;</span> List<span style="color: #000000;">.</span><span style="color: #008080;">map</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">fun</span> lst <span style="color: #000000;">-&gt;</span> map_async f lst<span style="color: #000000;">&#41;</span>
    <span style="color: #0000FF;">let</span> newList <span style="color: #000000;">=</span> <span style="color: #0000FF;">new</span> Queue<span style="color: #000000;">&lt;'</span>a list<span style="color: #000000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    Async<span style="color: #000000;">.</span><span style="color: #008080;">Run</span><span style="color: #000000;">&#40;</span>Async<span style="color: #000000;">.</span><span style="color: #008080;">Parallel</span> tasks<span style="color: #000000;">&#41;</span> <span style="color: #000000;">|&gt;</span> Array<span style="color: #000000;">.</span><span style="color: #008080;">iter</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">fun</span> seqList <span style="color: #000000;">-&gt;</span> newList<span style="color: #000000;">.</span><span style="color: #008080;">Enqueue</span> seqList<span style="color: #000000;">&#41;</span>
    newList</pre></div></div>

<p>The next function is <strong>length </strong>which just returns the number of elements in the parallel list. It has the signature</p>
<p><code>length : 'a plist -> int</code></p>
<p>and is implemented like this</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp"><span style="color: #008000; font-style: italic;">/// Calculate the length of the parallel list by adding the lengths of the sequential lists</span>
<span style="color: #0000FF;">let</span> length <span style="color: #000000;">&#40;</span>plist<span style="color: #000000;">:'</span>a plist<span style="color: #000000;">&#41;</span> <span style="color: #000000;">=</span>
    plist<span style="color: #000000;">.</span><span style="color: #008080;">to_list</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">|&gt;</span> List<span style="color: #000000;">.</span><span style="color: #008080;">map</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">fun</span> lst <span style="color: #000000;">-&gt;</span> List<span style="color: #000000;">.</span><span style="color: #008080;">length</span> lst<span style="color: #000000;">&#41;</span> <span style="color: #000000;">|&gt;</span> List<span style="color: #000000;">.</span><span style="color: #008080;">sum</span></pre></div></div>

<p>The last function is <strong>iter</strong>. This does the same as map except that it just iterates through the list without creating a new one. This is the equivalent of running an imperative for-loop over the list [n..m], and has the signature</p>
<p><code>iter : ('a -> unit) -> 'a plist -> unit</code></p>
<p>and the implementation of iter is</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp"><span style="color: #008000; font-style: italic;">/// Iterate through all elements by doing it asynchronously for all</span>
<span style="color: #008000; font-style: italic;">/// sequential lists.</span>
<span style="color: #0000FF;">let</span> iter f <span style="color: #000000;">&#40;</span>plist<span style="color: #000000;">:'</span>a plist<span style="color: #000000;">&#41;</span> <span style="color: #000000;">=</span>
    <span style="color: #0000FF;">let</span> iter_async f lst <span style="color: #000000;">=</span> async <span style="color: #000000;">&#123;</span>  List<span style="color: #000000;">.</span><span style="color: #008080;">iter</span> f lst <span style="color: #000000;">&#125;</span>
    <span style="color: #0000FF;">let</span> tasks <span style="color: #000000;">=</span> plist<span style="color: #000000;">.</span><span style="color: #008080;">to_list</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">|&gt;</span> List<span style="color: #000000;">.</span><span style="color: #008080;">map</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">fun</span> lst <span style="color: #000000;">-&gt;</span> iter_async f lst<span style="color: #000000;">&#41;</span>
    Async<span style="color: #000000;">.</span><span style="color: #008080;">Run</span><span style="color: #000000;">&#40;</span>Async<span style="color: #000000;">.</span><span style="color: #008080;">Parallel</span> tasks<span style="color: #000000;">&#41;</span> <span style="color: #000000;">|&gt;</span> ignore</pre></div></div>

<h3>Load Balancing</h3>
<p>I wrote earlier that there might be a problem if you use this approach for processing asynchronous tasks. How can that be? Well, suppose you have 2 cores in your CPU. You create a parallel list using two sequential lists. This parallel list contains URL’s that you want to fetch with the map function, in effect creating a new list of strings with the HTML in. So when you apply the function fetch_url to PList.map the PList will create two asynchronous tasks – one for each sequential list in the parallel list. What happens if one of the threads tries to fetch an URL from a really slow server? The entire thread pauses until all the data is fetched, leaving just one other thread to continue while the first one waits. So here you might want to spawn, say, 2 asynchronous tasks for each core. This will provide the CPU with more threads to work with in case anyone sits idle.</p>
<p>On the other hand, if the list consists of something that does not make the threads wait, for example a list of strings that needs to be parsed, too many threads will only slow things down since the CPU now has to switch between too many threads.</p>
<p>I invite you to experiment with this. You can provide a &#8220;loadfactor&#8221; to the create_empty function.</p>
<h2>Extending PList to Span Multiple Machines</h2>
<p>If we introduce the notion of &#8220;location&#8221; to the code, we can begin to experiment in another way. Consider this: A normal computer can be viewed as depicted below</p>
<div><img src="/public_files/Data_parallelism/figure4.JPG" alt="" width="50%" height="50%" /></div>
<p>A computer can have a number of CPU’s that in turn can have a number of cores. A computing cluster can be viewed similarly by extending the graph like this</p>
<div><img src="/public_files/Data_parallelism/figure5.JPG" alt="" width="70%" height="70%" /></div>
<p>where the individual computers are connected by some sort of network. What if we switch the nodes above out with a &#8220;location&#8221; object and use such a graph when we create and use our parallel data structures? We would use such a location object when creating our data structures; I could use a &#8220;local CPU location&#8221; object to create a parallel list running in local process space with the same number of sequential lists inside as there are cores in this particular CPU. Or I could use the top node of my location graph – the &#8220;cluster location&#8221; object to create a parallel list that spans the entire cluster of computers, in effect creating a distributed parallel data structure for my processing and storing of data. This is just something I am experimenting with.</p>
]]></content:encoded>
			<wfw:commentRss>http://sector0.dk/?feed=rss2&amp;p=79</wfw:commentRss>
		</item>
		<item>
		<title>The Programmers Dilemma</title>
		<link>http://sector0.dk/?p=63</link>
		<comments>http://sector0.dk/?p=63#comments</comments>
		<pubDate>Sun, 09 Nov 2008 12:32:32 +0000</pubDate>
		<dc:creator>frank</dc:creator>
		
		<category><![CDATA[Architecture]]></category>

		<category><![CDATA[Design]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Test]]></category>

		<category><![CDATA[gobbledygook]]></category>

		<guid isPermaLink="false">http://sector0.dk/?p=63</guid>
		<description><![CDATA[So what is all this talking about “automatic testing”? I know my code work so I don’t have to write some “test case” just to verify that; A complete waste of time!
Just kidding. No one in their right mind would say that they write bugfree code. One of the tools to help weed out the [...]]]></description>
			<content:encoded><![CDATA[<p>So what is all this talking about “automatic testing”? I <strong><em>know</em></strong> my code work so I don’t have to write some “test case” just to verify that; A complete waste of time!</p>
<p>Just kidding. No one in their right mind would say that they write bugfree code. One of the tools to help weed out the bugs before the code hits production is automatic testing, more specifically unit testing. This helps fix some problems but it also presents some of its own.</p>
<h4>The Dilemma</h4>
<p>The dilemma I am constantly finding myself in is this: I want to make beautiful code and design, but I also want to make sure it is functioning as it should. Now, there are a number of more or less subtle ways to <span id="more-63"></span>make your code less error prone and one is by making it as succint as possible, and writing it according to a specific design. That in itself is what we as programmers should strive for, because succint code that is written according to an easy-to-understand specification or  design is also easier for <em>other</em> programmers to read, update and bugfix.</p>
<p>Another way to reduce the number of errors in your code is of course by using unit testing. This, however, seriously hampers good design – in fact it works directly against good design; Suppose I have made a class with a number of methods. All methods except one are private or protected since they only serve to partition the task of the single public method. I don’t want to write a unit test (some would call it an integration test) for this single method since it would 1) cause side effects in my database/file system, and 2) is so complex that writing a serious test would requiere days of work because of the data setup required by the method as well as the sheer number of possibilities for testing inside and outside the boundaries of the valid inputs.</p>
<p>Okay, but what if I just test some or all of the private methods that handles some specific subproblem? Well, I would have to make them public in order to test them. This goes directly against my design, as well as the principles of encapsulation: the unnecessarily public methods only tends to make the interface of the class more confusing, especially for any programmer who later looks at – or uses – the class.</p>
<p>Another example: I am writing a method that takes a number of inputs, do some magic to these inputs to produce new data and writes those to disk/database/network/… Clearly the persistency is of no specific concern here, so in order to test this I have to split the method into two or more and test those separately. And if I&#8217;m really &#8220;lucky&#8221; I have to use dependency injection. All this only adds more code and serves no other purpose – other than enabling me to test the code – than making the code harder to read as well as partitioning code that conceptually and by function belongs together as a single unit.</p>
<p>Then there is the all-pervasive question of <em>how much</em>. How much should I test? Some say that we need to test as much as possible. That presents two distinct problems:</p>
<ol>
<li>Should I really test everything? I write a lot of code that is so simple that writing a test case for it is simply a waste of time. And adding over 200% more (test) code is simply not justifiable to anyone who have an economic interest in the project.  Besides, this just gives you a false sense of security as well as multiplying the number of errors in your test code.</li>
<li>How many inputs should I test the mthod against? All possible? Well, some accepts an infinite number of input combinations. So how will I choose the right inputs, both valid, invalid and borderline? Again, this only serves to give you a false sense of security.</li>
</ol>
<p>In my daily work I write unit tests, and I even use it occasionally in my private projects. But writing succint code that is structured logically is an even better way to write error free code as well as maintainable code. I really do not like having to make designs that resemble something I wrote in high school, and having my creativity hampered by… a tool. That is just <strong><em>wrong</em></strong>, and a <strong><em>lot </em></strong>of ugly code has been written on that account – code that is a nightmare to maintain, use and develop.</p>
<p>And what really makes me wonder is this: often I have heard the argument &#8220;writing a test forces me to think about how I would use this-and-that component&#8221;. Well, if you hadn&#8217;t thought about it before you started writing it you shouldn&#8217;t start at all. Some people even like to write the test before they write the functionality. I&#8217;m not one of those guys but still I have to write tests. Unit testing is often utilized in projects that uses so called &#8220;agile&#8221; development, but in my experience not everyone gets more agile that way.</p>
<p>Yeah yeah, occasionally I find bugs that I didn’t think about when writing a test. But more often than not I spend way too much time correcting an old test case to fit the new requirements than I do actually implementing those requirements.<br />
I state that the way we write tests today are inferior and an almost complete waste of time – a hell of a lot of bugs are found by the users regardless, and my time is better spent writing correct, logical, succint – and by definition – maintainable code than writing all those tests.</p>
<p>I write tests, but I don’t have to like it.</p>
]]></content:encoded>
			<wfw:commentRss>http://sector0.dk/?feed=rss2&amp;p=63</wfw:commentRss>
		</item>
		<item>
		<title>F# at JAOO 2008 in Aarhus, Denmark</title>
		<link>http://sector0.dk/?p=55</link>
		<comments>http://sector0.dk/?p=55#comments</comments>
		<pubDate>Sat, 09 Aug 2008 12:19:42 +0000</pubDate>
		<dc:creator>frank</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[F#]]></category>

		<category><![CDATA[Functional Programming]]></category>

		<category><![CDATA[JAOO]]></category>

		<guid isPermaLink="false">http://sector0.dk/?p=55</guid>
		<description><![CDATA[For a while I have looked at the schedule for the JAOO conference in Aarhus, Denmark this year, and thought: What a pity there is so little about functional programming. I mean, functional programming is really moving into the mainstream developer community these years as yet another (and very powerful) tool in our toolboxes. But [...]]]></description>
			<content:encoded><![CDATA[<p>For a while I have looked at the schedule for the <a href="http://jaoo.dk/conference/" target="_blank">JAOO conference</a> in Aarhus, Denmark this year, and thought: What a pity there is so little about functional programming. I mean, functional programming is really moving into the mainstream developer community these years as yet another (and very powerful) tool in our toolboxes. But that is not reflected in the schedule for JAOO, unfortunately.</p>
<p>There was one presentation of <a href="http://en.wikipedia.org/wiki/Scheme_(programming_language)" target="_blank">Scheme</a> but the speaker (and I simply can&#8217;t remember his name, sorry) unfortunately cancelled. <a href="http://en.wikipedia.org/wiki/Fortress_(programming_language)" target="_blank">Fortress</a> has been put on the schedule and I managed to get <a href="http://strangelights.com/" target="_blank">Robert Pickering</a> to come and talk about F#. I also talked to <a href="http://blogs.msdn.com/chrsmith/" target="_blank">Chris Smith</a> and I am currently trying to persuade the conference organizers to put him on the schedule too. It is a bit late in the process but I am doing my very best!</p>
<p>So pack your tooth brushes and laptops and head for the nearest airport or train station. I look forward to meeting you at JAOO!</p>
]]></content:encoded>
			<wfw:commentRss>http://sector0.dk/?feed=rss2&amp;p=55</wfw:commentRss>
		</item>
		<item>
		<title>Using NUnit with F# code</title>
		<link>http://sector0.dk/?p=33</link>
		<comments>http://sector0.dk/?p=33#comments</comments>
		<pubDate>Sun, 22 Jun 2008 19:58:40 +0000</pubDate>
		<dc:creator>frank</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[F#]]></category>

		<category><![CDATA[Functional Programming]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Test]]></category>

		<category><![CDATA[nunit]]></category>

		<guid isPermaLink="false">http://sector0.dk/?p=33</guid>
		<description><![CDATA[Switching to another programming language is difficult. Especially if you are switching from an imperative one to a functional one.  There are a number of reasons why you would think that switching is bad, and when considering F# I dare say that none of those reasons are correct. The language compiles to IL (Intermediate [...]]]></description>
			<content:encoded><![CDATA[<p>Switching to another programming language is difficult. Especially if you are switching from an imperative one to a functional one.  There are a number of reasons why you would think that switching is bad, and when considering F# I dare say that none of those reasons are correct. The language compiles to IL (Intermediate Language) code that runs on Microsofts tried and tested .NET runtime (or Mono’s ditto). You have access to all the same classes in the .NET hierarchy that you have from any other .NET capable language. You can write web applications, WinForms applications or console applications in F#. You can connect to any kind of database that you can connect to from any other .NET language. You can use your existing C# og VB.NET code in your F# applications, or you can write F# code and use it in your existing applications. You have the opportunity to write more concise and maintainable code in a language that has much higher performance than any other functional language I know – with the possible exception of Clean, but that is still debatable, and you can mix functional programming with object oriented ditto at your leasure.</p>
<p>But what about testing? In these TDD (Test Driven Development) oriented days being able to properly test your code with automated unit- and integration tests is paramount. And if you have to use another (or perhaps write your) test framework than the one you are already using and familiar with, that might be an insuperable problem. Fortunately you can easily use NUnit with F# and I will show you how.<span id="more-33"></span></p>
<h3>Testing your F# code from C#</h3>
<p>The most obvious way to go about testing F# code for a new F# programmer is writing your tests in C# (if you are a C# programmer. Let’s just assume that). This is okay since you are used to writing tests in this language, and it is no different from writing tests in one project that tests classes written in C# in another project. Let us start by defining an easily testable class in F#:</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp"><span style="color: #000000;">#</span>light
<span style="color: #0000FF;">namespace</span> TestableLib
&nbsp;
<span style="color: #0000FF;">type</span> TestableClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">=</span>
    <span style="color: #0000FF;">member</span> tc<span style="color: #000000;">.</span><span style="color: #008080;">DoubleNumber</span> n <span style="color: #000000;">=</span> <span style="color: #800000;">2</span><span style="color: #000000;">*</span>n
    <span style="color: #0000FF;">member</span> tc<span style="color: #000000;">.</span><span style="color: #008080;">SquareNumber</span> n <span style="color: #000000;">=</span> n<span style="color: #000000;">*</span>n</pre></div></div>

<p>A typical test fixture written in C# for this class looks like the following piece of code. It is a bit overdesigned – using a test fixture setup in this test is a bit too much, but bear with me for a while.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0000FF;">using</span> <span style="color: #008000;">NUnit.Framework</span><span style="color: #000000;">;</span>
<span style="color: #0000FF;">using</span> <span style="color: #008000;">TestableLib</span><span style="color: #000000;">;</span>
&nbsp;
<span style="color: #0000FF;">namespace</span> TestApplication
<span style="color: #000000;">&#123;</span>
    <span style="color: #000000;">&#91;</span>TestFixture<span style="color: #000000;">&#93;</span>
    <span style="color: #0000FF;">public</span> <span style="color: #0000FF;">class</span> TestableClassTest
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0000FF;">private</span> TestableClass _testableClass <span style="color: #000000;">=</span> null<span style="color: #000000;">;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>TestFixtureSetUp<span style="color: #000000;">&#93;</span>
        <span style="color: #0000FF;">public</span> <span style="color: #0000FF;">void</span> TestFixtureSetup<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> _testableClass <span style="color: #000000;">=</span> <span style="color: #0000FF;">new</span> TestableClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
        <span style="color: #0000FF;">public</span> <span style="color: #0000FF;">void</span> TestDoubleNumber<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">2</span>, _testableClass.<span style="color: #0000FF;">DoubleNumber</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">8</span>, _testableClass.<span style="color: #0000FF;">DoubleNumber</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">4</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">512</span>, _testableClass.<span style="color: #0000FF;">DoubleNumber</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">256</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
        <span style="color: #0000FF;">public</span> <span style="color: #0000FF;">void</span> TestSquareNumber<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">1</span>, _testableClass.<span style="color: #0000FF;">SquareNumber</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">16</span>, _testableClass.<span style="color: #0000FF;">SquareNumber</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">4</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">10000</span>, _testableClass.<span style="color: #0000FF;">SquareNumber</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">100</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This obviously works for methods defined in modules instead of classes. This is F#-modules, not .NET modules. A module in F# is resolved as a class in C#, and all methods defined in the module file are static methods on this class.</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp"><span style="color: #000000;">#</span>light
<span style="color: #0000FF;">module</span> TestableModule
&nbsp;
<span style="color: #0000FF;">let</span> doubleNumber n <span style="color: #000000;">=</span> <span style="color: #800000;">2</span><span style="color: #000000;">*</span>n
<span style="color: #0000FF;">let</span> squareNumber n <span style="color: #000000;">=</span> n<span style="color: #000000;">*</span>n</pre></div></div>

<p>A couple of test methods could look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
<span style="color: #0000FF;">public</span> <span style="color: #0000FF;">void</span> TestModuleDoubleNumber<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">2</span>, TestableModule.<span style="color: #0000FF;">doubleNumber</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">8</span>, TestableModule.<span style="color: #0000FF;">doubleNumber</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">4</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">512</span>, TestableModule.<span style="color: #0000FF;">doubleNumber</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">256</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
<span style="color: #0000FF;">public</span> <span style="color: #0000FF;">void</span> TestModuleSquareNumber<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">1</span>, TestableModule.<span style="color: #0000FF;">squareNumber</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">16</span>, TestableModule.<span style="color: #0000FF;">squareNumber</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">4</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">10000</span>, TestableModule.<span style="color: #0000FF;">squareNumber</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">100</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h3>Writing test fixtures in F#</h3>
<p>Whether you are testing a class written in C#, VB.NET, F# or any other .NET language you can write the tests in F# too. This is because you can annotate type definitions and members with attributes as you can with any other language. You will of course have to reference the “nunit.framework.dll&#8221; file where the attributes we use are defined.<br />
Test fixtures are classes too so we define a new type with a default constructor, and annotate the class and the methods with the attributes defined in the NUnit.Framework namespace:</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp"><span style="color: #000000;">#</span>light
<span style="color: #0000FF;">namespace</span> TestableLib
<span style="color: #0000FF;">open</span> NUnit<span style="color: #000000;">.</span><span style="color: #008080;">Framework</span>
&nbsp;
<span style="color: #0000FF;">type</span> TestableClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">=</span>
    <span style="color: #0000FF;">member</span> tc<span style="color: #000000;">.</span><span style="color: #008080;">DoubleNumber</span> n <span style="color: #000000;">=</span> <span style="color: #800000;">2</span><span style="color: #000000;">*</span>n
    <span style="color: #0000FF;">member</span> tc<span style="color: #000000;">.</span><span style="color: #008080;">SquareNumber</span> n <span style="color: #000000;">=</span> n<span style="color: #000000;">*</span>n
&nbsp;
<span style="color: #000000;">&#91;</span><span style="color: #000000;">&lt;</span>TestFixture<span style="color: #000000;">&gt;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0000FF;">type</span> TestClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">=</span>
    <span style="color: #0000FF;">let</span> <span style="color: #0000FF;">mutable</span> testableClass <span style="color: #000000;">=</span> TestableClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
    <span style="color: #008000; font-style: italic;">/// This is not necessary since the mutable testableClass is already set</span>
    <span style="color: #008000; font-style: italic;">/// but is just for showing how to define a test fixture setup method.</span>
    <span style="color: #000000;">&#91;</span><span style="color: #000000;">&lt;</span>TestFixtureSetUp<span style="color: #000000;">&gt;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0000FF;">member</span> tc<span style="color: #000000;">.</span><span style="color: #008080;">TestFixtureSetUp</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">=</span> testableClass <span style="color: #000000;">&lt;-</span> TestableClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
    <span style="color: #000000;">&#91;</span><span style="color: #000000;">&lt;</span>Test<span style="color: #000000;">&gt;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0000FF;">member</span> tc<span style="color: #000000;">.</span><span style="color: #008080;">TestDoubleNumber</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">=</span>
        Assert<span style="color: #000000;">.</span><span style="color: #008080;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">2</span>, testableClass<span style="color: #000000;">.</span><span style="color: #008080;">DoubleNumber</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        Assert<span style="color: #000000;">.</span><span style="color: #008080;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">8</span>, testableClass<span style="color: #000000;">.</span><span style="color: #008080;">DoubleNumber</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">4</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        Assert<span style="color: #000000;">.</span><span style="color: #008080;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">512</span>, testableClass<span style="color: #000000;">.</span><span style="color: #008080;">DoubleNumber</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">256</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
&nbsp;
    <span style="color: #000000;">&#91;</span><span style="color: #000000;">&lt;</span>Test<span style="color: #000000;">&gt;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0000FF;">member</span> tc<span style="color: #000000;">.</span><span style="color: #008080;">TestSquareNumber</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">=</span>
        Assert<span style="color: #000000;">.</span><span style="color: #008080;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">1</span>, testableClass<span style="color: #000000;">.</span><span style="color: #008080;">SquareNumber</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
        Assert<span style="color: #000000;">.</span><span style="color: #008080;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">16</span>, testableClass<span style="color: #000000;">.</span><span style="color: #008080;">SquareNumber</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">4</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
        Assert<span style="color: #000000;">.</span><span style="color: #008080;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">10000</span>, testableClass<span style="color: #000000;">.</span><span style="color: #008080;">SquareNumber</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">100</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>The output from the F# project can then be opened in the NUnit runner just like any other assembly with test fixtures defined.</p>
<h3>Note to Visual Studio users</h3>
<p>Since F# is still not part of the official .NET language stack (I am currently using version 1.9.4) there are a few minor(!) hurdles to overcome. If you normally use Visual Studio you are used to be able to add references to the projects in the IDE. This is not possible with F# project since the Visual Studio integration is not functioning optimally – or at least as good as other supported languages. But you can add a reference manually by selecting the property page of the project and adding a reference with the “-R&#8221; option. I have used ‘-R &#8220;c:\Program Files\NUnit 2.4.7\bin\nunit.framework.dll&#8221;’.</p>
]]></content:encoded>
			<wfw:commentRss>http://sector0.dk/?feed=rss2&amp;p=33</wfw:commentRss>
		</item>
		<item>
		<title>Huffman encoding in F#</title>
		<link>http://sector0.dk/?p=29</link>
		<comments>http://sector0.dk/?p=29#comments</comments>
		<pubDate>Sun, 22 Jun 2008 11:24:27 +0000</pubDate>
		<dc:creator>frank</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[F#]]></category>

		<category><![CDATA[Functional Programming]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[compression]]></category>

		<category><![CDATA[example]]></category>

		<category><![CDATA[Huffman]]></category>

		<guid isPermaLink="false">http://sector0.dk/?p=29</guid>
		<description><![CDATA[For a while I have been toying with F# - a strict, functional programming language - primarily to get to know the language. I have written a few utility programs and I am working on the problems defined on the Project Euler site. These mathematical problems are perfect for being solved in a functional programming [...]]]></description>
			<content:encoded><![CDATA[<p>For a while I have been toying with F# - a strict, functional programming language - primarily to get to know the language. I have written a few utility programs and I am working on the problems defined on the <a href="http://projecteuler.net" target="_blank">Project Euler site</a>. These mathematical problems are perfect for being solved in a functional programming language and it is a great way to work with a new language as well as getting a brush-up on your mathematics.</p>
<p>I have made an implementation of the Huffman compression algorithm in F# to showcase a few of the important things that make functional programming so great to some classes of problems.</p>
<p><img src="/public_files/images/Huffman_tree.png" alt="Huffman tree generated from the text 'this is an example of a huffman tree'. Source:wikipedia.org" width="384" height="251" /></p>
<p><small>Huffman tree generated from the text &#8216;this is an example of a huffman tree&#8217;.</small><small> Source:<a href="http://wikipedia.org" target="_blank">wikipedia.org</a></small><br />
<span id="more-29"></span><br />
I will not go into detail about the Huffman algorithm itself. The following quote is from <a href="http://wikipedia.org" target="_blank">wikipedia.org</a> about Huffman coding.</p>
<blockquote><p>Huffman coding uses a specific method for choosing the representation for each symbol, resulting in a <span class="mw-redirect">prefix-free code</span> (sometimes called &#8220;prefix codes&#8221;) (that is, the bit string representing some particular symbol is never a prefix of the bit string representing any other symbol) that expresses the most common characters using shorter strings of bits than are used for less common source symbols. Huffman was able to design the most efficient compression method of this type: no other mapping of individual source symbols to unique strings of bits will produce a smaller average output size when the actual symbol frequencies agree with those used to create the code. A method was later found to do this in linear time if input probabilities (also known as <em>weights</em>) are sorted.</p></blockquote>
<h3>The code</h3>
<p>The following code is an implementation of the Huffman algorithm in F#. The program does not encode a string into a compressed bit array, but will use the Huffman algorithm to count how many bits there will be in a compressed string. It is easy to see what needs to be altered in order for the algorithm to actually output a compressed array.</p>
<p>Furthermore the code cannot decode a compressed array. This is because I am just showcasing a few topics. It is easy to write a method that uses the Huffman tree to decode an array of bits into the original input string.</p>
<p>Finally the representation of the Huffman codes is not considered when calculating the size of the compressed array. In an application that actually writes the compressed data to a file or a stream, the huffman codes needs to be stored too in order for the decoder to properly decode the data.</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp"><span style="color: #000000;">#</span>light
<span style="color: #0000FF;">module</span> FsHuffmanEncoder
&nbsp;
<span style="color: #008000; font-style: italic;">// Define a node type using a discriminated union</span>
<span style="color: #0000FF;">type</span> Node <span style="color: #000000;">=</span>
    <span style="color: #000000;">|</span> InternalNode <span style="color: #0000FF;">of</span> int <span style="color: #000000;">*</span> Node <span style="color: #000000;">*</span> Node <span style="color: #008000; font-style: italic;">// an internal node with a weight and two subnodes</span>
    <span style="color: #000000;">|</span> LeafNode <span style="color: #0000FF;">of</span> int <span style="color: #000000;">*</span> byte <span style="color: #008000; font-style: italic;">// a leaf with a weight and a symbol</span>
&nbsp;
<span style="color: #008000; font-style: italic;">/// Get the weight of a node (how many occurencies are in the input file)</span>
<span style="color: #0000FF;">let</span> weight node <span style="color: #000000;">=</span>
    <span style="color: #0000FF;">match</span> node <span style="color: #0000FF;">with</span>
    <span style="color: #000000;">|</span> InternalNode<span style="color: #000000;">&#40;</span>w,<span style="color: #0000FF;">_</span>,<span style="color: #0000FF;">_</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">-&gt;</span> w
    <span style="color: #000000;">|</span> LeafNode<span style="color: #000000;">&#40;</span>w,<span style="color: #0000FF;">_</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">-&gt;</span> w
&nbsp;
<span style="color: #008000; font-style: italic;">/// Creates the initial list of leaf nodes</span>
<span style="color: #0000FF;">let</span> createNodes inputValues <span style="color: #000000;">=</span>
    <span style="color: #0000FF;">let</span> getCounts <span style="color: #000000;">&#40;</span>leafNodes<span style="color: #000000;">:</span><span style="color: #000000;">&#40;</span>int<span style="color: #000000;">*</span>byte<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">=</span>
        inputValues <span style="color: #000000;">|&gt;</span> Array<span style="color: #000000;">.</span><span style="color: #008080;">iter</span>
            <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">fun</span> b <span style="color: #000000;">-&gt;</span>   <span style="color: #0000FF;">let</span> <span style="color: #000000;">&#40;</span>w,v<span style="color: #000000;">&#41;</span> <span style="color: #000000;">=</span> leafNodes<span style="color: #000000;">.</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#40;</span>int<span style="color: #000000;">&#41;</span>b<span style="color: #000000;">&#93;</span>
                        leafNodes<span style="color: #000000;">.</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#40;</span>int<span style="color: #000000;">&#41;</span>b<span style="color: #000000;">&#93;</span> <span style="color: #000000;">&lt;-</span> <span style="color: #000000;">&#40;</span>w<span style="color: #800000;">+1</span>,v<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        leafNodes
    <span style="color: #000000;">&#91;</span><span style="color: #000000;">|</span>for b <span style="color: #0000FF;">in</span> 0uy<span style="color: #000000;">..</span>255uy <span style="color: #000000;">-&gt;</span> <span style="color: #000000;">&#40;</span><span style="color: #800000;">0</span>,b<span style="color: #000000;">&#41;</span><span style="color: #000000;">|</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">|&gt;</span> getCounts
    <span style="color: #000000;">|&gt;</span> List<span style="color: #000000;">.</span><span style="color: #008080;">of_array</span>
    <span style="color: #000000;">|&gt;</span> List<span style="color: #000000;">.</span><span style="color: #008080;">map</span> LeafNode
&nbsp;
<span style="color: #008000; font-style: italic;">/// Create a Huffman tree using the initial list of leaf nodes as basis</span>
<span style="color: #0000FF;">let</span> <span style="color: #0000FF;">rec</span> createHuffmanTree nodes <span style="color: #000000;">=</span>
    <span style="color: #0000FF;">match</span> nodes <span style="color: #000000;">|&gt;</span> List<span style="color: #000000;">.</span><span style="color: #008080;">sort</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">fun</span> node1 node2 <span style="color: #000000;">-&gt;</span> compare <span style="color: #000000;">&#40;</span>weight node1<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#40;</span>weight node2<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #0000FF;">with</span>
    <span style="color: #000000;">|</span> first <span style="color: #000000;">::</span> second <span style="color: #000000;">::</span> rest <span style="color: #000000;">-&gt;</span>
        <span style="color: #0000FF;">let</span> newNode <span style="color: #000000;">=</span> InternalNode<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>weight first<span style="color: #000000;">&#41;</span><span style="color: #000000;">+</span><span style="color: #000000;">&#40;</span>weight second<span style="color: #000000;">&#41;</span>,first,second<span style="color: #000000;">&#41;</span>
        createHuffmanTree <span style="color: #000000;">&#40;</span>newNode<span style="color: #000000;">::</span>rest<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">|</span> <span style="color: #000000;">&#91;</span>node<span style="color: #000000;">&#93;</span> <span style="color: #000000;">-&gt;</span> node
    <span style="color: #000000;">|</span> <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">-&gt;</span> failwith <span style="color: #800000;">&quot;Cannot create a huffman tree without input nodes&quot;</span>
&nbsp;
<span style="color: #008000; font-style: italic;">/// Get a map of (symbol, length-of-huffman-code) pairs used when calculating the theoretical</span>
<span style="color: #008000; font-style: italic;">/// length of the output.</span>
<span style="color: #0000FF;">let</span> getHuffmanCodes topNode <span style="color: #000000;">=</span>
    <span style="color: #0000FF;">let</span> <span style="color: #0000FF;">rec</span> assignBitPattern node <span style="color: #000000;">=</span>
        <span style="color: #0000FF;">match</span> node <span style="color: #0000FF;">with</span>
        <span style="color: #000000;">|</span> LeafNode<span style="color: #000000;">&#40;</span><span style="color: #0000FF;">_</span>,v<span style="color: #000000;">&#41;</span> <span style="color: #000000;">-&gt;</span> <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#40;</span>v,<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">|</span> InternalNode<span style="color: #000000;">&#40;</span><span style="color: #0000FF;">_</span>,leftNode,rightNode<span style="color: #000000;">&#41;</span> <span style="color: #000000;">-&gt;</span>
            <span style="color: #0000FF;">let</span> lCodes <span style="color: #000000;">=</span> assignBitPattern leftNode <span style="color: #000000;">|&gt;</span> List<span style="color: #000000;">.</span><span style="color: #008080;">map</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">fun</span> <span style="color: #000000;">&#40;</span>v,c<span style="color: #000000;">&#41;</span> <span style="color: #000000;">-&gt;</span> <span style="color: #000000;">&#40;</span>v,<span style="color: #0000FF;">false</span><span style="color: #000000;">::</span>c<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #0000FF;">let</span> rCodes <span style="color: #000000;">=</span> assignBitPattern rightNode <span style="color: #000000;">|&gt;</span> List<span style="color: #000000;">.</span><span style="color: #008080;">map</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">fun</span> <span style="color: #000000;">&#40;</span>v,c<span style="color: #000000;">&#41;</span> <span style="color: #000000;">-&gt;</span> <span style="color: #000000;">&#40;</span>v,<span style="color: #0000FF;">true</span><span style="color: #000000;">::</span>c<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            List<span style="color: #000000;">.</span><span style="color: #008080;">append</span> lCodes rCodes
    assignBitPattern topNode <span style="color: #000000;">|&gt;</span> List<span style="color: #000000;">.</span><span style="color: #008080;">map</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">fun</span> <span style="color: #000000;">&#40;</span>v,<span style="color: #000000;">&#40;</span>c<span style="color: #000000;">:</span>bool list<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">-&gt;</span> <span style="color: #000000;">&#40;</span>v,List<span style="color: #000000;">.</span><span style="color: #008080;">length</span> c<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">|&gt;</span> Map<span style="color: #000000;">.</span><span style="color: #008080;">of_list</span>
&nbsp;
<span style="color: #008000; font-style: italic;">/// Calculates the theoretical size of the compressed data (without representing the</span>
<span style="color: #008000; font-style: italic;">/// Huffman tree) in bits, and prints the result to stdout</span>
<span style="color: #0000FF;">let</span> encode <span style="color: #000000;">&#40;</span>input<span style="color: #000000;">:</span>byte<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">=</span>
    <span style="color: #0000FF;">let</span> mapByte huffmanCodes b <span style="color: #000000;">=</span>
        <span style="color: #0000FF;">match</span> Map<span style="color: #000000;">.</span><span style="color: #008080;">tryfind</span> b huffmanCodes <span style="color: #0000FF;">with</span>
        <span style="color: #000000;">|</span> Some huffmanCodeLength <span style="color: #000000;">-&gt;</span> huffmanCodeLength
        <span style="color: #000000;">|</span> None <span style="color: #000000;">-&gt;</span> failwith <span style="color: #800000;">&quot;Unknown input byte - invalid huffman tree&quot;</span>
    <span style="color: #0000FF;">let</span> huffmanCodes <span style="color: #000000;">=</span> createNodes input <span style="color: #000000;">|&gt;</span> createHuffmanTree <span style="color: #000000;">|&gt;</span> getHuffmanCodes
    <span style="color: #0000FF;">let</span> size <span style="color: #000000;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #000000;">|</span><span style="color: #800000;">0</span><span style="color: #000000;">|</span><span style="color: #000000;">&#93;</span> <span style="color: #008000; font-style: italic;">// this is an ugly hack, but it is just for show and tell</span>
    Array<span style="color: #000000;">.</span><span style="color: #008080;">iter</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">fun</span> b <span style="color: #000000;">-&gt;</span> size<span style="color: #000000;">.</span><span style="color: #000000;">&#91;</span><span style="color: #800000;">0</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&lt;-</span> size<span style="color: #000000;">.</span><span style="color: #000000;">&#91;</span><span style="color: #800000;">0</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">+</span> mapByte huffmanCodes b<span style="color: #000000;">&#41;</span> input
    printfn <span style="color: #800000;">&quot;Original size      : %d bits&quot;</span> <span style="color: #000000;">&#40;</span>input<span style="color: #000000;">.</span><span style="color: #008080;">Length*</span><span style="color: #800000;">8</span><span style="color: #000000;">&#41;</span>
    printfn <span style="color: #800000;">&quot;Compressed size    : %d bits&quot;</span> size<span style="color: #000000;">.</span><span style="color: #000000;">&#91;</span><span style="color: #800000;">0</span><span style="color: #000000;">&#93;</span></pre></div></div>

<p>The main entry point of the application:</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp"><span style="color: #000000;">#</span>light
<span style="color: #0000FF;">module</span> Main
<span style="color: #0000FF;">open</span> FsHuffmanEncoder
<span style="color: #0000FF;">open</span> System<span style="color: #000000;">.</span><span style="color: #008080;">IO</span>
&nbsp;
<span style="color: #0000FF;">let</span> fileName <span style="color: #000000;">=</span> <span style="color: #800000;">@&quot;c:\uncompressed_text.txt&quot;</span>
encode <span style="color: #000000;">&#40;</span>File<span style="color: #000000;">.</span><span style="color: #008080;">ReadAllBytes</span><span style="color: #000000;">&#40;</span>fileName<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<h3>Important lessons in this example</h3>
<h4>Type definitions with Discriminated union</h4>
<p>One of the first things you notice in the code above is the definition of the type <em>Node</em>. This consists of either a <em>LeafNode </em>or an <em>InternalNode</em> type, and these are just names for the tuples that they represent. These named tuples are called <em>discriminators</em>, and can be used in pattern matching like so:</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp"><span style="color: #0000FF;">let</span> weight node <span style="color: #000000;">=</span>
	<span style="color: #0000FF;">match</span> node <span style="color: #0000FF;">with</span>
	<span style="color: #000000;">|</span> InternalNode<span style="color: #000000;">&#40;</span>w,<span style="color: #0000FF;">_</span>,<span style="color: #0000FF;">_</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">-&gt;</span> w
	<span style="color: #000000;">|</span> LeafNode<span style="color: #000000;">&#40;</span>w,<span style="color: #0000FF;">_</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">-&gt;</span> w</pre></div></div>

<p>Pattern matching is an important construct in any functional programming language, and is used all over.</p>
<h4>Pipelining</h4>
<p>The |> &#8220;forward pipe&#8221; operator is a very important operator in F#, and you should familiarize yourself with it. There is nothing special about it, and a description of it could be &#8220;function application in reverse&#8221;. Using |> has the advantage of making your code much easier to read and to understand the flow.</p>
<p>The following two lines of code are exactly the same. The first uses the normal way of calling functions, the second line uses a pipe to do the same.</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp">List<span style="color: #000000;">.</span><span style="color: #008080;">iter</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">fun</span> x <span style="color: #000000;">-&gt;</span> x<span style="color: #000000;">*</span><span style="color: #800000;">2</span><span style="color: #000000;">&#41;</span> myList
myList <span style="color: #000000;">|&gt;</span> List<span style="color: #000000;">.</span><span style="color: #008080;">iter</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">fun</span> x <span style="color: #000000;">-&gt;</span> x<span style="color: #000000;">*</span><span style="color: #800000;">2</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>It should be clear that when using big compound statements with a lot of inline function calls in function parameters the pipe operator helps make the code more readable.</p>
<h4>Mutable state</h4>
<p>Immutable state is one of the core properties of functional programming languages. Immutable state helps programmers write more succint programs as well as more robust programs; if the state is not shared there are no race conditions, no invalid reads/writes of memory.</p>
<p>But some times mutable state can enable you to write some piece of code in a more efficient manner. In the Huffman example I use a mutable array to store the count of each input symbol. This could of course be done using immutable state, but the code would be more complex and the performance more poor.</p>
<p>The lesson here is that although you should strive to use immutable data structures as much as possible, using mutable state is okay - and even preferable - in some situations. And when interfacing with the .NET libraries, or code written in an imperative .NET language, <span style="text-decoration: underline;">you automatically use shared state</span>.</p>
<h4>Integration with other .NET languages</h4>
<h5>Using existing .NET libraries</h5>
<p>Using .NET libraries from F# code is just as easy as from any other language. In the Huffman example I use the class System.IO.File to read the bytes from a file on disk. At the top of the source file the statement <em>open System.IO</em> functions in the same manner as a <em>using</em> statement in C#.</p>
<p>The statement <em>encode (File.ReadAllBytes(fileName))</em> - which could also be written <em>File.ReadAllBytes(fileName) |> encode</em> using the pipe operator - simply calls the method <em>ReadAllBytes </em>defined in the class <em>File</em>.</p>
<h5>Using the encoder from C#</h5>
<p>Using types defined in F# in another programming language is just as easy as the other way around. The above example has a <em>module</em> statement. This automatically resolves as a type in C#, and all methods are public static methods. Using the encoder from C# could then look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0000FF;">static</span> <span style="color: #0000FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #0000FF;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0000FF;">string</span> fileName <span style="color: #000000;">=</span> <span style="color: #800000;">@&quot;c:\uncompressed_text.txt&quot;</span><span style="color: #000000;">;</span>
	FsHuffmanEncoder.<span style="color: #0000FF;">encode</span><span style="color: #000000;">&#40;</span>File.<span style="color: #0000FF;">ReadAllBytes</span><span style="color: #000000;">&#40;</span>fileName<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Defining types with instance members is just as easy in F# as in any other language.</p>
<h4>Recursion</h4>
<p>Recursion is another important concept in functional programming. Iterating over a list is something you do a lot in programming, and defining a recursive function to do just that is more efficient than using some imperative construction (which is quite legal in F#).</p>
<h5>Tail recursion</h5>
<p>But recursion is a double edged sword. If you have programmed recursive methods in an imperative language you have probably also run into one or more stack overflow exceptions.</p>
<p>Each time a method is called the return value is pushed on the execution stack so the runtime knows where to return to when the current method call terminates. If you are not careful in terminating your recursive methods in time an unlimited number of values are pushed onto the execution stack. In reality the stack overflows and the program terminates with a nasty exception. This is also something you must be aware of when defining recursive functions in F#, especially since recursion is a key programming technique in functional programming and is thus used much more intensely.</p>
<p>Fortunately most funtional programming languages - including F# - implements what is known as &#8220;tail recursion&#8221;. Tail recursion enables you to write recursive functions that will never result in a stack overflow - you are able to write eternally recursive functions. This is done by ensuring that the <em>last</em> expression in a function definition is the recursive call.</p>
<p>The following function will result in a stack overflow exception</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp"><span style="color: #0000FF;">let</span> <span style="color: #0000FF;">rec</span> badRecursiveFunction n <span style="color: #000000;">=</span>
	<span style="color: #0000FF;">match</span> n <span style="color: #0000FF;">with</span>
	<span style="color: #000000;">|</span> <span style="color: #0000FF;">_</span> <span style="color: #0000FF;">when</span> n<span style="color: #000000;">&lt;</span><span style="color: #800000;">9999999999</span> <span style="color: #000000;">-&gt;</span> badRecursiveFunction <span style="color: #000000;">&#40;</span>n<span style="color: #800000;">+1</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">|</span> <span style="color: #0000FF;">_</span> <span style="color: #000000;">-&gt;</span> printfn <span style="color: #800000;">&quot;%d&quot;</span> n <span style="color: #008000; font-style: italic;">//some big number</span></pre></div></div>

<p>This function however is tail recursive and will terminate gracefully without putting a stain on the execution stack</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp"><span style="color: #0000FF;">let</span> <span style="color: #0000FF;">rec</span> tailRecursiveFunction n <span style="color: #000000;">=</span>
	<span style="color: #0000FF;">match</span> n <span style="color: #0000FF;">with</span>
	<span style="color: #000000;">|</span> <span style="color: #800000;">9999999999</span> <span style="color: #000000;">-&gt;</span> printfn <span style="color: #800000;">&quot;%d&quot;</span> n <span style="color: #008000; font-style: italic;">//some big number</span>
	<span style="color: #000000;">|</span> <span style="color: #0000FF;">_</span> <span style="color: #000000;">-&gt;</span> tailRecursiveFunction <span style="color: #000000;">&#40;</span>n<span style="color: #800000;">+1</span><span style="color: #000000;">&#41;</span></pre></div></div>

<h5>All that ends well is tail recursive?</h5>
<p>Be careful, though. Making sure that the last expression written in the code is the recursive method is not enough. You have to make sure that it is the last expression <strong>evaluated</strong> on the stack machine in the runtime.</p>
<p>Consider the following example. The function &#8220;sum&#8221; takes a list of numbers as input and sums them.</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp"><span style="color: #000000;">#</span>light
&nbsp;
<span style="color: #0000FF;">let</span> sum numbers <span style="color: #000000;">=</span>
	<span style="color: #000000;">|</span> <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">-&gt;</span> <span style="color: #800000;">0</span>
	<span style="color: #000000;">|</span> head<span style="color: #000000;">::</span>tail <span style="color: #000000;">-&gt;</span> head <span style="color: #000000;">+</span> sum tail</pre></div></div>

<p>At first glance this looks tail recursive, but that is not the case. If you apply <code>[3;5]</code> the expression is evaluated like this:</p>
<pre>sum [3;5]	= 3 + ( sum [5] )
		= 3 + ( 5 + sum [] )
		= 3 + ( 5 + ( 0 ) )
</pre>
<p>In order to properly evaluate this expression the values are pushed onto the evaluation stack (this is effectively the result of evaluating the expression &#8217;sum&#8217;) and then the operator &#8216;+&#8217; is executed. This way &#8217;sum&#8217; is <span style="text-decoration: underline;">not</span> the last expression to execute even though it looks like it when reading the code.</p>
<p>So you need to know how the stack machine in the runtime functions in order to write reliable, high performance code. Knowing the language is never enough.</p>
<h4>Reference implementation i C#</h4>
<p>A somewhat equivalent implementation of the above algorithm written in C# can be seen below.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0000FF;">using</span> <span style="color: #008000;">System</span><span style="color: #000000;">;</span>
<span style="color: #0000FF;">using</span> <span style="color: #008000;">System.Collections.Generic</span><span style="color: #000000;">;</span>
<span style="color: #0000FF;">using</span> <span style="color: #008000;">System.Text</span><span style="color: #000000;">;</span>
&nbsp;
<span style="color: #0000FF;">namespace</span> CsHuffmanLib
<span style="color: #000000;">&#123;</span>
    <span style="color: #008000; font-style: italic;">/// </span>
    <span style="color: #008000; font-style: italic;">/// C# version of the Huffman algorithm</span>
    <span style="color: #008000; font-style: italic;">/// </span>
    <span style="color: #0000FF;">public</span> <span style="color: #0000FF;">class</span> CsHuffmanEncoder
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0000FF;">private</span> abstract <span style="color: #0000FF;">class</span> Node
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0000FF;">public</span> <span style="color: #0000FF;">int</span> frequency <span style="color: #000000;">=</span> <span style="color: #000000;">0</span><span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0000FF;">private</span> <span style="color: #0000FF;">class</span> LeafNode <span style="color: #000000;">:</span> Node
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0000FF;">public</span> <span style="color: #0000FF;">byte</span> symbol<span style="color: #000000;">;</span>
            <span style="color: #0000FF;">public</span> <span style="color: #0000FF;">bool</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> bitPattern<span style="color: #000000;">;</span>
&nbsp;
            <span style="color: #0000FF;">public</span> LeafNode<span style="color: #000000;">&#40;</span><span style="color: #0000FF;">byte</span> symbol, <span style="color: #0000FF;">int</span> frequency<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0000FF;">this</span>.<span style="color: #0000FF;">frequency</span> <span style="color: #000000;">=</span> frequency<span style="color: #000000;">;</span>
                <span style="color: #0000FF;">this</span>.<span style="color: #0000FF;">symbol</span> <span style="color: #000000;">=</span> symbol<span style="color: #000000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0000FF;">private</span> <span style="color: #0000FF;">class</span> InternalNode <span style="color: #000000;">:</span> Node
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0000FF;">public</span> Node leftNode<span style="color: #000000;">;</span>
            <span style="color: #0000FF;">public</span> Node rightNode<span style="color: #000000;">;</span>
&nbsp;
            <span style="color: #0000FF;">public</span> InternalNode<span style="color: #000000;">&#40;</span>Node leftNode, Node rightNode<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0000FF;">this</span>.<span style="color: #0000FF;">leftNode</span> <span style="color: #000000;">=</span> leftNode<span style="color: #000000;">;</span>
                <span style="color: #0000FF;">this</span>.<span style="color: #0000FF;">rightNode</span> <span style="color: #000000;">=</span> rightNode<span style="color: #000000;">;</span>
                frequency <span style="color: #000000;">=</span> leftNode.<span style="color: #0000FF;">frequency</span> <span style="color: #000000;">+</span> rightNode.<span style="color: #0000FF;">frequency</span><span style="color: #000000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0000FF;">public</span> <span style="color: #0000FF;">int</span> Encode<span style="color: #000000;">&#40;</span><span style="color: #0000FF;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> input<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            List leafNodes <span style="color: #000000;">=</span> GetLeafNodes<span style="color: #000000;">&#40;</span>input<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
            Node topNode <span style="color: #000000;">=</span> ConstructTree<span style="color: #000000;">&#40;</span><span style="color: #0000FF;">new</span> List<span style="color: #000000;">&#40;</span>leafNodes<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
            AssignBitPatterns<span style="color: #000000;">&#40;</span>topNode, <span style="color: #0000FF;">new</span> List<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
            <span style="color: #0000FF;">int</span> bitCount <span style="color: #000000;">=</span> <span style="color: #000000;">0</span><span style="color: #000000;">;</span>
            <span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">byte</span> b <span style="color: #0000FF;">in</span> input<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #008000; font-style: italic;">//find the matching symbol and store the bit count</span>
                <span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span>Node node <span style="color: #0000FF;">in</span> leafNodes<span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    LeafNode leafNode <span style="color: #000000;">=</span> node <span style="color: #0000FF;">as</span> LeafNode<span style="color: #000000;">;</span>
                    <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span>leafNode.<span style="color: #0000FF;">symbol</span> <span style="color: #000000;">==</span> b<span style="color: #000000;">&#41;</span>
                    <span style="color: #000000;">&#123;</span>
                        bitCount <span style="color: #000000;">+=</span> leafNode.<span style="color: #0000FF;">bitPattern</span>.<span style="color: #0000FF;">Length</span><span style="color: #000000;">;</span>
                        break<span style="color: #000000;">;</span>
                    <span style="color: #000000;">&#125;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #008000; font-style: italic;">//make sure the number of bits is dividable by 8 before converting to bytes</span>
            <span style="color: #0000FF;">return</span> bitCount <span style="color: #000000;">/</span> <span style="color: #000000;">8</span> <span style="color: #000000;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>bitCount <span style="color: #000000;">%</span> <span style="color: #000000;">8</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">!=</span> <span style="color: #000000;">0</span> <span style="color: #000000;">?</span> <span style="color: #000000;">1</span> <span style="color: #000000;">:</span> <span style="color: #000000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0000FF;">private</span> List GetLeafNodes<span style="color: #000000;">&#40;</span><span style="color: #0000FF;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> input<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008000; font-style: italic;">//initialize the list that is used to count symbol occurences</span>
            List initialLeafList <span style="color: #000000;">=</span> <span style="color: #0000FF;">new</span> List<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
            <span style="color: #0000FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">int</span> i <span style="color: #000000;">=</span> <span style="color: #000000;">0</span><span style="color: #000000;">;</span> i <span style="color: #000000;">&lt;</span> <span style="color: #000000;">256</span><span style="color: #000000;">;</span> i<span style="color: #000000;">++</span><span style="color: #000000;">&#41;</span>
                initialLeafList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #0000FF;">new</span> LeafNode<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #0000FF;">byte</span><span style="color: #000000;">&#41;</span>i, <span style="color: #000000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
            <span style="color: #008000; font-style: italic;">//iterate through all inputs to get frequencies</span>
            <span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">byte</span> b <span style="color: #0000FF;">in</span> input<span style="color: #000000;">&#41;</span>
                initialLeafList<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#40;</span><span style="color: #0000FF;">int</span><span style="color: #000000;">&#41;</span>b<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">frequency</span><span style="color: #000000;">++;</span>
&nbsp;
            <span style="color: #008000; font-style: italic;">//remove any symbols that have a frequency of 0</span>
            List finalLeafList <span style="color: #000000;">=</span> <span style="color: #0000FF;">new</span> List<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
            <span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span>LeafNode leaf <span style="color: #0000FF;">in</span> initialLeafList<span style="color: #000000;">&#41;</span>
                <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span>leaf.<span style="color: #0000FF;">frequency</span> <span style="color: #000000;">&gt;</span> <span style="color: #000000;">0</span><span style="color: #000000;">&#41;</span>
                    finalLeafList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>leaf<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
            <span style="color: #0000FF;">return</span> finalLeafList<span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0000FF;">private</span> Node ConstructTree<span style="color: #000000;">&#40;</span>List nodes<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            Node node1, node2<span style="color: #000000;">;</span>
            <span style="color: #0000FF;">while</span> <span style="color: #000000;">&#40;</span>nodes.<span style="color: #0000FF;">Count</span> <span style="color: #000000;">&gt;</span> <span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #008000; font-style: italic;">//find the two smallest nodes (by frequency)</span>
                node1 <span style="color: #000000;">=</span> FindSmallestNodeByFrequency<span style="color: #000000;">&#40;</span>nodes<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
                node2 <span style="color: #000000;">=</span> FindSmallestNodeByFrequency<span style="color: #000000;">&#40;</span>nodes<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
                nodes.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #0000FF;">new</span> InternalNode<span style="color: #000000;">&#40;</span>node1, node2<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0000FF;">return</span> nodes<span style="color: #000000;">&#91;</span><span style="color: #000000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0000FF;">private</span> Node FindSmallestNodeByFrequency<span style="color: #000000;">&#40;</span>List nodes<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            Node node <span style="color: #000000;">=</span> nodes<span style="color: #000000;">&#91;</span><span style="color: #000000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">;</span>
            <span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span>Node n <span style="color: #0000FF;">in</span> nodes<span style="color: #000000;">&#41;</span>
                <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span>n.<span style="color: #0000FF;">frequency</span> <span style="color: #000000;">&lt;</span> node.<span style="color: #0000FF;">frequency</span><span style="color: #000000;">&#41;</span>
                    node <span style="color: #000000;">=</span> n<span style="color: #000000;">;</span>
            nodes.<span style="color: #0000FF;">Remove</span><span style="color: #000000;">&#40;</span>node<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
            <span style="color: #0000FF;">return</span> node<span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0000FF;">private</span> <span style="color: #0000FF;">void</span> AssignBitPatterns<span style="color: #000000;">&#40;</span>Node node, List bitsSoFar<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            LeafNode leafNode <span style="color: #000000;">=</span> node <span style="color: #0000FF;">as</span> LeafNode<span style="color: #000000;">;</span>
            <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span>leafNode <span style="color: #000000;">!=</span> <span style="color: #0000FF;">null</span><span style="color: #000000;">&#41;</span>
                leafNode.<span style="color: #0000FF;">bitPattern</span> <span style="color: #000000;">=</span> bitsSoFar.<span style="color: #0000FF;">ToArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
            <span style="color: #0000FF;">else</span>
            <span style="color: #000000;">&#123;</span>
                InternalNode internalNode <span style="color: #000000;">=</span> node <span style="color: #0000FF;">as</span> InternalNode<span style="color: #000000;">;</span>
                bitsSoFar.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #0000FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span> <span style="color: #008000; font-style: italic;">//left node</span>
                AssignBitPatterns<span style="color: #000000;">&#40;</span>internalNode.<span style="color: #0000FF;">leftNode</span>, bitsSoFar<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
                bitsSoFar.<span style="color: #0000FF;">RemoveAt</span><span style="color: #000000;">&#40;</span>bitsSoFar.<span style="color: #0000FF;">Count</span> <span style="color: #000000;">-</span> <span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
                bitsSoFar.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span> <span style="color: #008000; font-style: italic;">//right node</span>
                AssignBitPatterns<span style="color: #000000;">&#40;</span>internalNode.<span style="color: #0000FF;">rightNode</span>, bitsSoFar<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
                bitsSoFar.<span style="color: #0000FF;">RemoveAt</span><span style="color: #000000;">&#40;</span>bitsSoFar.<span style="color: #0000FF;">Count</span> <span style="color: #000000;">-</span> <span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://sector0.dk/?feed=rss2&amp;p=29</wfw:commentRss>
		</item>
		<item>
		<title>Webcast about Erlang by Joe Armstrong</title>
		<link>http://sector0.dk/?p=32</link>
		<comments>http://sector0.dk/?p=32#comments</comments>
		<pubDate>Sat, 21 Jun 2008 19:56:42 +0000</pubDate>
		<dc:creator>frank</dc:creator>
		
		<category><![CDATA[Distributed computing]]></category>

		<category><![CDATA[Functional Programming]]></category>

		<category><![CDATA[JAOO]]></category>

		<category><![CDATA[Parallel computing]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[erlang]]></category>

		<category><![CDATA[concurrency]]></category>

		<category><![CDATA[distributed computing]]></category>

		<category><![CDATA[functional]]></category>

		<category><![CDATA[webcast]]></category>

		<guid isPermaLink="false">http://sector0.dk/?p=32</guid>
		<description><![CDATA[I recently discovered this webcast called &#8220;Erlang - software for a concurrent world&#8221; by Joe Armstrong. It is recorded at a JAOO conference. Yes, I wrote &#8220;a&#8221;&#8230; I can&#8217;t find any info on which one it is but i strongly suspect it is from either London, Sydney or Brisbane in 2008.
Anyway, the webcast is about [...]]]></description>
			<content:encoded><![CDATA[<p>I recently discovered <a href="http://www.infoq.com/presentations/erlang-software-for-a-concurrent-world" target="_blank">this webcast</a> called &#8220;Erlang - software for a concurrent world&#8221; by Joe Armstrong. It is recorded at a <a href="http://www.jaoo.dk" target="_blank">JAOO</a> conference. Yes, I wrote &#8220;a&#8221;&#8230; I can&#8217;t find any info on which one it is but i strongly suspect it is from either London, Sydney or Brisbane in 2008.</p>
<p>Anyway, the webcast is about an hour long, and is really interesting.</p>
<p>Remember to check out <a href="http://armstrongonsoftware.blogspot.com/" target="_blank">Joe Armstrongs blog</a> if you are interested in Erlang and functional programming.</p>
]]></content:encoded>
			<wfw:commentRss>http://sector0.dk/?feed=rss2&amp;p=32</wfw:commentRss>
		</item>
		<item>
		<title>Website conversion</title>
		<link>http://sector0.dk/?p=27</link>
		<comments>http://sector0.dk/?p=27#comments</comments>
		<pubDate>Sat, 21 Jun 2008 18:29:13 +0000</pubDate>
		<dc:creator>frank</dc:creator>
		
		<category><![CDATA[gobbledygook]]></category>

		<category><![CDATA[drupal]]></category>

		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://sector0.dk/?p=27</guid>
		<description><![CDATA[Those who know me knows that I am not a web-programmer. I don&#8217;t think I am compatible with PHP, HTML, XML, AJAX, and all the other web technologies out there. So when I need to do something with my domain I tend to rely heavily on open source software designed so that I have to [...]]]></description>
			<content:encoded><![CDATA[<p>Those who know me knows that I am not a web-programmer. I don&#8217;t think I am compatible with PHP, HTML, XML, AJAX, and all the other web technologies out there. So when I need to do something with my domain I tend to rely heavily on open source software designed so that I have to write a minimum of code. The past months I have used <a href="http://drupal.org" target="_blank">Drupal</a> for my homepage. It has worked okay, but lately I had grown a bit tired of it - it was slow, and it seemed impossible to get it to do <em>exactly</em> what I needed. Furthermore I couldn&#8217;t upgrade it to a more recent and secure version without crashing the entire webserver, so a new solution was needed.</p>
<p>Granted, Drupal is a big content management system and is not designed to run a small homepage. Yes, I am an idiot when it comes to web technologies - so what. Fortunately a good colleague suggested <a href="http://wordpress.org" target="_blank">WordPress</a> for my website, so this saturday I have spent an incalculable number of hours trying to convert the blog entries and pages from Drupal to WordPress.</p>
<p>Whatever happened to compatible technologies..? If there is an easy way to convert data from one CMS to another it has eluded me. I had to do it by hand (export from the MySql database to a local file, use a text editor to edit the INSERT-statements and extract the HTML, copy-paste into the new system, and repeat&#8230;), but now it is complete (I think). The only regret I have is that I lost the comments for the blog entries, but that is a small price to pay.</p>
<p>So now I can lean back and enjoy the fruits of my hard labour. And hope that I will not need to upgrade my website again&#8230; <strong><em>ever!</em></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://sector0.dk/?feed=rss2&amp;p=27</wfw:commentRss>
		</item>
		<item>
		<title>The future of programming - a brief look at two functional programming languages</title>
		<link>http://sector0.dk/?p=23</link>
		<comments>http://sector0.dk/?p=23#comments</comments>
		<pubDate>Thu, 03 Apr 2008 17:24:29 +0000</pubDate>
		<dc:creator>frank</dc:creator>
		
		<category><![CDATA[F#]]></category>

		<category><![CDATA[Functional Programming]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[erlang]]></category>

		<guid isPermaLink="false">http://sector0.dk/?p=23</guid>
		<description><![CDATA[I have been a software developer for more than 8 years now, and all this time I have been developing software in imperative languages like C++, Java and C#. During the years I have occasionally had a taste of functional programming in the language Haskell. The interest has never been big enough to do some [...]]]></description>
			<content:encoded><![CDATA[<p>I have been a software developer for more than 8 years now, and all this time I have been developing software in imperative languages like C++, Java and C#. During the years I have occasionally had a taste of functional programming in the language <a href="http://haskell.org" target="_blank">Haskell</a>. The interest has never been big enough to do some real software development, but lately I have looked more into a couple of other functional programming languages, namely <a href="http://erlang.org" target="_blank">Erlang</a> and <a href="http://research.microsoft.com/fsharp/fsharp.aspx" target="_blank">F#</a>.</p>
<h4>What is functional programming</h4>
<p>Functional programming is a different programming paradigm than what most programmers are used to. Programs written in imperative languages like C++, C# and Java are sequential programs, whereas programs written in a functional programming languages (FPL) is an expression, an is evaluated as such.</p>
<p>So in a FPL you write expressions, and in imperative languages you write methods. Code written in a FPL is more concise and easier to understand, and in short the benefits of using a FPL are listed here:</p>
<ul>
<li>More concise code, which means faster development time and fewer bugs.</li>
<li>More comprehensible code, which again leads to fewer bugs and higher reliability.</li>
<li>Immutable state, which leads to more reliable programs since writing multithreaded programs is easier without having to worry about shared state, locks and the problems that arise from this.<span id="more-23"></span></li>
</ul>
<p>But don&#8221;t take my word for it. Try it yourself - I have listed a few links at the bottom of this post to some FPLs and articles. Also, you should read John Hughes article from The Computer Journal here: <a href="/public_files/why_fp_matters.pdf" target="_blank">Why Functional Programming Matters</a>.</p>
<h5>An example</h5>
<p>As an example I have written two small programs which calculates the number of primes between 2 and 9,999,999. The first one is written in C#:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0000FF;">static</span> <span style="color: #0000FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #0000FF;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0000FF;">int</span> count <span style="color: #000000;">=</span> <span style="color: #000000;">0</span><span style="color: #000000;">;</span>
	<span style="color: #0000FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">int</span> candidate <span style="color: #000000;">=</span> <span style="color: #000000;">2</span><span style="color: #000000;">;</span> candidate <span style="color: #000000;">&lt;=</span> <span style="color: #000000;">9999999</span><span style="color: #000000;">;</span> candidate<span style="color: #000000;">++</span><span style="color: #000000;">&#41;</span>
		<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span>IsPrime<span style="color: #000000;">&#40;</span>candidate<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
			count<span style="color: #000000;">++;</span>
	Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>count<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0000FF;">private</span> <span style="color: #0000FF;">static</span> <span style="color: #0000FF;">bool</span> IsPrime<span style="color: #000000;">&#40;</span><span style="color: #0000FF;">int</span> candidate<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0000FF;">int</span> max <span style="color: #000000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #0000FF;">int</span><span style="color: #000000;">&#41;</span>Math.<span style="color: #0000FF;">Sqrt</span><span style="color: #000000;">&#40;</span>candidate<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">+</span> <span style="color: #000000;">1</span><span style="color: #000000;">;</span>
	<span style="color: #0000FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">int</span> divisor <span style="color: #000000;">=</span> <span style="color: #000000;">2</span><span style="color: #000000;">;</span> divisor <span style="color: #000000;">&lt;=</span> max<span style="color: #000000;">;</span> divisor<span style="color: #000000;">++</span><span style="color: #000000;">&#41;</span>
		<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>candidate <span style="color: #000000;">%</span> divisor<span style="color: #000000;">&#41;</span> <span style="color: #000000;">==</span> <span style="color: #000000;">0</span><span style="color: #000000;">&#41;</span>
			<span style="color: #0000FF;">return</span> false<span style="color: #000000;">;</span>
	<span style="color: #0000FF;">return</span> true<span style="color: #000000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>and the second in F#:</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp"><span style="color: #000000;">#</span>light
<span style="color: #0000FF;">module</span> PrimeCalculator
<span style="color: #0000FF;">open</span> System
&nbsp;
<span style="color: #0000FF;">let</span> isPrime candidate <span style="color: #000000;">=</span>
    <span style="color: #0000FF;">match</span> candidate <span style="color: #000000;">%</span> <span style="color: #800000;">2</span><span style="color: #000000;">=</span><span style="color: #800000;">0</span> <span style="color: #0000FF;">with</span>
    <span style="color: #000000;">|</span> <span style="color: #0000FF;">true</span> <span style="color: #000000;">-&gt;</span>   <span style="color: #800000;">0</span>
    <span style="color: #000000;">|</span> <span style="color: #0000FF;">_</span> <span style="color: #000000;">-&gt;</span>      <span style="color: #0000FF;">let</span> <span style="color: #0000FF;">rec</span> isDividable candidate divisor max <span style="color: #000000;">=</span>
                    <span style="color: #0000FF;">match</span> candidate <span style="color: #0000FF;">with</span>
                    <span style="color: #000000;">|</span> <span style="color: #0000FF;">_</span> <span style="color: #0000FF;">when</span> divisor<span style="color: #000000;">&gt;</span>max <span style="color: #000000;">-&gt;</span> <span style="color: #800000;">1</span> <span style="color: #008000; font-style: italic;">//it is a prime</span>
                    <span style="color: #000000;">|</span> <span style="color: #0000FF;">_</span> <span style="color: #0000FF;">when</span> candidate <span style="color: #000000;">%</span> divisor<span style="color: #000000;">=</span><span style="color: #800000;">0</span> <span style="color: #000000;">-&gt;</span> <span style="color: #800000;">0</span> <span style="color: #008000; font-style: italic;">//not a prime</span>
                    <span style="color: #000000;">|</span> <span style="color: #0000FF;">_</span> <span style="color: #000000;">-&gt;</span> isDividable candidate <span style="color: #000000;">&#40;</span>divisor<span style="color: #800000;">+1</span><span style="color: #000000;">&#41;</span> max
                isDividable candidate <span style="color: #800000;">2</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>int<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#40;</span>Math<span style="color: #000000;">.</span><span style="color: #008080;">Sqrt</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>float<span style="color: #000000;">&#41;</span>candidate<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #0000FF;">let</span> <span style="color: #0000FF;">rec</span> countPrimes n max count <span style="color: #000000;">=</span>
    <span style="color: #0000FF;">match</span> n<span style="color: #000000;">&gt;</span>max <span style="color: #0000FF;">with</span>
    <span style="color: #000000;">|</span> <span style="color: #0000FF;">true</span> <span style="color: #000000;">-&gt;</span> count
    <span style="color: #000000;">|</span> <span style="color: #0000FF;">_</span> <span style="color: #000000;">-&gt;</span> countPrimes <span style="color: #000000;">&#40;</span>n<span style="color: #800000;">+1</span><span style="color: #000000;">&#41;</span> max <span style="color: #000000;">&#40;</span>count<span style="color: #000000;">+</span><span style="color: #000000;">&#40;</span>isPrime n<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #0000FF;">let</span> primeCount <span style="color: #000000;">=</span> countPrimes <span style="color: #800000;">2</span> <span style="color: #800000;">9999999</span> <span style="color: #800000;">0</span>
printfn <span style="color: #800000;">&quot;#primes = %d&quot;</span> primeCount</pre></div></div>

<p>A much more succinct and readable version can be seen here. Notice however that this version does not perform as fast as the version above.</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp"><span style="color: #000000;">#</span>light
&nbsp;
<span style="color: #0000FF;">let</span> <span style="color: #0000FF;">rec</span> isPrime n divisor <span style="color: #000000;">=</span>
    <span style="color: #0000FF;">match</span> n <span style="color: #0000FF;">with</span>
    <span style="color: #000000;">|</span> <span style="color: #0000FF;">_</span> <span style="color: #0000FF;">when</span> divisor<span style="color: #000000;">&gt;</span><span style="color: #000000;">&#40;</span>int<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#40;</span>sqrt <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>float<span style="color: #000000;">&#41;</span>n<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">-&gt;</span> <span style="color: #0000FF;">true</span>
    <span style="color: #000000;">|</span> <span style="color: #0000FF;">_</span> <span style="color: #000000;">-&gt;</span> <span style="color: #0000FF;">if</span> n<span style="color: #000000;">%</span>divisor<span style="color: #000000;">=</span><span style="color: #800000;">0</span> <span style="color: #0000FF;">then</span> <span style="color: #0000FF;">false</span> <span style="color: #0000FF;">else</span> isPrime n <span style="color: #000000;">&#40;</span>divisor<span style="color: #800000;">+1</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #000000;">&#91;</span><span style="color: #800000;">2</span><span style="color: #000000;">..</span><span style="color: #800000;">9999999</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">|&gt;</span> List<span style="color: #000000;">.</span><span style="color: #008080;">filter</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">fun</span> x <span style="color: #000000;">-&gt;</span> isPrime x <span style="color: #800000;">2</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">|&gt;</span> List<span style="color: #000000;">.</span><span style="color: #008080;">length</span> <span style="color: #000000;">|&gt;</span> printfn <span style="color: #800000;">&quot;#primes = %d&quot;</span></pre></div></div>

<p>Since this is F#, and F# is a superset of OCaml, you can use object oriented programming mixed with your functional programming. But you can also use imperative programming mixed with either functional or object oriented code. The following code does the same as above, but uses imperative code to iterate through all possible prime candidates. This version is even slower, but much less RAM intensive.</p>
<p>I will get into the performance of the different coding styles in a later article, since this is obviously a major factor. This isn&#8217;t a surprise since F# <i>is</i> a functional programming language. More on that later.</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp"><span style="color: #000000;">#</span>light
&nbsp;
<span style="color: #0000FF;">let</span> <span style="color: #0000FF;">rec</span> isPrime n divisor <span style="color: #000000;">=</span>
    <span style="color: #0000FF;">match</span> n <span style="color: #0000FF;">with</span>
    <span style="color: #000000;">|</span> <span style="color: #0000FF;">_</span> <span style="color: #0000FF;">when</span> divisor<span style="color: #000000;">&gt;</span><span style="color: #000000;">&#40;</span>int<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#40;</span>sqrt <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>float<span style="color: #000000;">&#41;</span>n<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">-&gt;</span> <span style="color: #0000FF;">true</span>
    <span style="color: #000000;">|</span> <span style="color: #0000FF;">_</span> <span style="color: #000000;">-&gt;</span> <span style="color: #0000FF;">if</span> n<span style="color: #000000;">%</span>divisor<span style="color: #000000;">=</span><span style="color: #800000;">0</span> <span style="color: #0000FF;">then</span> <span style="color: #0000FF;">false</span> <span style="color: #0000FF;">else</span> isPrime n <span style="color: #000000;">&#40;</span>divisor<span style="color: #800000;">+1</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #0000FF;">let</span> <span style="color: #0000FF;">mutable</span> count <span style="color: #000000;">=</span> <span style="color: #800000;">0</span>
<span style="color: #0000FF;">for</span> x <span style="color: #0000FF;">in</span> <span style="color: #800000;">2</span><span style="color: #000000;">..</span><span style="color: #800000;">9999999</span> <span style="color: #0000FF;">do</span>
    <span style="color: #0000FF;">if</span> isPrime x <span style="color: #800000;">2</span> <span style="color: #0000FF;">then</span> count <span style="color: #000000;">&lt;-</span> count<span style="color: #800000;">+1</span>
printfn <span style="color: #800000;">&quot;#primes = %d&quot;</span> count</pre></div></div>

<p>Another example of how concise and expressive a FPL is, is this implementation of the quicksort algorithm written in Haskell:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell">qsort <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>     <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
qsort <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> qsort <span style="color: green;">&#40;</span><span style="font-weight: bold;">filter</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">&lt;</span> x<span style="color: green;">&#41;</span> xs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">++</span> <span style="color: green;">&#91;</span>x<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">++</span> qsort <span style="color: green;">&#40;</span><span style="font-weight: bold;">filter</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">&gt;=</span> x<span style="color: green;">&#41;</span> xs<span style="color: green;">&#41;</span></pre></div></div>

<h4>Erlang</h4>
<p>One of the languages that I think will have a profound effect on the future of programming is Erlang. The reason I think that is, that it is <em>the</em> language for writing parallel, concurrent and distributed applications at the moment; it was designed with parallel and distributed computing in mind, as I wrote about in an <a href="http://sector0.dk/?p=5" target="_self">earlier post</a>.<br />
In Erlang each thread (they call it a &#8220;process&#8221;) is extremely lightweight compared to a normal operating system thread: an Erlang process takes up 300 bytes of space (plus the state, of course), with the result that the scheduler can switch between them in no time. A typical Erlang system runs several thousand processes without any discernible performance penalty.</p>
<p>Another thing that distinguishes Erlang from most other functional programming languages (FPL) is how easy it is to learn. In contrast with some other FPL&#8217;&#8217;s (and I have seen some really esoteric ones) it is simple and intuitive; It took me a few days of reading and writing to learn the basics of the language, how to spawn processes, and how to write distributed applications.</p>
<p>The only drawback of Erlang is its performance, which is quite poor compared to C# or other modern imperative languages. But it is only few types of applications that really need to keep the CPU(s) busy all the time - a lot of the time the CPUs are idle; a normal client- or server application that is constantly in the red is usually sick in one way or the other.</p>
<h4>F#</h4>
<p>F# (pronounced F-Sharp) is a relatively new programming language from Microsofts research department. It is a dialect of <a href="http://caml.inria.fr/ocaml/index.en.html" target="_blank">Objective Caml (OCaml)</a>, and you can in fact compile most OCaml  code with the F# compiler and run it without any modifications. F# gives you all the benefits of a FPL, and a performance profile comparable to that of C#. I don&#8221;t mean to sound like a bad commercial, but the benefits are all too clear to me - in some applications performance is paramount.</p>
<p>Besides, in november 2007 Microsoft announced that it will move the language from research to the list of supported .NET programming languages.</p>
<p>What makes this language so important is that it is compiled and run on the normal .NET runtime, just like any other .NET language. First of all this gives us the benefit of using a virtual machine that has been tested and optimized through several years. Second of all - in line with the .NET &#8220;mantra&#8221; - F# gives programmers the ability to interface with other .NET languages and the huge framework behind the .NET runtime. And I don&#8221;t mean &#8220;interfacing&#8221; as in using some kind of language bridge. No, it is native: from F# programs you have access to the framework available to all other .NET languages, and from any other .NET language you can use components written in F#.<br />
This gives programmers a big advantage when writing software. You can for example choose to write the user interface code in an imperative language like C#, and write the data processing code in F#, which is much more concise and intuitive when it comes to expressing algorithms and formulaes in code.</p>
<p>The ability to seamlessly combine two or more languages is the true strength of F#. I intend to explore that fact more when researching genetic programming, by utilizing my <a href="http://sector0.dk/?page_id=15" target="_self">MPAPI</a> framework to build parallel and distributed applications, with the core functionality developed in F#.</p>
<p>What do you think? Is this where we are heading in the future, is this where we <span style="text-decoration: underline;">should</span> be heading, or is the various projects to parallelize (and further complicate) the languages as we know them the way to go? Try googling &#8220;parallel computing&#8221; on <a href="http://blogsearch.google.com/" target="_blank">Google&#8217;s blog search</a> and see what&#8217;&#8217;s cooking in this interesting area.</p>
<h4>Links</h4>
<ul>
<li><a href="http://research.microsoft.com/fsharp/fsharp.aspx" target="_blank">F# homepage at Microsoft Research</a></li>
<li><a href="http://erlang.org" target="_blank">Open Erlang</a></li>
<li><a href="http://haskell.org" target="_blank">Haskell</a></li>
<li><a href="http://caml.inria.fr/ocaml/index.en.html" target="_blank">Objective Caml</a></li>
<li><a href="/public_files/why_fp_matters.pdf" target="_blank">Why Functional Programming Matters</a></li>
<li><a href="http://msdn2.microsoft.com/en-us/magazine/cc164244.aspx" target="_blank">F# Primer</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://sector0.dk/?feed=rss2&amp;p=23</wfw:commentRss>
		</item>
		<item>
		<title>Mono vs. Microsoft : bout 2</title>
		<link>http://sector0.dk/?p=26</link>
		<comments>http://sector0.dk/?p=26#comments</comments>
		<pubDate>Mon, 31 Mar 2008 17:42:02 +0000</pubDate>
		<dc:creator>frank</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[Mono.NET]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[mono]]></category>

		<guid isPermaLink="false">http://sector0.dk/?p=26</guid>
		<description><![CDATA[After posting the previous blog entry on Mono IL code, Marek Safar and Miguel de Icaza from the Mono team noticed that I had made the rather embarassing mistake of compiling the code in debug-mode when compiling with Microsofts compiler. That left me with quite a red face, but now is the time to do [...]]]></description>
			<content:encoded><![CDATA[<p>After posting the previous blog entry on Mono IL code, Marek Safar and Miguel de Icaza from the Mono team noticed that I had made the rather embarassing mistake of compiling the code in debug-mode when compiling with Microsofts compiler. That left me with quite a red face, but now is the time to do it properly.</p>
<p>For this test I have written a small application that computes prime numbers using a rather inefficient but simple algorithm. The program calculates the prime numbers between 2 and 100,000 (yeah, I just love primes). This is done 1,000 times and the average execution time is displayed.<span id="more-26"></span></p>
<p>The application consists of this single class:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0000FF;">class</span> Program
<span style="color: #000000;">&#123;</span>
	<span style="color: #0000FF;">static</span> <span style="color: #0000FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #0000FF;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0000FF;">int</span> n <span style="color: #000000;">=</span> <span style="color: #000000;">1000</span><span style="color: #000000;">;</span>
		<span style="color: #0000FF;">double</span> sumMs <span style="color: #000000;">=</span> <span style="color: #000000;">0</span><span style="color: #000000;">;</span>
		<span style="color: #0000FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">int</span> i <span style="color: #000000;">=</span> <span style="color: #000000;">0</span><span style="color: #000000;">;</span> i <span style="color: #000000;">&lt;</span> n<span style="color: #000000;">;</span> i<span style="color: #000000;">++</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			DateTime dtStart <span style="color: #000000;">=</span> DateTime.<span style="color: #0000FF;">Now</span><span style="color: #000000;">;</span>
			Run<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
			TimeSpan tsTotal <span style="color: #000000;">=</span> DateTime.<span style="color: #0000FF;">Now</span> <span style="color: #000000;">-</span> dtStart<span style="color: #000000;">;</span>
			sumMs <span style="color: #000000;">+=</span> tsTotal.<span style="color: #0000FF;">TotalMilliseconds</span><span style="color: #000000;">;</span>
			Console.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span><span style="color: #0000FF;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Number {0}<span style="color: #008080; font-weight: bold;">\\</span>r&quot;</span>, i<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
		<span style="color: #000000;">&#125;</span>
		Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #0000FF;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Total execution time = {0}&quot;</span>, sumMs<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
		Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #0000FF;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Average execution time = {0}&quot;</span>, sumMs <span style="color: #000000;">/</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">double</span><span style="color: #000000;">&#41;</span>n<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0000FF;">private</span> <span style="color: #0000FF;">static</span> <span style="color: #0000FF;">void</span> Run<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0000FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">long</span> l <span style="color: #000000;">=</span> <span style="color: #000000;">2</span><span style="color: #000000;">;</span> l <span style="color: #000000;">&lt;</span> <span style="color: #000000;">100000</span><span style="color: #000000;">;</span> l<span style="color: #000000;">++</span><span style="color: #000000;">&#41;</span>
			IsPrime<span style="color: #000000;">&#40;</span>l<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0000FF;">private</span> <span style="color: #0000FF;">static</span> <span style="color: #0000FF;">bool</span> IsPrime<span style="color: #000000;">&#40;</span><span style="color: #0000FF;">long</span> primeCandidate<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008000; font-style: italic;">//two is by definition a prime number, 1 is not however</span>
		<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span>primeCandidate <span style="color: #000000;">==</span> <span style="color: #000000;">2</span><span style="color: #000000;">&#41;</span>
			<span style="color: #0000FF;">return</span> true<span style="color: #000000;">;</span>
		<span style="color: #008000; font-style: italic;">//throw an exception if the number is less than 2</span>
		<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span>primeCandidate <span style="color: #000000;">&lt;</span> <span style="color: #000000;">2</span><span style="color: #000000;">&#41;</span>
			<span style="color: #0000FF;">throw</span> <span style="color: #0000FF;">new</span> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Prime candidates cannot be less than 2&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
		<span style="color: #008000; font-style: italic;">//throw away even numbers</span>
		<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>primeCandidate <span style="color: #000000;">%</span> <span style="color: #000000;">2</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">==</span> <span style="color: #000000;">0</span><span style="color: #000000;">&#41;</span>
			<span style="color: #0000FF;">return</span> false<span style="color: #000000;">;</span>
&nbsp;
		<span style="color: #0000FF;">long</span> max <span style="color: #000000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">long</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#40;</span>Math.<span style="color: #0000FF;">Sqrt</span><span style="color: #000000;">&#40;</span>primeCandidate<span style="color: #000000;">&#41;</span> <span style="color: #000000;">+</span> <span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
		<span style="color: #0000FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF;">long</span> divisor <span style="color: #000000;">=</span> <span style="color: #000000;">3</span><span style="color: #000000;">;</span> divisor <span style="color: #000000;">&lt;</span> max<span style="color: #000000;">;</span> divisor <span style="color: #000000;">+=</span> <span style="color: #000000;">2</span><span style="color: #000000;">&#41;</span>
			<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>primeCandidate <span style="color: #000000;">%</span> divisor<span style="color: #000000;">&#41;</span> <span style="color: #000000;">==</span> <span style="color: #000000;">0</span><span style="color: #000000;">&#41;</span>
				<span style="color: #0000FF;">return</span> false<span style="color: #000000;">;</span>
		<span style="color: #0000FF;">return</span> true<span style="color: #000000;">;</span> <span style="color: #008000; font-style: italic;">//it is a prime</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The program is compiled with both csc.exe (Microsoft) and gmcs.exe (Mono) with the following commands:</p>
<pre>csc /optimize+ /target:exe /out:perftest.ms.exe Program.cs
gmcs Program.cs -optimize+ -target:exe -out:perftest.mono.exe</pre>
<p>Surprisingly this yields two executables that are <span style="text-decoration: underline;">exactly</span> the same size, but that might be a coincidence; On average the Mono-generated binaries is half to two-thirds the size of the same code compiled with Microsofts compiler, so Mono is still the best choice when it comes to mobile devices.</p>
<p>The executables where run 5 times each, on each runtime. The results are shown below in milliseconds of execution time.</p>
<table border="1" frame="box" align="center">
<thead>
<tr>
<td>Microsoft runtime</td>
<td>Mono runtime</td>
</tr>
</thead>
<tbody>
<tr>
<td>
<table border="1">
<thead>
<tr>
<td>Mono compiler<!--</p>
<td--></td>
<td>Microsoft compiler<!--</p>
<td--></td>
</tr>
</thead>
<tbody>
<tr>
<td>20,140625</td>
<td>20,156250</td>
</tr>
<tr>
<td>20,187500</td>
<td>20,156250</td>
</tr>
<tr>
<td>20,125000</td>
<td>20,171875</td>
</tr>
<tr>
<td>20,203125</td>
<td>20,140625</td>
</tr>
<tr>
<td>20,06250</td>
<td>20,15625</td>
</tr>
</tbody>
</table>
</td>
<td>
<table border="1">
<thead>
<tr>
<td>Mono compiler<!--</p>
<td--></td>
<td>Microsoft compiler<!--</p>
<td--></td>
</tr>
</thead>
<tbody>
<tr>
<td>88,937</td>
<td>84,984</td>
</tr>
<tr>
<td>89,001</td>
<td>84,875</td>
</tr>
<tr>
<td>89,000</td>
<td>84,890</td>
</tr>
<tr>
<td>89,016</td>
<td>84,828</td>
</tr>
<tr>
<td>88,985</td>
<td>84,952</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<p>At closer inspection it seems that the Mono-code is sligtly faster on Microsofts runtime, and the Microsoft-code is slightly faster on the Mono runtime. This may be another coincidence, but nonetheless the discrepancies are too small to be anything other than statistical anomalies and <span style="text-decoration: underline;">nothing conclusive can be said from the data.</span></p>
<p>The only conclusion we can draw from the above data is, that apparently the Mono runtime is somewhat slower than Microsofts ditto. I write &#8220;apparently&#8221; since the data set and the number of algorithms tested are <span style="text-decoration: underline;">not</span> enough to provide us with enough statistical data to make that assumption. For example I have observed that TCP-connections perform just as well on Monos runtime as Microsofts, and the differences in execution time in the above sample are so small that they can be disregarded as measurement errors.</p>
<p>But the guys working on Mono is well aware that their runtime does not perform as well as Microsofts. Currently they are all busy implementing stuff from .NET 3.0 and 3.5, but I think that we will see some improvements on performance in the near future.</p>
<p>And let&#8217;&#8217;s not forget how important the hard work these guys are doing is. Having a multi-platform .NET environment is so important for the future of software development (.NET languages are some of the most popular languages) that I am more than willing to overlook a few differences in performance and functionality. Besides, performance isn&#8221;t everything. In most software systems - even enterprise level - the processors are mostly idle.</p>
]]></content:encoded>
			<wfw:commentRss>http://sector0.dk/?feed=rss2&amp;p=26</wfw:commentRss>
		</item>
	</channel>
</rss>
