<?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>Inadvertent incorrectness &#187; F#</title>
	<atom:link href="http://ken.friislarsen.net/blog/category/coding/fsharp/feed/" rel="self" type="application/rss+xml" />
	<link>http://ken.friislarsen.net/blog</link>
	<description>... but that doesn&#039;t matter, because I turn it into a sexy dance</description>
	<lastBuildDate>Fri, 16 Jul 2010 11:26:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>I Am Going to JAOO 2008 As A F# Expert</title>
		<link>http://ken.friislarsen.net/blog/2008/09/11/i-am-going-to-jaoo-2008-as-a-f-expert/</link>
		<comments>http://ken.friislarsen.net/blog/2008/09/11/i-am-going-to-jaoo-2008-as-a-f-expert/#comments</comments>
		<pubDate>Thu, 11 Sep 2008 20:01:45 +0000</pubDate>
		<dc:creator>Ken</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[F#]]></category>

		<guid isPermaLink="false">http://ken.friislarsen.net/blog/2008/09/11/i-am-going-to-jaoo-2008-as-a-f-expert/</guid>
		<description><![CDATA[Microsoft Denmark have invited me to participate in JAOO 2008. If I in return spend some time in the Microsoft stand demoing F# and answering questions about F# and functional programming in general. When Martin Esmann (Microsoft Academic Developer Evangelist) &#8230; <a href="http://ken.friislarsen.net/blog/2008/09/11/i-am-going-to-jaoo-2008-as-a-f-expert/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Microsoft Denmark have invited me to participate in <a href="http://jaoo.dk">JAOO 2008</a>. If I in return spend some time in the Microsoft stand demoing <a href="http://research.microsoft.com/fsharp/fsharp.aspx">F#</a> and answering questions about F# and functional programming in general.</p>

<p>When <a href="http://blogs.msdn.com/mesmann">Martin Esmann</a> (Microsoft Academic Developer Evangelist) approached me, I told him that I&#8217;d be happy to show up, but I&#8217;m not using F# from within Visual Studio. In fact, I&#8217;m usually not even using Microsoft&#8217;s .NET implementation. I&#8217;m a happy <a href="http://www.mono-project.com">Mono</a> user. To Martin&#8217;s credit his initial reaction was that it would be cool that I demoed F# using Linux and Mono in the Microsoft stand.</p>

<p>However, after talking it over with his colleges, Martin got back to me and told me that they thought that using Linux and Mono to demo F# would send to mixed a message. I agree with that. So unfortunately I won&#8217;t be demoing Mono in a Microsoft stand this time. But I&#8217;ll of course bring my own laptop, and might show F# on Mono and Linux if there are questions about portability. Maybe I should contact the Mono guys and ask for a T-shirt, so that I silently can invite questions about Mono  <img src='http://ken.friislarsen.net/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>

<p>Time to practice some F# demos.  Any suggestions for what would be effective? I&#8217;m planning to show of the brand new Units of Measure and also sequence comprehensions.</p>
]]></content:encoded>
			<wfw:commentRss>http://ken.friislarsen.net/blog/2008/09/11/i-am-going-to-jaoo-2008-as-a-f-expert/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Decoding Morse Code With F# Comprehensions</title>
		<link>http://ken.friislarsen.net/blog/2007/11/09/decoding-morse-code-with-f-comprehensions/</link>
		<comments>http://ken.friislarsen.net/blog/2007/11/09/decoding-morse-code-with-f-comprehensions/#comments</comments>
		<pubDate>Fri, 09 Nov 2007 11:31:30 +0000</pubDate>
		<dc:creator>Ken</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://ken.friislarsen.net/blog/2007/11/09/decoding-morse-code-with-f-comprehensions/</guid>
		<description><![CDATA[In my last post I showed how to decode morse code in Python using list comprehensions. In this post I show how to do it in F# instead. First using list comprehensions: let codes = [("A",".-"); ("B","-..."); ("C","-.-."); ("D","-.."); ("E","."); &#8230; <a href="http://ken.friislarsen.net/blog/2007/11/09/decoding-morse-code-with-f-comprehensions/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In my last post I showed how to <a href="http://ken.friislarsen.net/blog/2007/09/19/morse-code-decoding-with-python-list-comprehensions/">decode morse code in Python using list comprehensions</a>.  In this post I show how to do it in <a href="http://research.microsoft.com/fsharp/fsharp.aspx">F#</a> instead.</p>

<p>First using list comprehensions:</p>

<pre>let codes =
    [("A",".-");   ("B","-..."); ("C","-.-."); ("D","-.."); ("E",".");
     ("F","..-."); ("G","--.");  ("H","...."); ("I","..");  ("J",".---");
     ("K","-.-");  ("L",".-.."); ("M","--");   ("N","-.");  ("O","---");
     ("P",".--."); ("Q","--.-"); ("R",".-.");  ("S","..."); ("T","-");
     ("U","..-");  ("V","...-"); ("W",".--");  ("X","-..-");("Y","-.--");
     ("Z","--..")]</pre>

<pre>let rec decode input =
    if input = "" then [""]
    else [ for c, code in codes when input.StartsWith(code)
           for rest in decode(input.Substring(String.length code))
           -&gt; c + rest ]</pre>

<p>As it can be seen the code is almost identical to the Python code.  Incidentally, I could not find a function equivalent to Python&#8217;s <code>startswith</code> method in the O&#8217;Caml standard library (without using regular expressions).  Fortunately F# came with one from the .NET library.</p>

<p>Much to my the surprise the compiled F# (running on Mono 1.2.4) is 4 times slower that the Python code.  I then rewrote the program to use sequence comprehensions:</p>

<pre>
let rec decode input =
    if input = "" then { -&gt; "" }
    else { for c, code in codes when input.StartsWith(code)
           for rest in decode(input.Substring(String.length code))
           -&gt; c + rest }</pre>

<p>This version runs faster, and uses only a constant amount of memory. Still the Python version is three and half times faster.</p>

<p>I then tried to run the programs on an other computer with Microsoft&#8217;s .NET implementation. This improved the F# running times a lot.  However, they are still 40% to 80% slower than the Python version.</p>

<p><img src="http://ken.friislarsen.net/blog/wp-content/fs-vs-python-morse.png" alt="Chart of F# vs Python on morse code decoding" /></p>

<p>My current guess is that Python is much better at handling strings than .NET.</p>

<p>The actual numbers:</p>

<table border="1">
<tr>
<th></th>
<th colspan="2">Linux, Mono</th>
<th colspan="2">Vista, MS .NET</th>
</tr>
<tr>
<td>Program</td>
<th>Time
sec</th>
<th>Ratio</th>
<th>Time
sec</th>
<th>Ratio</th>
</tr>
<tr>
<th><code>morse.py</code></th>
<td>7.94 ± 0.05</td>
<td>1</td>
<td>11.03 ± 0.03</td>
<td>1</td>
</tr>
<tr>
<th><code>morse.fs</code></th>
<td>33.07 ± 0.19</td>
<td>4.17</td>
<td>19.65 ± 0.33</td>
<td>1.78</td>
</tr>
<tr>
<th><code>morse-seq.fs</code></th>
<td>28.1 ± 0.59</td>
<td>3.54</td>
<td>16.12 ± 0.36</td>
<td>1.46</td>
</tr>
</table>

<p><strong>Update 2007-11-12 Added test files:</strong></p>

<ul>
    <li>Python code: <a href="http://ken.friislarsen.net/tmp/morse-py.txt"><code>morse.py</code></a></li>
    <li>F# code using list comprehensions: <a href="http://ken.friislarsen.net/tmp/morse.fs"><code>morse.fs</code></a></li>
    <li>F# code using sequence comprehensions: <a href="http://ken.friislarsen.net/tmp/morse-seq.fs"><code>morse-seq.fs</code></a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://ken.friislarsen.net/blog/2007/11/09/decoding-morse-code-with-f-comprehensions/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
