<?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; Python</title>
	<atom:link href="http://ken.friislarsen.net/blog/category/coding/python/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>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>
		<item>
		<title>Morse Code Decoding With Python List Comprehensions</title>
		<link>http://ken.friislarsen.net/blog/2007/09/19/morse-code-decoding-with-python-list-comprehensions/</link>
		<comments>http://ken.friislarsen.net/blog/2007/09/19/morse-code-decoding-with-python-list-comprehensions/#comments</comments>
		<pubDate>Wed, 19 Sep 2007 20:16:44 +0000</pubDate>
		<dc:creator>Ken</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://ken.friislarsen.net/blog/2007/09/19/morse-code-decoding-with-python-list-comprehensions/</guid>
		<description><![CDATA[As a small exercise for getting up to speed with Python I decided to solve ruby quiz #121, which is to to write a function that finds all possible decodings of a string of Morse codes without letter- and word-separators. &#8230; <a href="http://ken.friislarsen.net/blog/2007/09/19/morse-code-decoding-with-python-list-comprehensions/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>As a small exercise for getting up to speed with <a href="http://python.org">Python</a> I decided to solve <a href="http://www.rubyquiz.com/quiz121.html">ruby quiz #121</a>, which is to to write a function that finds all possible decodings of a string of <a href="http://en.wikipedia.org/wiki/Morse_code">Morse codes</a> without letter- and word-separators.  Given the nature of the problem I decided to use python&#8217;s list comprehensions for the solution.</p>

<p>Without further ado here is the code I ended up with:</p>

<pre>#!/usr/bin/env python

import string

letters = [('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',"--..")]

def decode(input):
    if input == "" :
        return [""]
    else:
        return [ letter + remaining
                 for letter, code in letters if input.startswith(code)
                 for remaining in decode(input[len(code):]) ]

# Some Testing code
def test(s, code):
    if s in decode(code):
        print code + " can be decoded as " + s
    else:
        print code + " can NOT be decoded as " + s

test("SOFIA", "...---..-....-")
test("SOPHIA", "...---..-....-")
test("EUGENIA", "...---..-....-")</pre>

<p>Interesting, my solution is rather similar to <a href="http://patricklogan.blogspot.com/2007/09/list-comprehensions.html">Patrick Logan&#8217;s Erlang solution</a>. And I find it simpler to understand than the <a href="http://www.haskell.org/haskellwiki/Haskell_Quiz/Morse_Code">Haskell solutions at the HaskellWiki.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://ken.friislarsen.net/blog/2007/09/19/morse-code-decoding-with-python-list-comprehensions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
