<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<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>
	<lastBuildDate>Tue, 23 Aug 2011 10:32:55 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5</generator>
		<item>
		<title>Monadic Parsing in F#</title>
		<link>http://sector0.dk/?p=125</link>
		<comments>http://sector0.dk/?p=125#comments</comments>
		<pubDate>Mon, 01 Aug 2011 19:25:01 +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[monads]]></category>
		<category><![CDATA[parsers]]></category>

		<guid isPermaLink="false">http://sector0.dk/?p=125</guid>
		<description><![CDATA[This article is also available in PDF Abstract This article is a short tutorial on how to write extensible recursive descent parsers with no mutable state using monads in F#. The parsers that are build using the techniques described here are able to accept ambiguous grammars, and have arbitrary lenght lookahead. These LL(*) (Parr 2007) [...]]]></description>
				<content:encoded><![CDATA[<p><strong>This article is also available in <a href="/public_files/Monadic_Parsing_in_FSharp.pdf">PDF</a><br />
</strong></p>
<h2>Abstract</h2>
<p>This article is a short tutorial on how to write extensible recursive<br />
descent parsers with no mutable state using monads in F#. The parsers<br />
that are build using the techniques described here are able to accept<br />
ambiguous grammars, and have arbitrary lenght lookahead. These <em>LL(*)</em><br />
(Parr 2007) parsers are not limited to any finite number of lookaheads.<br />
Although this potentially reduces performance in comparison to machine<br />
generated bottom-up parsers, the techniques described in this article<br />
will make simpler and more elegant parsers. Also the parsers eliminate<br />
the need for lexical analysis (tokenization) which is done on the<br />
fly.</p>
<p><span id="more-125"></span></p>
<div class="p"><!----></div>
<h2>Parser Type</h2>
<div class="p"><!----></div>
<p>We start by defining the signature of our parser functions. The type<br />
signature is:</p>
<div class="p"><!----></div>
<pre>  type Parser&lt;'r&gt; = Parser of (char list -&gt; ('r*char list) list)</pre>
<p>That is, a parser is a function that takes a list of characters and<br />
produces a list of tuples. The tuple consists of the result and the<br />
- usually updated &#8211; character list that is the remainder of the input<br />
to be parsed. Putting these result tuples in a list enables us to<br />
accept ambiguous grammars.</p>
<div class="p"><!----></div>
<p>A parser function also needs to be applied so we define a partial<br />
function for that:</p>
<div class="p"><!----></div>
<pre>  let parse (Parser p) = p</pre>
<div class="p"><!----></div>
<h2>Monadic Bind Combinators</h2>
<div class="p"><!----></div>
<p>The monadic bind combinator <tt>&gt;&gt;=</tt> will run<br />
a parser and apply those results (remember that a parser returns a<br />
list of results) to the next parser. The combinator takes a parser<br />
and a function that, given a result, returns a new parser. The result<br />
of applying the second parser to the list of results is a list of<br />
lists of results, and these lists will be concatenated before returning:</p>
<div class="p"><!----></div>
<pre>  let (&gt;&gt;=) p f = Parser(fun cs -&gt;
    List.concat [for (r,cs') in parse p cs -&gt; parse (f r) cs'])</pre>
<p>A parser is typically of the general format:</p>
<div class="p"><!----></div>
<pre>  parser1 &gt;&gt;= fun r1 -&gt;
  parser2 &gt;&gt;= fun r2 -&gt;
  ...
  parsern &gt;&gt;= fun rn -&gt;
  mreturn (s r1 r2 ... rn)</pre>
<p>where s is a <em>semantic</em> function that is applied to the results<br />
of the parsers.</p>
<div class="p"><!----></div>
<p>We might not be interested in the result of a parser but only in the<br />
fact that it succeeded. So instead of writing</p>
<div class="p"><!----></div>
<pre>  parser1 &gt;&gt;= fun _ -&gt;
  parser2 &gt;&gt;= fun _ -&gt;
  ...</pre>
<p>we simply want to discard the result right away and write:</p>
<div class="p"><!----></div>
<pre>  parser1 &gt;&gt;
  parser2 &gt;&gt;
  ...</pre>
<p>The combinator <tt>&gt;&gt;</tt> can be expressed in<br />
terms of <tt>&gt;&gt;=</tt>:</p>
<div class="p"><!----></div>
<pre>  let (&gt;&gt;) p q = p &gt;&gt;= fun _ -&gt; q</pre>
<div class="p"><!----></div>
<h2>Fundamental Parsers</h2>
<div class="p"><!----></div>
<p>In order to construct more complex parsers and combinators, both general<br />
and specific to a grammar, we define a few basic parsers. The first<br />
simply injects a value into a result without consuming any input characters:</p>
<div class="p"><!----></div>
<pre>  let mreturn r = Parser(fun cs -&gt; [(r,cs)])</pre>
<p>The next is a parser that returns an empty result, often denoted λ,<br />
Λ or ϵ:</p>
<div class="p"><!----></div>
<pre>  let lambda = Parser(fun _ -&gt; [])</pre>
<p>Next is the <tt>item</tt> parser. This parser consumes a single character<br />
unconditionally:</p>
<div class="p"><!----></div>
<pre>  let item = Parser(fun cs -&gt;
    match cs with [] -&gt; [] | c::cs' -&gt; [(c,cs')])</pre>
<p>Using these fundamental parser we can define two other fundamental<br />
parser. The first is the conditional parser:</p>
<div class="p"><!----></div>
<pre>  let sat cond =
    item &gt;&gt;= fun c -&gt; if cond c then mreturn c else lambda</pre>
<p>which takes the first character and applies a conditional function<br />
to it. If the condition evaluates to <tt>true</tt> the character is<br />
returned, otherwise an empty result is returned.</p>
<p>The <tt>char</tt> parser consumes a character if and only if the character<br />
matches:</p>
<div class="p"><!----></div>
<pre>  let char c = sat ((=)c)</pre>
<p>and the <tt>digit</tt> parser consumes a character if and only if<br />
the character is in the range <tt>[0..9]</tt>:</p>
<div class="p"><!----></div>
<pre>  let digit = sat (fun c -&gt;
    (List.tryFind ((=)c) ['0'..'9']).IsSome)</pre>
<p>The <tt>alpha</tt> parser acts like <tt>digit</tt>:</p>
<div class="p"><!----></div>
<pre>  let alpha = sat (fun c -&gt;
    (List.tryFind ((=)c)(List.append
      ['a'..'z'] ['A'..'Z'])).IsSome)</pre>
<div class="p"><!----></div>
<h2>Choice Combinators</h2>
<div class="p"><!----></div>
<p>Often we need to be able to make a choice between two or more different<br />
parsers. The choice combinator does just that:</p>
<div class="p"><!----></div>
<pre>  let (&lt;|&gt;) p q = Parser(fun cs -&gt;
    match parse p cs with
    | [] -&gt; parse q cs
    | rs -&gt; rs)</pre>
<p>The choice combinator takes two arguments <tt>p</tt> and <tt>q</tt>,<br />
both of which are parsers. It first applies the first parser. If it<br />
succeeds then that result is returned- If the result is an empty list<br />
it means it failed, and <tt>q</tt> is parsed. Note that <tt>q</tt><br />
might also fail.</p>
<div class="p"><!----></div>
<p>There is another and equally important choice combinator: the ambiguous<br />
choice combinator. It appends the result of each parser to each other<br />
and returns that as a result. If one or the other parser fails with<br />
<tt>[]</tt> the result is that from the other parser.</p>
<div class="p"><!----></div>
<pre>  let (++) p q = Parser(fun cs -&gt;
    List.append (parse p cs) (parse q cs))</pre>
<div class="p"><!----></div>
<h2>Recursive Combinators</h2>
<div class="p"><!----></div>
<p>The Kleene* and Kleene (0-or-many and 1-or-many<br />
repetitions of a parser, respectively) combinators are expressed in<br />
terms of each other:</p>
<div class="p"><!----></div>
<pre>  let rec many0 p = many1 p &lt;|&gt; mreturn []
  and many1 p = p &gt;&gt;= fun r -&gt; many0 p &gt;&gt;= fun rs -&gt; mreturn (r::rs)</pre>
<p>Sometimes it is necessary to check that not only a single character<br />
but an entire string can be parsed, for example when parsing keywords:</p>
<div class="p"><!----></div>
<pre>  let rec symbol cs =
    match cs with
    | [] -&gt; mreturn []
    | c::cs' -&gt; char c &gt;&gt; symbol cs' &gt;&gt; mreturn cs</pre>
<div class="p"><!----></div>
<h2>Ambiguous Choice and LL(*)</h2>
<div class="p"><!----></div>
<p>The <tt>++</tt> combinator is important since it lets us parse ambiguous<br />
grammars. Suppose we have the simple grammar:</p>
<table border="0" width="100%">
<tbody>
<tr>
<td>
<table border="0" cellspacing="0" cellpadding="2" align="center">
<tbody>
<tr>
<td align="center"></td>
<td align="center">
<table border="0">
<tbody>
<tr>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">expr</td>
</tr>
</tbody>
</table>
</td>
<td align="center">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">→</td>
</tr>
</tbody>
</table>
</td>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">number ′;′</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">number</td>
</tr>
</tbody>
</table>
</td>
<td align="center">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">→</td>
</tr>
</tbody>
</table>
</td>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">integer | decimal</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">integer</td>
</tr>
</tbody>
</table>
</td>
<td align="center">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">→</td>
</tr>
</tbody>
</table>
</td>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">digit<sup>+</sup></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">decimal</td>
</tr>
</tbody>
</table>
</td>
<td align="center">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">→</td>
</tr>
</tbody>
</table>
</td>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">digit<sup>+</sup> ′.′ digit<sup>*</sup></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
<td align="center"></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<p>Our first attempt at writing a parser looks like this:</p>
<div class="p"><!----></div>
<pre>  let rec expr =
    number &gt;&gt;= fun n -&gt;
    char ';' &gt;&gt;
    mreturn n
  and number = integer &lt;|&gt; dec
  and integer =
    many1 digit &gt;&gt;= fun digits -&gt;
    mreturn &lt;apply sematic function on digits&gt;
  and dec =
    many1 digit &gt;&gt;= fun digits -&gt;
    char '.' &gt;&gt;
    many0 digit &gt;&gt;= fun decimals -&gt;
    mreturn &lt;apply sematic function on nums and decimals&gt;</pre>
<p>At first glance it looks fine. We expect to parse a number followed<br />
by a semicolon and nothing more in this example. But what happens<br />
if we try to parse the string &#8220;<tt>123.4</tt>;&#8221;? The <tt>number</tt><br />
parser returns an integer. The rest of <tt>expr</tt> fails since it<br />
expects a semicolon but finds a period.</p>
<p>If we substitute the use of <tt>&lt;|&gt;</tt> with <tt>++</tt> the result<br />
is different:</p>
<div class="p"><!----></div>
<pre>  ...
  and number = integer ++ dec
  ...</pre>
<p>The <tt>++</tt> combinator applies both parsers and return <tt>[(123,['.';'4';';']);(123.4,[';'])]</tt><br />
as a result. The <tt>&gt;&gt;=</tt> combinator takes<br />
this list of results and applies the next parser (here <tt>char<br />
'.'...</tt>) on both. Since the next character after <tt>123</tt> is not<br />
<tt>.</tt> this is discarded and only <tt>123.4</tt> is usable.</p>
<p>This simple example demonstrates the ability to write parsers with<br />
arbitrary length lookahead without having to explicitly implement<br />
a lookahead parser.</p>
<div class="p"><!----></div>
<h2>Other Useful Parsers and Functions</h2>
<div class="p"><!----></div>
<p>A few string helper functions might come in handy since a string and<br />
a character list are not the same thing in F#. We therefore use the<br />
unary functions</p>
<div class="p"><!----></div>
<pre>  // convert a string to a list of characters
  let (~&amp;) (str:string) = str.ToCharArray() |&gt; List.ofArray
  // convert a list of characters to a string
  let (~%) (chars:char list) = new String(Array.ofList chars)</pre>
<p>There are other combinators that are quite useful:</p>
<ul>
<li> Parse a series of parser <em>p</em> separated by a parser <em>sep</em><br />
and return the list of results from parsing <em>p</em>. For example,<br />
parsing the string <em>aba</em> or <em>abababa</em>:</p>
<pre>  let sepBy p sep =
    p &gt;&gt;= fun r -&gt;
    many0 (sep &gt;&gt; p) &gt;&gt;= fun rs -&gt;
    mreturn (r::rs)</pre>
<div class="p"><!----></div>
</li>
<li> Parse <em>p</em> followed by another parser <em>e</em>:
<pre>  let endBy p e =
    p &gt;&gt;= fun r -&gt;
    e &gt;&gt;
    mreturn r</pre>
<div class="p"><!----></div>
</li>
<li> Parse a new line and an end-of-line. Note that <tt>"rn"</tt><br />
is a newline on win32 systems.</p>
<pre>  let newline = symbol &amp;"\r\n"
  let endofline = many0 (char ' ') &gt;&gt; newline &gt;&gt; many0 (char ' ')</pre>
<div class="p"><!----></div>
</li>
<li> Parse any number (\geqq1) of spaces, here defined as either <tt>'<br />
'</tt> or <tt>'t'</tt>:</p>
<pre>  let space = many1 (char ' ' &lt;|&gt; char '\t')</pre>
<div class="p"><!----></div>
</li>
<li> Either parse the parser <em>p</em> or nothing. In grammar terms it is<br />
expressed as <em>p  &#8211;  Λ</em><em><em> and is the equivalent<br />
of 0 or 1 successful applications of </em></em><em>p.</em></p>
<pre>  let orLambda p = (p &gt;&gt;= fun v -&gt; mreturn [v]) &lt;|&gt; mreturn []</pre>
<div class="p"><!----></div>
</li>
</ul>
<p>Of course, it is possible to construct any number of parsers and combinators<br />
given the ones already mentioned here. It all depends on the particular<br />
needs when implementing a parser for any given grammar, and this tutorial<br />
is by no means exhaustive.</p>
<div class="p"><!----></div>
<h2>Error Messages</h2>
<div class="p"><!----></div>
<p>Parsers that either return a successful result or nothing at all are<br />
not particular useful; we need to be able to combine error messages<br />
and send them back up the chain so they can be displayed to the programmer.</p>
<div class="p"><!----></div>
<p>We would like to be able to write an error message like <em>&#8220;Error<br />
at line x, column y. Unexpected end-of-file. Expected &#8216;.&#8217;, &#8216;;&#8217; or<br />
&#8216;n&#8221;&#8216;</em>. To do this we extend our parser definition<br />
thus:</p>
<div class="p"><!----></div>
<pre>  type Parser&lt;'r&gt; = Parser of (char list -&gt;
    (('r*char list) list*string list*string*int))</pre>
<p>That is, a parser is a function that takes a list of characters and<br />
returns a tuple with 4 values. Their meanings are:</p>
<ul>
<li> <tt>('r*char list) list</tt> : the result of the parsing. An empty<br />
list if the parser fails.</p>
<div class="p"><!----></div>
</li>
<li> <tt>string list</tt> : a list of strings of what the parser <em>expected</em><br />
when it failed.</p>
<div class="p"><!----></div>
</li>
<li> <tt>string</tt> : a description of the <em>unexpected</em>, for example<br />
&#8220;<em>end-of-file</em>&#8221; or &#8220;<em>&#8216;;&#8217;</em>&#8220;.</p>
<div class="p"><!----></div>
</li>
<li> <tt>int</tt> : a pointer to where the error is. Here we use the number<br />
of characters <em>not</em> parsed yet. This number can easily be converted<br />
to line- and column number in the original input.</p>
<div class="p"><!----></div>
</li>
</ul>
<p>The monadic bind combinator must still run a parser and apply the<br />
results to the next parser. Results will still be concatenated. Each<br />
result list is followed by a list of strings describing what the parser<br />
expected, and these must likewise be concatenated. The bind combinator<br />
looks like this:</p>
<div class="p"><!----></div>
<pre>  let (&gt;&gt;=) p f = Parser(fun cs -&gt;
    match parse p cs with
    | ([],exs,unex,pos) -&gt; ([],exs,unex,pos)
    | (rs,_,_,_) -&gt;
      List.map (fun (r,cs') -&gt; parse (f r) cs') rs
      |&gt; List.fold (fun (rs,exs,_,_) (rs',exs',unex,pos) -&gt;
      (List.append rs' rs,List.append exs' exs,unex,pos))
        ([],[],"",len cs))</pre>
<p>The <tt>&gt;&gt;</tt> combinator is the same as before.</p>
<p>The choice operators also needs to handle the new parser type:</p>
<div class="p"><!----></div>
<pre>  let (++) p q = Parser(fun cs -&gt;
    let (prs,pexs,punex,ppos) = parse p cs
    let (qrs,qexs,qunex,qpos) = parse q cs
    (  List.append prs qrs,
      List.append pexs qexs,
      (if ppos&lt;qpos then punex else qunex),
      min ppos qpos))</pre>
<div class="p"><!----></div>
<pre>  let (&lt;|&gt;) p q = Parser(fun cs -&gt;
    match parse p cs with
    | ([],exs,_,_) -&gt;
      let (rs,exs',unex,pos) = parse q cs
      (rs,List.append exs exs',unex,pos)
    | other -&gt; other)</pre>
<p>And the fundamental parsers <tt>mreturn</tt> and <tt>lambda</tt> likewise:</p>
<div class="p"><!----></div>
<pre>  let mreturn r = Parser(fun cs -&gt; ([(r,cs)],[],"",len cs))</pre>
<div class="p"><!----></div>
<pre>  let lambda = Parser(fun cs -&gt; ([],[],"",len cs))</pre>
<p>where <tt>len</tt> is a partial function that calls <tt>List.length</tt>.<br />
In addition we need an <tt>err</tt> parser that takes a string that<br />
describes something unexpected:</p>
<div class="p"><!----></div>
<pre>  let err unex = Parser(fun cs -&gt; ([],[],unex,len cs))</pre>
<p>It is not always desirable to return a long list of what was expected<br />
(think: the entire alphabet when a digit was expected) so a <em>replace-expected-errors</em><br />
operator <tt>&gt;&gt;@</tt> is needed:</p>
<div class="p"><!----></div>
<pre>  let (&gt;&gt;@) p exp = Parser(fun cs -&gt;
    match parse p cs with
    | ([],_,unex,pos) -&gt; ([],[exp],unex,pos)
    | other -&gt; other)</pre>
<p>The <tt>item</tt> parser is changed so it returns an error message<br />
if it is applied to an empty input:</p>
<div class="p"><!----></div>
<pre>  let item = Parser(fun cs -&gt;
    match cs with
    | [] -&gt; ([],[],"end-of-file",0)
    | c::cs' -&gt; ([(c,cs')],[],"",0))</pre>
<p>The <tt>sat</tt> parser is enhanced to take a string. This string<br />
will describe what was expected if the condition is not met and is<br />
injected into the result using the <tt>&gt;&gt;@</tt><br />
operator:</p>
<div class="p"><!----></div>
<pre>  let sat cond exp =
    (item &gt;&gt;= fun c -&gt; if cond c then mreturn c else err %[c])
    &gt;&gt;@ exp</pre>
<p>The <tt>char</tt> parser uses <tt>sat</tt> and now looks like this:</p>
<div class="p"><!----></div>
<pre>  let char c = sat ((=)c) %[c]</pre>
<p>This way the <tt>char</tt> parser will return the character in the<br />
list of expected&#8217;s if the input does not satisfy the condition.</p>
<p>The parsers <tt>digit</tt> and <tt>alpha</tt> uses <tt>sat</tt> so<br />
they too must supply an error message in case of failure:</p>
<div class="p"><!----></div>
<pre>  let digit = sat (fun c -&gt;
    (List.tryFind ((=)c) ['0'..'9']).IsSome) "a digit"

  let alpha = sat (fun c -&gt;
    (List.tryFind ((=)c) (List.append
      ['a'..'z'] ['A'..'Z'])).IsSome) "a letter"</pre>
<p>The <tt>symbol</tt> parser which takes a list of characters and recursively<br />
applies the <tt>char</tt> parser to the input is enhanced using the<br />
<tt>&gt;&gt;@</tt> parser in case of failure:</p>
<div class="p"><!----></div>
<pre>  let rec symbol cs =
    (match cs with
    | [] -&gt; mreturn []
    | c::cs' -&gt; char c &gt;&gt; symbol cs' &gt;&gt; mreturn cs)
    &gt;&gt;@ %cs</pre>
<p>The <tt>endBy</tt> parser must take the last error message from <em>p</em><br />
and concatenate it with the error message from <em>e</em> using the<br />
<em>++</em> combinator:</p>
<div class="p"><!----></div>
<pre>  let endBy p e =
    p &gt;&gt;= fun r -&gt;
    (p ++ e) &gt;&gt;
    mreturn r</pre>
<p>To test that we have reached the end of the input we have the parser<br />
<tt>endoffile</tt>:</p>
<div class="p"><!----></div>
<pre>  let endoffile = Parser(fun cs -&gt;
    match cs with
    | [] -&gt; ([([],[])],[],"",0)
    | _ -&gt; ([],["end-of-file"],%[List.head cs],len cs))</pre>
<div class="p"><!----></div>
<h2>An example</h2>
<div class="p"><!----></div>
<p>Let us use what we have written to implement a parser that accepts<br />
languages of the following simple grammar (Scott 2006):</p>
<div class="p"><!----></div>
<table border="0" width="100%">
<tbody>
<tr>
<td>
<table border="0" cellspacing="0" cellpadding="2" align="center">
<tbody>
<tr>
<td align="center"></td>
<td align="center">
<table border="0">
<tbody>
<tr>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">program</td>
</tr>
</tbody>
</table>
</td>
<td align="center">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">→</td>
</tr>
</tbody>
</table>
</td>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">stmt_list</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">stmt_list</td>
</tr>
</tbody>
</table>
</td>
<td align="center">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">→</td>
</tr>
</tbody>
</table>
</td>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">stmt   stmt_list   |   Λ</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">stmt</td>
</tr>
</tbody>
</table>
</td>
<td align="center">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">→</td>
</tr>
</tbody>
</table>
</td>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">identifier &#8221;:=&#8221; expr | &#8221;read&#8221; identifier | &#8221;write&#8221; expr</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">expr</td>
</tr>
</tbody>
</table>
</td>
<td align="center">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">→</td>
</tr>
</tbody>
</table>
</td>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">term term_tail</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">term_tail</td>
</tr>
</tbody>
</table>
</td>
<td align="center">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">→</td>
</tr>
</tbody>
</table>
</td>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">add_op term term_tail | Λ</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">term</td>
</tr>
</tbody>
</table>
</td>
<td align="center">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">→</td>
</tr>
</tbody>
</table>
</td>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">factor factor_tail</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">factor_tail</td>
</tr>
</tbody>
</table>
</td>
<td align="center">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">→</td>
</tr>
</tbody>
</table>
</td>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">mul_op factor factor_tail | Λ</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">factor</td>
</tr>
</tbody>
</table>
</td>
<td align="center">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">→</td>
</tr>
</tbody>
</table>
</td>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">′(′ expr ′)′ | identifier | number</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">add_op</td>
</tr>
</tbody>
</table>
</td>
<td align="center">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">→</td>
</tr>
</tbody>
</table>
</td>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">′+′ | ′−′</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">mul_op</td>
</tr>
</tbody>
</table>
</td>
<td align="center">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">→</td>
</tr>
</tbody>
</table>
</td>
<td align="left">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">′*′ | ′/′</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
<td align="center"></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<p>First order of business is to specify what the parser will be returning.<br />
We want the parser to return a list of some type of expression which<br />
we can later evaluate. Evaluation can then either be executing the<br />
program in an interpreter, or some sort of compilation with generation<br />
of machine code, byte code, IL-code or something else. In this example<br />
we evaluate by interpreting, and the parser will return a list of<br />
expressions which is specified in the following discriminated union:</p>
<div class="p"><!----></div>
<pre>  type Expr =
    | Number of decimal
    | Identifier of string
    | Assignment of string*Expr
    | Read of string
    | Write of Expr
    | AddOp of Expr*Expr
    | SubOp of Expr*Expr
    | MulOp of Expr*Expr
    | DivOp of Expr*Expr</pre>
<p>The values of this discriminated union becomes the semantic functions<br />
in our production rules. The production rules can be implemented using<br />
the combinators like this:</p>
<div class="p"><!----></div>
<pre>  let rec program = endBy (sepBy stmt endofline) endoffile
  and stmt = read &lt;|&gt; write &lt;|&gt; assignment
  and read =
    ( symbol &amp;"read" &gt;&gt;
      many1 (char ' ') &gt;&gt;
      many1 alpha &gt;&gt;= fun identifier -&gt;
      Read (%identifier) |&gt; mreturn) &gt;&gt;@ "Read &lt;var-name&gt;"
  and write =
    ( symbol &amp;"write" &gt;&gt;
      many1 (char ' ') &gt;&gt;
      expr &gt;&gt;= fun e -&gt;
      Write e |&gt; mreturn) &gt;&gt;@ "Write &lt;expression&gt;"
  and assignment =
    ( many1 alpha &gt;&gt;= fun identifier -&gt;
      symbol &amp;":=" &gt;&gt;
      expr &gt;&gt;= fun e -&gt;
      Assignment(%identifier,e) |&gt; mreturn)
     &gt;&gt;@ "&lt;var-name&gt;:=&lt;expression&gt;"
  and expr =
    term &gt;&gt;= fun t -&gt;
    (term_tail &gt;&gt;= fun (op,b) -&gt; mreturn (op t b)) &lt;|&gt; mreturn t
  and term_tail =
    addop &gt;&gt;= fun op -&gt;
    expr &gt;&gt;= fun b -&gt;
    mreturn (op,b)
  and term =
    factor &gt;&gt;= fun f -&gt;
    (factor_tail &gt;&gt;= fun (op,b) -&gt; mreturn (op f b)) &lt;|&gt; mreturn f
  and factor_tail =
    mulop &gt;&gt;= fun op -&gt;
    term &gt;&gt;= fun b -&gt;
    mreturn (op,b)
  and factor =
    ( char '(' &gt;&gt;= fun _ -&gt;
      expr &gt;&gt;= fun e -&gt;
      char ')' &gt;&gt;
      mreturn e)
    &lt;|&gt; number
    &lt;|&gt; (many1 alpha &gt;&gt;= fun identifier -&gt;
        Identifier(%identifier) |&gt; mreturn)
  and addop =
    (char '+' &gt;&gt; mreturn (fun a b -&gt; AddOp(a,b))) &lt;|&gt;
    (char '-' &gt;&gt; mreturn (fun a b -&gt; SubOp(a,b)))
  and mulop =
    (char '*' &gt;&gt; mreturn (fun a b -&gt; MulOp(a,b))) &lt;|&gt;
    (char '/' &gt;&gt; mreturn (fun a b -&gt; DivOp(a,b)))
  and number = integer ++ decimal
  and integer =
    many1 digit &gt;&gt;= fun digits -&gt;
    Decimal.Parse(%digits+",0") |&gt; Number |&gt; mreturn
  and decimal =
    many1 digit &gt;&gt;= fun nums -&gt;
    char '.' &gt;&gt;
    many1 digit &gt;&gt;= fun decimals -&gt;
      Decimal.Parse(%nums + "," + %decimals)
      |&gt; Number
      |&gt; mreturn</pre>
<div class="p"><!----></div>
<p>This parser is capable of parsing programs like this one:</p>
<div class="p"><!----></div>
<pre>  let prog =
    "read a
    read b
    sum:=a+b
    prod:=a*b
    c:=b
    write sum
    write prod
    write c
    write ((43.2*a)+(2*b))/32.45"</pre>
<div class="p"><!----></div>
<p>Finally, evaluating the program can be done by calling <tt>parse<br />
program &amp;prog</tt> and passing the result to the <tt>evaluate</tt> function<br />
in the very simple evaluation sample code below:</p>
<div class="p"><!----></div>
<pre>  let vars = new Dictionary&lt;string,decimal&gt;()

  let rec eval stmt =
    match stmt with
    | Number d -&gt; d
    | Identifier id -&gt;
      if vars.ContainsKey(id) then vars.[id]
      else new Exception(sprintf "Unknown variable '%s'" id) |&gt; raise
    | Assignment (id,exp) -&gt;
      let v = eval exp
      vars.Add(id,v)
      v
    | Read id -&gt;
      Console.Write (sprintf "%s &gt; " id)
      let v = Console.ReadLine() |&gt; Decimal.Parse
      vars.Add(id,v)
      v
    | Write exp -&gt;
      let v = eval exp
      Console.WriteLine v
      v
    | AddOp (e1,e2) -&gt; (eval e1) + (eval e2)
    | SubOp (e1,e2) -&gt; (eval e1) - (eval e2)
    | MulOp (e1,e2) -&gt; (eval e1) * (eval e2)
    | DivOp (e1,e2) -&gt; (eval e1) / (eval e2)

  let rec evaluate stmts =
    match stmts with
    | [] -&gt; ()
    | stmt::stmts' -&gt;
      eval stmt |&gt; ignore
      evaluate stmts'</pre>
<div class="p"><!----></div>
<p>As I have shown it is quite easy to define readable, elegant parsers<br />
using monads. Furthermore, it is easy to use the existing combinators<br />
and parsers to write new ones.</p>
<p><strong>This article is also available in <a href="/public_files/Monadic_Parsing_in_FSharp.pdf">PDF</a><br />
</strong></p>
<div class="p"><!----></div>
<h2>References</h2>
<div class="p"><!----></div>
<p>Parr, T. (2007) <em>The Definite ANTLR Reference &#8211; Building Domain-Specific<br />
Languages.</em></p>
<p>Scott, M.L. (2006) <em>Programming Language Pragmatics</em>, 2<br />
edition.</p>
]]></content:encoded>
			<wfw:commentRss>http://sector0.dk/?feed=rss2&#038;p=125</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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[erlang]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Programming]]></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: [...]]]></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 &#8211; 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 &#8211; 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 &#8211; 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&#038;p=104</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</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 [...]]]></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 &#8211; say &#8211; iterating, and it is very easy to make parallel. I will show you a few examples written in F# &#8211; 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 (&#8216;::’) 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&#038;p=79</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</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[gobbledygook]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Test]]></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 [...]]]></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&#038;p=63</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</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&#038;p=55</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</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 Language) [...]]]></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&#038;p=33</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</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# &#8211; a strict, functional programming language &#8211; 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# &#8211; a strict, functional programming language &#8211; 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 &#8211; and even preferable &#8211; 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> &#8211; which could also be written <em>File.ReadAllBytes(fileName) |> encode</em> using the pipe operator &#8211; 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 &#8211; including F# &#8211; 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 &#8211; 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 &#8216;sum&#8217;) and then the operator &#8216;+&#8217; is executed. This way &#8216;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&#038;p=29</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</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[erlang]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[JAOO]]></category>
		<category><![CDATA[Parallel computing]]></category>
		<category><![CDATA[Programming]]></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 &#8211; 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 [...]]]></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 &#8211; 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&#038;p=32</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</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 &#8211; 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 &#8211; 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&#038;p=27</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The future of programming &#8211; 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[erlang]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Programming]]></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 &#8211; 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&#8221;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 &#8211; 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 &#8211; 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 &#8211; in line with the .NET &#8220;mantra&#8221; &#8211; 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&#8221;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&#038;p=23</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
