<?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>porges &#187; Haskell</title>
	<atom:link href="http://porg.es/blog/tag/haskell/feed" rel="self" type="application/rss+xml" />
	<link>http://porg.es/blog</link>
	<description>... master of none</description>
	<lastBuildDate>Sat, 12 Sep 2009 07:57: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>n-pivot quicksort</title>
		<link>http://porg.es/blog/n-pivot-quicksort</link>
		<comments>http://porg.es/blog/n-pivot-quicksort#comments</comments>
		<pubDate>Sat, 12 Sep 2009 07:56:15 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[comparison]]></category>
		<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[quicksort]]></category>
		<category><![CDATA[sorting]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=392</guid>
		<description><![CDATA[2-pivot quicksort This posting on the Java core library mailing list proposes to replace the current quicksort with a new, 2-pivot quicksort. Ordinary quicksort in Haskell looks something like this: quicksort &#91;&#93; = &#91;&#93; quicksort &#40;pivot:rest&#41; = quicksort &#91;x&#124; x ← rest, x ≤ pivot&#93; ++ &#91;pivot&#93; ++ quicksort &#91;x&#124; x ← rest, x &#62; [...]]]></description>
			<content:encoded><![CDATA[<h3>2-pivot quicksort</h3>
<p>This <a href="http://permalink.gmane.org/gmane.comp.java.openjdk.core-libs.devel/2628">posting on the Java core library mailing list</a> proposes to replace the current quicksort with a new, 2-pivot quicksort.</p>
<p>Ordinary quicksort in Haskell looks something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">quicksort <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>
quicksort <span style="color: green;">&#40;</span>pivot:rest<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span>
   quicksort <span style="color: green;">&#91;</span>x<span style="color: #339933; font-weight: bold;">|</span> x ← rest<span style="color: #339933; font-weight: bold;">,</span> x ≤ pivot<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">++</span>
   <span style="color: green;">&#91;</span>pivot<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">++</span>
   quicksort <span style="color: green;">&#91;</span>x<span style="color: #339933; font-weight: bold;">|</span> x ← rest<span style="color: #339933; font-weight: bold;">,</span> x <span style="color: #339933; font-weight: bold;">&gt;</span> pivot<span style="color: green;">&#93;</span></pre></div></div>

<p>Note the similarity of this to the notation for &#8216;invariants&#8217; used in the email:</p>
<pre>[ <= p | >= p ]</pre>
<p>Similarly, for the 2-pivot invariants (I have corrected a typo in this):</p>
<pre>[ < P1 | P1 <= &#038; <= P2 | > P2 ]</pre>
<p>We can derive the code:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">quicksort2 <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>
quicksort2 <span style="color: green;">&#40;</span>only:<span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span>only<span style="color: green;">&#93;</span>
quicksort2 <span style="color: green;">&#40;</span>first:second:rest<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span>
   quicksort2 <span style="color: green;">&#91;</span>x<span style="color: #339933; font-weight: bold;">|</span>x ← rest<span style="color: #339933; font-weight: bold;">,</span> x <span style="color: #339933; font-weight: bold;">&lt;</span> p1<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">++</span>
   <span style="color: green;">&#91;</span>p1<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">++</span>
   quicksort2 <span style="color: green;">&#91;</span>x<span style="color: #339933; font-weight: bold;">|</span>x ← rest<span style="color: #339933; font-weight: bold;">,</span> p1 ≤ x <span style="color: #339933; font-weight: bold;">&amp;&amp;</span> x ≤ p2<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">++</span>
   <span style="color: green;">&#91;</span>p2<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">++</span>
   quicksort2 <span style="color: green;">&#91;</span>x<span style="color: #339933; font-weight: bold;">|</span>x ← rest<span style="color: #339933; font-weight: bold;">,</span> p2 <span style="color: #339933; font-weight: bold;">&lt;</span> x<span style="color: green;">&#93;</span>
  <span style="color: #06c; font-weight: bold;">where</span> <span style="color: green;">&#40;</span>p1<span style="color: #339933; font-weight: bold;">,</span>p2<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">if</span> first ≤ second <span style="color: #06c; font-weight: bold;">then</span> <span style="color: green;">&#40;</span>first<span style="color: #339933; font-weight: bold;">,</span>second<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">else</span> <span style="color: green;">&#40;</span>second<span style="color: #339933; font-weight: bold;">,</span>first<span style="color: green;">&#41;</span></pre></div></div>

<p>Which does, in fact, run faster than the ordinary 1-pivot quicksort.</p>
<h3><i>n</i>-pivot quicksort</h3>
<p>Of course, this being Haskell we want to take things to their logical conclusions, and allow any number of pivots to be used. My formulation follows:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">quicksortN <span style="color: #339933; font-weight: bold;">_</span> <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>
quicksortN <span style="color: #339933; font-weight: bold;">_</span> <span style="color: green;">&#40;</span>x:<span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><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>
quicksortN n xs
    <span style="color: #339933; font-weight: bold;">|</span> n <span style="color: #339933; font-weight: bold;">&gt;</span> <span style="font-weight: bold;">length</span> xs <span style="color: #339933; font-weight: bold;">=</span> quicksortN <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>n `<span style="font-weight: bold;">div</span>` <span style="color: red;">2</span><span style="color: green;">&#41;</span> `<span style="font-weight: bold;">max</span>` <span style="color: red;">1</span><span style="color: green;">&#41;</span> xs
    <span style="color: #339933; font-weight: bold;">|</span> <span style="font-weight: bold;">otherwise</span> <span style="color: #339933; font-weight: bold;">=</span> part True pivots
    <span style="color: #06c; font-weight: bold;">where</span>
      <span style="color: green;">&#40;</span>takexs<span style="color: #339933; font-weight: bold;">,</span>dropxs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">splitAt</span> n xs
      pivots <span style="color: #339933; font-weight: bold;">=</span> quicksortN <span style="color: red;">1</span> takexs
      part False <span style="color: green;">&#40;</span>p2:<span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span>      <span style="color: #339933; font-weight: bold;">=</span> quicksortN n <span style="color: green;">&#91;</span>x<span style="color: #339933; font-weight: bold;">|</span>x ← dropxs<span style="color: #339933; font-weight: bold;">,</span> p2 <span style="color: #339933; font-weight: bold;">&lt;</span> x<span style="color: green;">&#93;</span>
      part False <span style="color: green;">&#40;</span>p1:p2:rest<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> quicksortN n <span style="color: green;">&#91;</span>x<span style="color: #339933; font-weight: bold;">|</span>x ← dropxs<span style="color: #339933; font-weight: bold;">,</span> p1 <span style="color: #339933; font-weight: bold;">&lt;</span> x <span style="color: #339933; font-weight: bold;">&amp;&amp;</span> x ≤ p2<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">++</span> <span style="color: green;">&#91;</span>p2<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">++</span> part False <span style="color: green;">&#40;</span>p2:rest<span style="color: green;">&#41;</span>
      part True  <span style="color: green;">&#40;</span>p1:<span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span>      <span style="color: #339933; font-weight: bold;">=</span> quicksortN n <span style="color: green;">&#91;</span>x<span style="color: #339933; font-weight: bold;">|</span>x ← dropxs<span style="color: #339933; font-weight: bold;">,</span> x ≤ p1<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">++</span> <span style="color: green;">&#91;</span>p1<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">++</span> quicksortN n <span style="color: green;">&#91;</span>x<span style="color: #339933; font-weight: bold;">|</span>x ← dropxs<span style="color: #339933; font-weight: bold;">,</span> p1 <span style="color: #339933; font-weight: bold;">&lt;</span> x<span style="color: green;">&#93;</span> <span style="color: #5d478b; font-style: italic;">-- remember me? :)</span>
      part True  <span style="color: green;">&#40;</span>p1:p2:rest<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> quicksortN n <span style="color: green;">&#91;</span>x<span style="color: #339933; font-weight: bold;">|</span>x ← dropxs<span style="color: #339933; font-weight: bold;">,</span> x ≤ p1<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">++</span> <span style="color: green;">&#91;</span>p1<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">++</span> part False <span style="color: green;">&#40;</span>p1:p2:rest<span style="color: green;">&#41;</span></pre></div></div>

<p>Now there are only 3 things that I can see that are left &#8216;tweakable&#8217;;</p>
<ul>
<li>how to reduce &#8216;n&#8217; when it is greater than length of the list</p>
<ul>
<li>dividing by 2 here</li>
</ul>
</li>
<li>how to sort the pivots
<ul>
<li>using a recursive call to the 1-pivot version of itself here</li>
</ul>
</li>
<li>whether or not to reduce the number of pivots as sorting continues
<ul>
<li>&#8216;no&#8217; chosen here</li>
</ul>
</li>
</ul>
<p>I have no idea whether these are sensible defaults <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_smile.gif" alt="" /></p>
<h3>Which <i>n</i> should I choose?</h3>
<p>Finally, some code for the timings. You&#8217;ll need <a href="http://hackage.haskell.org/package/timeit">timeit</a> to run it.</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">main <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
 gen ← getStdGen
 <span style="color: #06c; font-weight: bold;">let</span> n <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">10</span><span style="color: #339933; font-weight: bold;">^</span><span style="color: red;">6</span>
 <span style="color: #06c; font-weight: bold;">let</span> r <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">take</span> n <span style="color: #339933; font-weight: bold;">$</span> randoms gen <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#93;</span>
 <span style="font-weight: bold;">print</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">sum</span> r <span style="color: #5d478b; font-style: italic;">-- force list</span>
 timeIt <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">putStrLn</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="color: green;">&#40;</span><span style="background-color: #3cb371;">&quot;quicksort = &quot;</span><span style="color: #339933; font-weight: bold;">++</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">show</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">sum</span> <span style="color: #339933; font-weight: bold;">$</span> quicksort r
 timeIt <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">putStrLn</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="color: green;">&#40;</span><span style="background-color: #3cb371;">&quot;quicksort2 = &quot;</span><span style="color: #339933; font-weight: bold;">++</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">show</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">sum</span> <span style="color: #339933; font-weight: bold;">$</span> quicksort2 r
 <span style="font-weight: bold;">mapM_</span> <span style="color: green;">&#40;</span>\x → timeIt <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">putStrLn</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="background-color: #3cb371;">&quot;quicksortN &quot;</span><span style="color: #339933; font-weight: bold;">++</span> <span style="font-weight: bold;">show</span> x<span style="color: #339933; font-weight: bold;">++</span><span style="background-color: #3cb371;">&quot; = &quot;</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">++</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">show</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">sum</span> <span style="color: #339933; font-weight: bold;">$</span> quicksortN x r<span style="color: green;">&#41;</span>
    <span style="color: green;">&#91;</span>1<span style="color: #339933; font-weight: bold;">..</span>20<span style="color: green;">&#93;</span></pre></div></div>

<p>Sample output:</p>
<pre>$ ./sort
858124546
quicksort = 858124546
CPU time:   4.60s
quicksort2 = 858124546
CPU time:   3.66s
quicksortN 1 = 858124546
CPU time:   5.19s
quicksortN 2 = 858124546
CPU time:   3.77s
quicksortN 3 = 858124546
CPU time:   3.28s
quicksortN 4 = 858124546
CPU time:   3.27s
quicksortN 5 = 858124546
CPU time:   3.06s
quicksortN 6 = 858124546
CPU time:   3.04s
quicksortN 7 = 858124546
CPU time:   3.04s
quicksortN 8 = 858124546
CPU time:   3.07s
quicksortN 9 = 858124546
CPU time:   3.10s
quicksortN 10 = 858124546
CPU time:   3.14s
quicksortN 11 = 858124546
CPU time:   3.22s
quicksortN 12 = 858124546
CPU time:   3.31s
quicksortN 13 = 858124546
CPU time:   3.30s
quicksortN 14 = 858124546
CPU time:   3.37s
quicksortN 15 = 858124546
CPU time:   3.54s
quicksortN 16 = 858124546
CPU time:   3.41s
quicksortN 17 = 858124546
CPU time:   3.52s
quicksortN 18 = 858124546
CPU time:   3.65s
quicksortN 19 = 858124546
CPU time:   3.63s
quicksortN 20 = 858124546
CPU time:   3.77s
</pre>
<p>As expected, generalized quicksort performs slower than the equivalent hard-coded quicksorts for specific values of <i>n</i>, but higher values of <i>n</i> than 1 or 2 can perform better.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/n-pivot-quicksort/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Email address validation: Simpler, Faster, More Correct</title>
		<link>http://porg.es/blog/email-address-validation-simpler-faster-more-correct</link>
		<comments>http://porg.es/blog/email-address-validation-simpler-faster-more-correct#comments</comments>
		<pubDate>Wed, 11 Mar 2009 11:10:31 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[address]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[rfc]]></category>
		<category><![CDATA[rfc5322]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=317</guid>
		<description><![CDATA[So, I have merged the obsolete-syntax into the code from the last post. This has resulted in shorter, cleaner, faster validation which is also more correct. I didn’t like the fact that in the old code there were places where explicit try points needed to be included. It seems that these arose because the ‘obsolete’ [...]]]></description>
			<content:encoded><![CDATA[<p>So, I have merged the obsolete-syntax into the code from the last post. This has resulted in shorter, cleaner, faster validation which is <em>also</em> more correct.</p>
<p>I didn’t like the fact that in the old code there were places where explicit <code>try</code> points needed to be included. It seems that these arose because the ‘obsolete’ syntax was tacked-on to the EBNF for the normal syntax, creating much overlap. Since I merged the syntaxes together, there are <em>no</em> explicit try points needed (there are some implicit ones, I believe, such as in <code>optional</code>). This makes the code both faster and easier to understand.</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">module</span> Text<span style="color: #339933; font-weight: bold;">.</span>Email<span style="color: #339933; font-weight: bold;">.</span>Validation <span style="color: green;">&#40;</span>isValid<span style="color: green;">&#41;</span>
<span style="color: #06c; font-weight: bold;">where</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">import</span> Text<span style="color: #339933; font-weight: bold;">.</span>Parsec
<span style="color: #06c; font-weight: bold;">import</span> Text<span style="color: #339933; font-weight: bold;">.</span>Parsec<span style="color: #339933; font-weight: bold;">.</span><span style="color: #cccc00; font-weight: bold;">Char</span>
<span style="color: #06c; font-weight: bold;">import</span> Data<span style="color: #339933; font-weight: bold;">.</span><span style="color: #cccc00; font-weight: bold;">Char</span> <span style="color: green;">&#40;</span>chr<span style="color: green;">&#41;</span>
&nbsp;
isValid <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">String</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Bool</span>
isValid x <span style="color: #339933; font-weight: bold;">=</span> 	<span style="font-weight: bold;">either</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">const</span> False<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">const</span> True<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>valid x<span style="color: green;">&#41;</span>
&nbsp;
simply <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">&gt;&gt;</span> <span style="font-weight: bold;">return</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
<span style="color: #5d478b; font-style: italic;">-- simply converts a parser returning something to a parser returning nothing</span>
&nbsp;
valid <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">String</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Either</span> ParseError <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
valid <span style="color: #339933; font-weight: bold;">=</span> parse addrSpec <span style="background-color: #3cb371;">&quot;&quot;</span>
&nbsp;
addrSpec <span style="color: #339933; font-weight: bold;">=</span> localPart <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> char '<span style="color: #339933; font-weight: bold;">@</span>' <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> domain <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> eof
&nbsp;
localPart <span style="color: #339933; font-weight: bold;">=</span> dottedAtoms
domain <span style="color: #339933; font-weight: bold;">=</span> dottedAtoms <span style="color: #339933; font-weight: bold;">&lt;|&gt;</span> domainLiteral 
&nbsp;
dottedAtoms <span style="color: #339933; font-weight: bold;">=</span> simply <span style="color: #339933; font-weight: bold;">$</span> <span style="color: green;">&#40;</span>optional cfws <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> <span style="color: green;">&#40;</span>atom <span style="color: #339933; font-weight: bold;">&lt;|&gt;</span> quotedString<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> optional cfws<span style="color: green;">&#41;</span>
	`sepBy1` <span style="color: green;">&#40;</span>char '<span style="color: #339933; font-weight: bold;">.</span>'<span style="color: green;">&#41;</span>
atom <span style="color: #339933; font-weight: bold;">=</span> simply <span style="color: #339933; font-weight: bold;">$</span> many1 atomText
atomText <span style="color: #339933; font-weight: bold;">=</span> simply <span style="color: #339933; font-weight: bold;">$</span> alphaNum <span style="color: #339933; font-weight: bold;">&lt;|&gt;</span> oneOf <span style="background-color: #3cb371;">&quot;!#$%&amp;'*+-/=?^_`{|}~&quot;</span>
&nbsp;
domainLiteral <span style="color: #339933; font-weight: bold;">=</span>  between <span style="color: green;">&#40;</span>optional cfws <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> char '<span style="color: green;">&#91;</span>'<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>char '<span style="color: green;">&#93;</span>' <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> optional cfws<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$</span>
	many <span style="color: green;">&#40;</span>optional fws <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> domainText<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> optional fws
domainText <span style="color: #339933; font-weight: bold;">=</span> ranges <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span>33<span style="color: #339933; font-weight: bold;">..</span>90<span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#91;</span>94<span style="color: #339933; font-weight: bold;">..</span>126<span style="color: green;">&#93;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">&lt;|&gt;</span> obsNoWsCtl
&nbsp;
quotedString <span style="color: #339933; font-weight: bold;">=</span> between <span style="color: green;">&#40;</span>char '<span style="background-color: #3cb371;">&quot;') (char '&quot;</span>'<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$</span>
	many <span style="color: green;">&#40;</span>optional fws <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> quotedContent<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> optional fws
quotedContent <span style="color: #339933; font-weight: bold;">=</span> quotedText <span style="color: #339933; font-weight: bold;">&lt;|&gt;</span> quotedPair
quotedText <span style="color: #339933; font-weight: bold;">=</span> ranges <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span><span style="color: red;">33</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#91;</span>35<span style="color: #339933; font-weight: bold;">..</span>91<span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#91;</span>93<span style="color: #339933; font-weight: bold;">..</span>126<span style="color: green;">&#93;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">&lt;|&gt;</span> obsNoWsCtl
quotedPair <span style="color: #339933; font-weight: bold;">=</span> char '\\' <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> <span style="color: green;">&#40;</span>vchar <span style="color: #339933; font-weight: bold;">&lt;|&gt;</span> wsp <span style="color: #339933; font-weight: bold;">&lt;|&gt;</span> lf <span style="color: #339933; font-weight: bold;">&lt;|&gt;</span> cr <span style="color: #339933; font-weight: bold;">&lt;|&gt;</span> obsNoWsCtl <span style="color: #339933; font-weight: bold;">&lt;|&gt;</span> nullChar<span style="color: green;">&#41;</span>
&nbsp;
cfws <span style="color: #339933; font-weight: bold;">=</span> simply <span style="color: #339933; font-weight: bold;">$</span> many <span style="color: green;">&#40;</span>comment <span style="color: #339933; font-weight: bold;">&lt;|&gt;</span> fws<span style="color: green;">&#41;</span>
fws <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>many1 wsp <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> optional <span style="color: green;">&#40;</span>crlf <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> many1 wsp<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
	<span style="color: #339933; font-weight: bold;">&lt;|&gt;</span> <span style="color: green;">&#40;</span>many1 <span style="color: green;">&#40;</span>crlf <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> many1 wsp<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> <span style="font-weight: bold;">return</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
&nbsp;
comment <span style="color: #339933; font-weight: bold;">=</span> simply <span style="color: #339933; font-weight: bold;">$</span> between <span style="color: green;">&#40;</span>char '<span style="color: green;">&#40;</span>'<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>char '<span style="color: green;">&#41;</span>'<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$</span>
	many <span style="color: green;">&#40;</span>commentContent <span style="color: #339933; font-weight: bold;">&lt;|&gt;</span> fws<span style="color: green;">&#41;</span>
commentContent <span style="color: #339933; font-weight: bold;">=</span> commentText <span style="color: #339933; font-weight: bold;">&lt;|&gt;</span> quotedPair <span style="color: #339933; font-weight: bold;">&lt;|&gt;</span> comment
commentText <span style="color: #339933; font-weight: bold;">=</span> ranges <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span>33<span style="color: #339933; font-weight: bold;">..</span>39<span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#91;</span>42<span style="color: #339933; font-weight: bold;">..</span>91<span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#91;</span>93<span style="color: #339933; font-weight: bold;">..</span>126<span style="color: green;">&#93;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">&lt;|&gt;</span> obsNoWsCtl
&nbsp;
nullChar <span style="color: #339933; font-weight: bold;">=</span> simply <span style="color: #339933; font-weight: bold;">$</span> char '\<span style="color: red;">0</span>'
wsp <span style="color: #339933; font-weight: bold;">=</span> simply <span style="color: #339933; font-weight: bold;">$</span> oneOf <span style="background-color: #3cb371;">&quot; <span style="background-color: #3cb371; font-weight: bold;">\t</span>&quot;</span>
cr <span style="color: #339933; font-weight: bold;">=</span> simply <span style="color: #339933; font-weight: bold;">$</span> char '\r'
lf <span style="color: #339933; font-weight: bold;">=</span> simply <span style="color: #339933; font-weight: bold;">$</span> char '\n'
crlf <span style="color: #339933; font-weight: bold;">=</span> simply <span style="color: #339933; font-weight: bold;">$</span> cr <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> lf
vchar <span style="color: #339933; font-weight: bold;">=</span> ranges <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span>0x21<span style="color: #339933; font-weight: bold;">..</span>0x7e<span style="color: green;">&#93;</span><span style="color: green;">&#93;</span>
obsNoWsCtl <span style="color: #339933; font-weight: bold;">=</span> ranges <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span>1<span style="color: #339933; font-weight: bold;">..</span>8<span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#91;</span><span style="color: red;">11</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">12</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#91;</span>14<span style="color: #339933; font-weight: bold;">..</span>31<span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#91;</span><span style="color: red;">127</span><span style="color: green;">&#93;</span><span style="color: green;">&#93;</span>
ranges <span style="color: #339933; font-weight: bold;">=</span> simply <span style="color: #339933; font-weight: bold;">.</span> oneOf <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">map</span> chr <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">concat</span></pre></div></div>

<p>This now passes all of Dominic Sayer&#8217;s tests that it is meant to—the domain validation used in Dominic Sayer&#8217;s tests is more strict than RFC5322 specifies. Expect this to change!</p>
<p>For those who’d like to know, email addresses that now parse that didn’t before include the often-used (‘|’ is merely to indicate the end of whitespace):</p>
<pre>I.                        |
 am.                  |
 a.      |
 nice.|
 guy@(yeah)you.com</pre>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/email-address-validation-simpler-faster-more-correct/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Properly validating e-mail addresses (or converting EBNF to Parsec)</title>
		<link>http://porg.es/blog/properly-validating-e-mail-addresses</link>
		<comments>http://porg.es/blog/properly-validating-e-mail-addresses#comments</comments>
		<pubDate>Sun, 08 Mar 2009 12:49:52 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[music]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[parsec]]></category>
		<category><![CDATA[rfc]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=307</guid>
		<description><![CDATA[Update: See the better code in the next post. In recent times there have been several calls for websites to properly validate email addresses. Invariably, the compiled regex from Perl’s RFC822 is pasted up as The Way To Do It. The problem with this is (as the source code from the Perl module notes) is [...]]]></description>
			<content:encoded><![CDATA[<p><i>Update</i>: See the better code in <a href="http://porg.es/blog/email-address-validation-simpler-faster-more-correct">the next post</a>.</p>
<p>In recent times there have been <a href="http://www.reddit.com/r/programming/search?q=email+valid">several calls for websites to properly validate email addresses</a>. Invariably, the compiled <a href="http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html">regex from Perl’s RFC822</a> is pasted up as The Way To Do It. The problem with this is (as the source code from the Perl module notes) is that email addresses <em>cannot</em> be validated by a simple regex (due to requiring parenthesis-matching). The Perl code addresses this by first stripping out all comments and then parsing via regex.</p>
<p>With this in mind, I thought that implementing the Addr-Spec specification from <a href="http://tools.ietf.org/html/rfc5322#section-3.4.1">RFC 5322</a> (only released less than 6 months ago) might be a good test of the Haskell library Parsec. So, without further ado I went ahead and translated the EBNF from RFC 5322 directly into Parsec.</p>
<p>The mapping is something like this:</p>
<dl>
<dt>juxtaposition</dt>
<dd><code>&gt;&gt;</code></dd>
<dt>/</dt>
<dd><code>&lt;|&gt;</code></dd>
<dt>*</dt>
<dd><code>many</code></dd>
<dt>1*</dt>
<dd><code>many1</code></dd>
<dt>[]</dt>
<dd><code>optional</code></dd>
</dl>
<p>Here is the result:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> Text.<span style="color: black;">Parsec</span>
<span style="color: #ff7700;font-weight:bold;">import</span> Text.<span style="color: black;">Parsec</span>.<span style="color: black;">Char</span>
<span style="color: #ff7700;font-weight:bold;">import</span> Data.<span style="color: black;">Char</span> <span style="color: black;">&#40;</span><span style="color: #008000;">chr</span><span style="color: black;">&#41;</span>
&nbsp;
isValid :: String -<span style="color: #66cc66;">&gt;</span> Bool
isValid x = let result = valid x <span style="color: #ff7700;font-weight:bold;">in</span>
	either <span style="color: black;">&#40;</span>const <span style="color: #008000;">False</span><span style="color: black;">&#41;</span> <span style="color: black;">&#40;</span>const <span style="color: #008000;">True</span><span style="color: black;">&#41;</span> result
&nbsp;
valid :: String -<span style="color: #66cc66;">&gt;</span> Either ParseError <span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
valid x = parse addrSpec <span style="color: #483d8b;">&quot;&quot;</span> x
&nbsp;
ignore x = x <span style="color: #66cc66;">&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
addrSpec = localPart <span style="color: #66cc66;">&gt;&gt;</span> char <span style="color: #483d8b;">'@'</span> <span style="color: #66cc66;">&gt;&gt;</span> domain <span style="color: #66cc66;">&gt;&gt;</span> eof
&nbsp;
localPart = dotAtom <span style="color: #66cc66;">&lt;</span>|<span style="color: #66cc66;">&gt;</span> quotedString <span style="color: #66cc66;">&lt;</span>|<span style="color: #66cc66;">&gt;</span> obsLocalPart <span style="color: #66cc66;">&lt;?&gt;</span> <span style="color: #483d8b;">&quot;local part&quot;</span>
domain = dotAtom <span style="color: #66cc66;">&lt;</span>|<span style="color: #66cc66;">&gt;</span> domainLiteral <span style="color: #66cc66;">&lt;</span>|<span style="color: #66cc66;">&gt;</span> obsDomain <span style="color: #66cc66;">&lt;?&gt;</span> <span style="color: #483d8b;">&quot;domain&quot;</span>
&nbsp;
domainLiteral = optional cfws <span style="color: #66cc66;">&gt;&gt;</span> char <span style="color: #483d8b;">'['</span> <span style="color: #66cc66;">&gt;&gt;</span>
		many <span style="color: black;">&#40;</span> optional fws <span style="color: #66cc66;">&gt;&gt;</span> dtext<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;&gt;</span>
		optional fws  <span style="color: #66cc66;">&gt;&gt;</span> char <span style="color: #483d8b;">']'</span> <span style="color: #66cc66;">&gt;&gt;</span> optional cfws
		<span style="color: #66cc66;">&lt;?&gt;</span> <span style="color: #483d8b;">&quot;domain literal&quot;</span>
&nbsp;
ranges = oneOf . <span style="color: #008000;">map</span> <span style="color: #008000;">chr</span> . <span style="color: black;">concat</span>
vchar = ranges <span style="color: black;">&#91;</span><span style="color: black;">&#91;</span>0x21..0x7E<span style="color: black;">&#93;</span><span style="color: black;">&#93;</span> -- <span style="color: #ff7700;font-weight:bold;">from</span> Backus-Naur RFC
dtext = ranges <span style="color: black;">&#91;</span><span style="color: black;">&#91;</span>33..90<span style="color: black;">&#93;</span>,<span style="color: black;">&#91;</span>94..126<span style="color: black;">&#93;</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">&lt;</span>|<span style="color: #66cc66;">&gt;</span> obsDtext
qtext = ranges <span style="color: black;">&#91;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">33</span><span style="color: black;">&#93;</span>,<span style="color: black;">&#91;</span>35..91<span style="color: black;">&#93;</span>,<span style="color: black;">&#91;</span>93..126<span style="color: black;">&#93;</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">&lt;</span>|<span style="color: #66cc66;">&gt;</span> obsQtext
atext = alphaNum <span style="color: #66cc66;">&lt;</span>|<span style="color: #66cc66;">&gt;</span> oneOf <span style="color: #483d8b;">&quot;!#$%&amp;'*+-/=?^_`{|}~&quot;</span>
ctext = ranges <span style="color: black;">&#91;</span><span style="color: black;">&#91;</span>33..39<span style="color: black;">&#93;</span>,<span style="color: black;">&#91;</span>42..91<span style="color: black;">&#93;</span>,<span style="color: black;">&#91;</span>93..126<span style="color: black;">&#93;</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">&lt;</span>|<span style="color: #66cc66;">&gt;</span> obsCtext
wsp = char <span style="color: #483d8b;">' '</span>
	<span style="color: #66cc66;">&lt;</span>|<span style="color: #66cc66;">&gt;</span> char <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\t</span>'</span>
	<span style="color: #66cc66;">&lt;?&gt;</span> <span style="color: #483d8b;">&quot;space or tab&quot;</span>
&nbsp;
cr = char <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\r</span>'</span> <span style="color: #66cc66;">&lt;?&gt;</span> <span style="color: #483d8b;">&quot;carriage return&quot;</span>
lf = char <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span> <span style="color: #66cc66;">&lt;?&gt;</span> <span style="color: #483d8b;">&quot;line feed&quot;</span>
crlf = cr <span style="color: #66cc66;">&gt;&gt;</span> lf <span style="color: #66cc66;">&lt;?&gt;</span> <span style="color: #483d8b;">&quot;CRLF line ending&quot;</span>
&nbsp;
-- <span style="color: #808080; font-style: italic;"># modification: added try</span>
cfws = <span style="color: #ff7700;font-weight:bold;">try</span> <span style="color: black;">&#40;</span>many1 <span style="color: black;">&#40;</span>optional fws <span style="color: #66cc66;">&gt;&gt;</span> comment<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;&gt;</span> optional fws<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span>|<span style="color: #66cc66;">&gt;</span> ignore fws
-- <span style="color: #808080; font-style: italic;"># modification from RFC: adding try because of overlap</span>
fws = <span style="color: #ff7700;font-weight:bold;">try</span> <span style="color: black;">&#40;</span>optional <span style="color: black;">&#40;</span>many wsp <span style="color: #66cc66;">&gt;&gt;</span> crlf<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;&gt;</span> many1 wsp<span style="color: black;">&#41;</span>
	<span style="color: #66cc66;">&lt;</span>|<span style="color: #66cc66;">&gt;</span> many1 wsp
	<span style="color: #66cc66;">&lt;</span>|<span style="color: #66cc66;">&gt;</span> obsFws
&nbsp;
-- <span style="color: #808080; font-style: italic;"># modification: added try</span>
comment = between <span style="color: black;">&#40;</span>char <span style="color: #483d8b;">'('</span><span style="color: black;">&#41;</span> <span style="color: black;">&#40;</span>char <span style="color: #483d8b;">')'</span><span style="color: black;">&#41;</span> <span style="color: black;">&#40;</span>many <span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">try</span> <span style="color: black;">&#40;</span>optional fws <span style="color: #66cc66;">&gt;&gt;</span> ccontent<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;&gt;</span> optional fws<span style="color: black;">&#41;</span>
	<span style="color: #66cc66;">&lt;?&gt;</span> <span style="color: #483d8b;">&quot;comment&quot;</span>
ccontent = ignore ctext
	<span style="color: #66cc66;">&lt;</span>|<span style="color: #66cc66;">&gt;</span> ignore quotedPair
	<span style="color: #66cc66;">&lt;</span>|<span style="color: #66cc66;">&gt;</span> comment
&nbsp;
atom = optional cfws <span style="color: #66cc66;">&gt;&gt;</span> many1 atext <span style="color: #66cc66;">&gt;&gt;</span> optional cfws
dotAtomText = many1 atext <span style="color: #66cc66;">&gt;&gt;</span> many <span style="color: black;">&#40;</span>char <span style="color: #483d8b;">'.'</span> <span style="color: #66cc66;">&gt;&gt;</span> many1 atext<span style="color: black;">&#41;</span>
dotAtom = optional cfws <span style="color: #66cc66;">&gt;&gt;</span> dotAtomText <span style="color: #66cc66;">&gt;&gt;</span> optional cfws
&nbsp;
-- <span style="color: #808080; font-style: italic;"># other change from RFC -- merge prefix</span>
quotedPair = char <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\\</span>'</span> <span style="color: #66cc66;">&gt;&gt;</span> <span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>vchar <span style="color: #66cc66;">&lt;</span>|<span style="color: #66cc66;">&gt;</span> wsp<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span>|<span style="color: #66cc66;">&gt;</span> obsQp<span style="color: black;">&#41;</span>
qcontent = qtext <span style="color: #66cc66;">&lt;</span>|<span style="color: #66cc66;">&gt;</span> quotedPair
quotedString = optional cfws <span style="color: #66cc66;">&gt;&gt;</span>	char <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span> <span style="color: #66cc66;">&gt;&gt;</span> many <span style="color: black;">&#40;</span>optional fws <span style="color: #66cc66;">&gt;&gt;</span> qcontent<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;&gt;</span>
	optional fws <span style="color: #66cc66;">&gt;&gt;</span>	char <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span> <span style="color: #66cc66;">&gt;&gt;</span> optional cfws
	<span style="color: #66cc66;">&lt;?&gt;</span> <span style="color: #483d8b;">&quot;quoted string&quot;</span>
&nbsp;
-- <span style="color: #808080; font-style: italic;"># Obsolete syntax</span>
obsNoWsCtl = ranges <span style="color: black;">&#91;</span><span style="color: black;">&#91;</span>1..8<span style="color: black;">&#93;</span>,<span style="color: black;">&#91;</span>11..12<span style="color: black;">&#93;</span>,<span style="color: black;">&#91;</span>14..31<span style="color: black;">&#93;</span>,<span style="color: black;">&#91;</span><span style="color: #ff4500;">127</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span>
obsCtext = obsNoWsCtl
obsDtext = obsNoWsCtl <span style="color: #66cc66;">&lt;</span>|<span style="color: #66cc66;">&gt;</span> quotedPair
obsQtext = obsNoWsCtl
-- <span style="color: #808080; font-style: italic;"># change: see above</span>
obsQp = <span style="color: black;">&#40;</span>char <span style="color: black;">&#40;</span><span style="color: #008000;">chr</span> <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span>|<span style="color: #66cc66;">&gt;</span> obsNoWsCtl <span style="color: #66cc66;">&lt;</span>|<span style="color: #66cc66;">&gt;</span> lf <span style="color: #66cc66;">&lt;</span>|<span style="color: #66cc66;">&gt;</span> cr<span style="color: black;">&#41;</span>
obsLocalPart = word <span style="color: #66cc66;">&gt;&gt;</span> many <span style="color: black;">&#40;</span>char <span style="color: #483d8b;">'.'</span> <span style="color: #66cc66;">&gt;&gt;</span> word<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
obsDomain = atom <span style="color: #66cc66;">&gt;&gt;</span> many <span style="color: black;">&#40;</span>char <span style="color: #483d8b;">'.'</span> <span style="color: #66cc66;">&gt;&gt;</span> atom<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
obsFws = many1 wsp <span style="color: #66cc66;">&gt;&gt;</span> many <span style="color: black;">&#40;</span>crlf <span style="color: #66cc66;">&gt;&gt;</span> many1 wsp<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
word = atom <span style="color: #66cc66;">&lt;</span>|<span style="color: #66cc66;">&gt;</span> quotedString</pre></div></div>

<p><i>A note before I continue:</i> since Parsec by default does no backtracking (in order to remain efficient), there are a couple of places (two that I&#8217;ve found so far) where the original EBNF needs to be changed slightly. I have noted these in the source above. It is possible there are a couple more places that need fixing, but I haven’t run this against a large test suite yet to find them. (They are most likely to be in the ‘obsolete syntax’ section.)</p>
<p>And of course, some demonstrations (keep in mind that there is an extra level of escaping operating here&#8230; where relevant I&#8217;ve included the unescaped email address in a comment):</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">isValid <span style="color: #483d8b;">&quot;porges@example.com&quot;</span> == <span style="color: #008000;">True</span>
isValid <span style="color: #483d8b;">&quot;porges@@example.com&quot;</span> == <span style="color: #008000;">False</span>
isValid <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>porges@<span style="color: #000099; font-weight: bold;">\&quot;</span>@example.com&quot;</span> == <span style="color: #008000;">True</span> -- <span style="color: #808080; font-style: italic;"># &quot;porges@&quot;@porg.es</span>
isValid <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>por(g)es@<span style="color: #000099; font-weight: bold;">\&quot;</span>@example.com&quot;</span> == <span style="color: #008000;">True</span> -- <span style="color: #808080; font-style: italic;"># &quot;por(g)es@&quot;@porg.es</span>
isValid <span style="color: #483d8b;">&quot;porges(comment)@example.com&quot;</span> == <span style="color: #008000;">True</span>
isValid <span style="color: #483d8b;">&quot;porges(comme(nests)nt)@example.com&quot;</span> == <span style="color: #008000;">True</span>
isValid <span style="color: #483d8b;">&quot;porges(comme(nests)nt())@example.com&quot;</span> == <span style="color: #008000;">True</span>
isValid <span style="color: #483d8b;">&quot;porges(()comme(nests)nt())@example.com&quot;</span> == <span style="color: #008000;">True</span>
isValid <span style="color: #483d8b;">&quot;()porges(()comme(nests)nt())@example.com&quot;</span> == <span style="color: #008000;">True</span>
isValid <span style="color: #483d8b;">&quot;((lol)porges(()comme(nests)nt())@example.com&quot;</span> == <span style="color: #008000;">False</span>
isValid <span style="color: #483d8b;">&quot;((lol))porges(()comme(nests)nt())@example.com&quot;</span> == <span style="color: #008000;">True</span>
isValid <span style="color: #483d8b;">&quot;(lol))porges(()comme(nests)nt())@example.com&quot;</span> == <span style="color: #008000;">False</span>
isValid <span style="color: #483d8b;">&quot;((lol))porges(()comme(nests)nt())@example.com&quot;</span> == <span style="color: #008000;">True</span>
isValid <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>s<span style="color: #000099; font-weight: bold;">\\</span><span style="color: #000099; font-weight: bold;">\0</span><span style="color: #000099; font-weight: bold;">\&quot;</span>@example.com&quot;</span> == <span style="color: #008000;">True</span>
-- <span style="color: #808080; font-style: italic;"># &quot;s\NUL&quot;@example.com, where NUL is actually the</span>
-- <span style="color: #808080; font-style: italic;"># null character! Yep, can't strlen() on email addresses...</span></pre></div></div>

<p>I managed to find <a href="http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx">this post on email addresses</a> by Phil Haack which has the following tests:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>Abc<span style="color: #000099; font-weight: bold;">\\</span>@def<span style="color: #000099; font-weight: bold;">\&quot;</span>@example.com&quot;</span>,<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>Fred Bloggs<span style="color: #000099; font-weight: bold;">\&quot;</span>@example.com&quot;</span>,<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>Joe<span style="color: #000099; font-weight: bold;">\\</span>Blow<span style="color: #000099; font-weight: bold;">\&quot;</span>@example.com&quot;</span>,<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>Abc@def<span style="color: #000099; font-weight: bold;">\&quot;</span>@example.com&quot;</span>,<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;customer/department=shipping@example.com&quot;</span>,<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;$A12345@example.com&quot;</span>,<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;!def!xyz%abc@example.com&quot;</span>,<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;_somename@example.com&quot;</span>,<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;NotAnEmail&quot;</span>,<span style="color: #008000;">False</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;@NotAnEmail&quot;</span>,<span style="color: #008000;">False</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>test<span style="color: #000099; font-weight: bold;">\\</span><span style="color: #000099; font-weight: bold;">\\</span>blah<span style="color: #000099; font-weight: bold;">\&quot;</span>@example.com&quot;</span>,<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>test<span style="color: #000099; font-weight: bold;">\\</span>blah<span style="color: #000099; font-weight: bold;">\&quot;</span>@example.com&quot;</span>,<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>,
-- <span style="color: #808080; font-style: italic;"># Phil gets false for this, which I think is wrong</span>
-- <span style="color: #808080; font-style: italic;"># (Dominic Sayers notes the same at the end of the comment thread)</span>
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>test<span style="color: #000099; font-weight: bold;">\\</span><span style="color: #000099; font-weight: bold;">\r</span>blah<span style="color: #000099; font-weight: bold;">\&quot;</span>@example.com&quot;</span>,<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>test<span style="color: #000099; font-weight: bold;">\r</span>blah<span style="color: #000099; font-weight: bold;">\&quot;</span>@example.com&quot;</span>,<span style="color: #008000;">False</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>test<span style="color: #000099; font-weight: bold;">\\</span><span style="color: #000099; font-weight: bold;">\&quot;</span>blah<span style="color: #000099; font-weight: bold;">\&quot;</span>@example.com&quot;</span>,<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>test<span style="color: #000099; font-weight: bold;">\&quot;</span>blah<span style="color: #000099; font-weight: bold;">\&quot;</span>@example.com&quot;</span>,<span style="color: #008000;">False</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;customer/department@example.com&quot;</span>,<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;$A12345@example.com&quot;</span>,<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;!def!xyz%abc@example.com&quot;</span>,<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;_Yosemite.Sam@example.com&quot;</span>,<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;~@example.com&quot;</span>,<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;.wooly@example.com&quot;</span>,<span style="color: #008000;">False</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;wo..oly@example.com&quot;</span>,<span style="color: #008000;">False</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;pootietang.@example.com&quot;</span>,<span style="color: #008000;">False</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;.@example.com&quot;</span>,<span style="color: #008000;">False</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>Austin@Powers<span style="color: #000099; font-weight: bold;">\&quot;</span>@example.com&quot;</span>,<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Ima.Fool@example.com&quot;</span>,<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>Ima.Fool<span style="color: #000099; font-weight: bold;">\&quot;</span>@example.com&quot;</span>,<span style="color: #008000;">False</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>Ima Fool<span style="color: #000099; font-weight: bold;">\&quot;</span>@example.com&quot;</span>,<span style="color: #008000;">False</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Ima Fool@example.com&quot;</span>,<span style="color: #008000;">False</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span></pre></div></div>

<p>Next job is to test it against <a href="http://www.dominicsayers.com/isemail/">this batch&#8230;</a></p>
<p><i>Update:</i> Yep, fails in several areas with the obsolete syntax. I’ve fixed one above. (Note that I’m not concerned with the failures in the domain part of the address, as the RFC5322 EBNF for this is more liberal than the tests require.)</p>
<p>Might have to refactor the syntax&#8230; there is a large overlap with the obsolete syntax. (Or just use ‘try’, but that’s not so efficient.)</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/properly-validating-e-mail-addresses/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Parametrizing Monoids and Monads</title>
		<link>http://porg.es/blog/parametrizing-monoids-and-monads</link>
		<comments>http://porg.es/blog/parametrizing-monoids-and-monads#comments</comments>
		<pubDate>Mon, 16 Feb 2009 05:10:19 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[arity]]></category>
		<category><![CDATA[category]]></category>
		<category><![CDATA[criticism]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[indexed]]></category>
		<category><![CDATA[monad]]></category>
		<category><![CDATA[monads]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=284</guid>
		<description><![CDATA[Dan Piponi’s latest post “Beyond Monads” has prompted some wonderment (and forehead-slapping). For example: How did we all miss that before? —Peaker The answer (of course!) is that while we might have, they didn&#8217;t. For example, Edward Kmett’s category-extras package has had Control.Monad.Indexed available for use in Haskell since early last year, while the concept [...]]]></description>
			<content:encoded><![CDATA[<p>Dan Piponi’s latest post “<a href="http://blog.sigfpe.com/2009/02/beyond-monads.html">Beyond Monads</a>” has prompted some wonderment (and forehead-slapping). For example:</p>
<blockquote><p>How did we all miss that before?<img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_smile.gif" alt="" /></p>
<p style="text-align:right">—<cite><a href="http://www.reddit.com/r/haskell/comments/7xi0r/a_neighborhood_of_infinity_beyond_monads/c07ohx9">Peaker</a></cite></p></blockquote>
<p>The answer (of course!) is that while <em>we</em> might have, <em>they</em> didn&#8217;t. <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_smile.gif" alt="" /> For example, Edward Kmett’s <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/category-extras-0.53.5">category-extras</a> package has had <a href="http://hackage.haskell.org/packages/archive/category-extras/0.53.5/doc/html/Control-Monad-Indexed.html">Control.Monad.Indexed</a> available for use in Haskell since early last year, while the concept for its implementation in Haskell has been around since at least 2005 (see the paper “<a href="http://crab.rutgers.edu/~pjohann/f14-ghani.pdf">Monadic Augment and Generalised Short Cut Fusion</a>”). Needless to say, Oleg has <a href="http://okmij.org/ftp/Computation/monads.html#param-monad">been there</a> too.</p>
<p>For me, this prompted a vaguely-related revelation. Say that we have a Category class:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">class</span> Category <span style="color: green;">&#40;</span>⤳<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
	<span style="font-weight: bold;">id</span> ∷ a ⤳ a
	<span style="color: green;">&#40;</span>∘<span style="color: green;">&#41;</span> ∷ <span style="color: green;">&#40;</span>b ⤳ c<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>a ⤳ b<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>a ⤳ c<span style="color: green;">&#41;</span></pre></div></div>

<p>I had recently been wondering how categories other than (→), such as graphs (and even the trivial category) fit into this model. Now that I have figured it out, the answer is simple; it is a matter of arity. Graphs and other datastructure-related categories don’t have the extra type arguments (unless you’re working in a dependently-typed language), and so can’t fit into this. What we want to do is <em>fake</em> the extra arguments, by having some extra ones that we can just ignore (the name, FF, is short for FakeFake, i.e. two fake arguments).</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">newtype</span> FF f a b <span style="color: #339933; font-weight: bold;">=</span> FF <span style="color: green;">&#123;</span> unFF <span style="color: #339933; font-weight: bold;">::</span> f <span style="color: green;">&#125;</span></pre></div></div>

<p>Now we can define the trivial category!</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">class</span> Category <span style="color: green;">&#40;</span>FF <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
	<span style="font-weight: bold;">id</span> <span style="color: #339933; font-weight: bold;">=</span> FF <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
	f ∘ g <span style="color: #339933; font-weight: bold;">=</span> FF <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span></pre></div></div>

<p>Viewed in this light, it is obvious why (as Dan notes in the original post) Monoids give rise to Monads and Categories give rise to ParametrizedMonads, as we have:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">class</span> Monoid m ⇒ Category <span style="color: green;">&#40;</span>FF m<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
	<span style="font-weight: bold;">id</span> <span style="color: #339933; font-weight: bold;">=</span> FF mempty
	<span style="color: green;">&#40;</span>FF f<span style="color: green;">&#41;</span> ∘ <span style="color: green;">&#40;</span>FF g<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> FF <span style="color: green;">&#40;</span>mappend f g<span style="color: green;">&#41;</span></pre></div></div>

<p>Next, we want to write implementations for all our old Monads in the new PMonad class. We might do it like this (with a new newtype for the different arguments of this datatype, RealFakeFakeReal):</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">newtype</span> RFFR f x y a <span style="color: #339933; font-weight: bold;">=</span> RFFR <span style="color: green;">&#123;</span> unRFFR ∷ <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span> <span style="color: green;">&#125;</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">class</span> PMonad m <span style="color: #06c; font-weight: bold;">where</span>
	<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">&gt;&gt;=</span><span style="color: green;">&#41;</span> ∷ m s1 s2 a → <span style="color: green;">&#40;</span>a → m s2 s3 b<span style="color: green;">&#41;</span> → m s1 s3 b
	<span style="font-weight: bold;">return</span> ∷ a → m s s a
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>Old<span style="color: #339933; font-weight: bold;">.</span><span style="color: #cccc00; font-weight: bold;">Monad</span> m<span style="color: green;">&#41;</span> ⇒ PMonad <span style="color: green;">&#40;</span>RFFR m<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
	<span style="color: green;">&#40;</span>RFFR x<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> f <span style="color: #339933; font-weight: bold;">=</span> RFFR <span style="color: green;">&#40;</span>x Old<span style="color: #339933; font-weight: bold;">.&gt;&gt;=</span> unRFFR ∘ f<span style="color: green;">&#41;</span>
	<span style="font-weight: bold;">return</span> x <span style="color: #339933; font-weight: bold;">=</span> RFFR <span style="color: green;">&#40;</span>Old<span style="color: #339933; font-weight: bold;">.</span><span style="font-weight: bold;">return</span> x<span style="color: green;">&#41;</span></pre></div></div>

<p>There is a big issue that this highlights. It is <em>hard</em> to work with types of different arities in Haskell. (This is noted in the ‘<a href="http://haskell.org/haskellwiki/Quantified_contexts">quantified contexts</a>’ proposal.)</p>
<p>As an example, since we now know that every Monoid is a Category, we might think to drop Monoids and just use Categories everywhere (I should note that this is actually a silly idea <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_razz.gif" alt="" />!). Unfortunately, we can’t just do this as we’ll have to use <code>FF</code> wrappers/unwrappers everywhere:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">x <span style="color: #339933; font-weight: bold;">=</span> unFF <span style="color: #339933; font-weight: bold;">$</span> FF <span style="color: green;">&#91;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">3</span><span style="color: green;">&#93;</span> ∘ FF <span style="color: green;">&#91;</span><span style="color: red;">4</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">5</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">6</span><span style="color: green;">&#93;</span></pre></div></div>

<p>I have not yet found a workaround for this. Help would be nice <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_smile.gif" alt="" /></p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/parametrizing-monoids-and-monads/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Refactoring in Haskell: Adding an Argument</title>
		<link>http://porg.es/blog/refactoring-in-haskell-adding-an-argument</link>
		<comments>http://porg.es/blog/refactoring-in-haskell-adding-an-argument#comments</comments>
		<pubDate>Wed, 10 Dec 2008 03:44:47 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[short]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=258</guid>
		<description><![CDATA[Just a small tip on this: When you add an argument to a function that already exists you should check the existing usage of the function. Say you have this: f x y z = ... &#8230; and you want: f x y z w = ... First of all you should check the contexts [...]]]></description>
			<content:encoded><![CDATA[<p>Just a small tip on this: When you add an argument to a function that already exists you should check the existing usage of the function. Say you have this:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">f x y z <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #339933; font-weight: bold;">...</span></pre></div></div>

<p>&#8230; and you want:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">f x y z w <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #339933; font-weight: bold;">...</span></pre></div></div>

<p>First of all you should check the contexts where <code>f</code> is used. For example, if <code>f</code> is quite often used as an argument to <code>map</code> (<code>fmap</code>, <code>&lt;$&gt;</code>):</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">elsewhere xs <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">map</span> <span style="color: green;">&#40;</span>f a b<span style="color: green;">&#41;</span> xs</pre></div></div>

<p>If you naïvely add <code>w</code> to the end of the argument list you&#8217;ll end up with this instead:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">elsewhere xs <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">map</span> <span style="color: green;">&#40;</span>\x<span style="color: #339933; font-weight: bold;">-&gt;</span> f a b x c<span style="color: green;">&#41;</span> xs</pre></div></div>

<p>On the other hand, if you take a look at the usage of <code>f</code> you can decide that because <code>f</code> is often used to <code>map</code> over things, you can insert the additional parameter <em>before</em> the last, and end up with nicer code:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">elsewhere xs <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">map</span> <span style="color: green;">&#40;</span>f a b c<span style="color: green;">&#41;</span> xs</pre></div></div>

<p>You may even discover that the existing <code>f</code> is used in a lot of lambdas that could be avoided by rearranging its arguments!</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/refactoring-in-haskell-adding-an-argument/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generating x86 assembly with Haskell</title>
		<link>http://porg.es/blog/generating-x86-assembly-with-haskell</link>
		<comments>http://porg.es/blog/generating-x86-assembly-with-haskell#comments</comments>
		<pubDate>Mon, 03 Nov 2008 12:51:14 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[asm]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=236</guid>
		<description><![CDATA[A quick and dirty example: data Exp = Lit Int -- literal value &#124; Call String Exp -- calling a function &#124; Bin BinOp Exp Exp -- binary operations &#124; Param -- the parameter deriving &#40;Show,Eq&#41; &#160; data Function = Func String Exp -- a function has a name and expression data BinOp = Add [...]]]></description>
			<content:encoded><![CDATA[<p>A quick and dirty example:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">data</span> Exp <span style="color: #339933; font-weight: bold;">=</span> Lit <span style="color: #cccc00; font-weight: bold;">Int</span> <span style="color: #5d478b; font-style: italic;">-- literal value</span>
	<span style="color: #339933; font-weight: bold;">|</span> Call <span style="color: #cccc00; font-weight: bold;">String</span> Exp <span style="color: #5d478b; font-style: italic;">-- calling a function</span>
	<span style="color: #339933; font-weight: bold;">|</span> Bin BinOp Exp Exp <span style="color: #5d478b; font-style: italic;">-- binary operations</span>
	<span style="color: #339933; font-weight: bold;">|</span> Param <span style="color: #5d478b; font-style: italic;">-- the parameter</span>
	<span style="color: #06c; font-weight: bold;">deriving</span> <span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Show</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: #cccc00; font-weight: bold;">Eq</span><span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">data</span> Function <span style="color: #339933; font-weight: bold;">=</span> Func <span style="color: #cccc00; font-weight: bold;">String</span> Exp <span style="color: #5d478b; font-style: italic;">-- a function has a name and expression</span>
<span style="color: #06c; font-weight: bold;">data</span> BinOp <span style="color: #339933; font-weight: bold;">=</span> Add <span style="color: #339933; font-weight: bold;">|</span> Subtract <span style="color: #339933; font-weight: bold;">|</span> Multiply <span style="color: #339933; font-weight: bold;">|</span> Divide <span style="color: #06c; font-weight: bold;">deriving</span><span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Show</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: #cccc00; font-weight: bold;">Eq</span><span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- an instance to allow us to write nicer definitions</span>
<span style="color: #06c; font-weight: bold;">instance</span> <span style="color: #cccc00; font-weight: bold;">Num</span> Exp <span style="color: #06c; font-weight: bold;">where</span>
	x <span style="color: #339933; font-weight: bold;">*</span> y <span style="color: #339933; font-weight: bold;">=</span> Bin Multiply x y
	x <span style="color: #339933; font-weight: bold;">-</span> y <span style="color: #339933; font-weight: bold;">=</span> Bin Subtract x y
	x <span style="color: #339933; font-weight: bold;">+</span> y <span style="color: #339933; font-weight: bold;">=</span> Bin Add x y
	<span style="font-weight: bold;">fromInteger</span> x <span style="color: #339933; font-weight: bold;">=</span> Lit <span style="color: green;">&#40;</span><span style="font-weight: bold;">fromInteger</span> x<span style="color: green;">&#41;</span>
&nbsp;
outFunc <span style="color: green;">&#40;</span>Func name it<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> name <span style="color: #339933; font-weight: bold;">++</span> <span style="color: #5d478b; font-style: italic;">-- output function name</span>
	<span style="background-color: #3cb371;">&quot;:<span style="background-color: #3cb371; font-weight: bold;">\n</span>push ebp<span style="background-color: #3cb371; font-weight: bold;">\n</span>mov ebp,esp<span style="background-color: #3cb371; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933; font-weight: bold;">++</span> <span style="color: #5d478b; font-style: italic;">-- save old stack frame</span>
	output it <span style="color: #339933; font-weight: bold;">++</span> <span style="color: #5d478b; font-style: italic;">-- output function</span>
	<span style="background-color: #3cb371;">&quot;pop eax<span style="background-color: #3cb371; font-weight: bold;">\n</span>pop ebp<span style="background-color: #3cb371; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933; font-weight: bold;">++</span> <span style="color: #5d478b; font-style: italic;">-- restore old stack frame</span>
	<span style="background-color: #3cb371;">&quot;ret 4<span style="background-color: #3cb371; font-weight: bold;">\n</span><span style="background-color: #3cb371; font-weight: bold;">\n</span>&quot;</span> <span style="color: #5d478b; font-style: italic;">-- remove argument from stack and return</span>
&nbsp;
output <span style="color: green;">&#40;</span>Bin op l r<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> outBin op l r <span style="color: #5d478b; font-style: italic;">-- binary operations</span>
output <span style="color: green;">&#40;</span>Lit x<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="background-color: #3cb371;">&quot;push dword &quot;</span> <span style="color: #339933; font-weight: bold;">++</span> <span style="font-weight: bold;">show</span> x <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;<span style="background-color: #3cb371; font-weight: bold;">\n</span>&quot;</span> <span style="color: #5d478b; font-style: italic;">-- push literal</span>
output <span style="color: green;">&#40;</span>Param<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="background-color: #3cb371;">&quot;push dword [ebp+8]<span style="background-color: #3cb371; font-weight: bold;">\n</span>&quot;</span> <span style="color: #5d478b; font-style: italic;">-- push parameter</span>
output <span style="color: green;">&#40;</span>Call name with<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> output with <span style="color: #339933; font-weight: bold;">++</span>
	<span style="background-color: #3cb371;">&quot;call &quot;</span> <span style="color: #339933; font-weight: bold;">++</span> name <span style="color: #339933; font-weight: bold;">++</span>
	<span style="background-color: #3cb371;">&quot;<span style="background-color: #3cb371; font-weight: bold;">\n</span>push eax<span style="background-color: #3cb371; font-weight: bold;">\n</span>&quot;</span> <span style="color: #5d478b; font-style: italic;">-- put return value onto stack</span>
&nbsp;
outBin Add <span style="color: #339933; font-weight: bold;">=</span> outBinGen <span style="background-color: #3cb371;">&quot;add&quot;</span>
outBin Subtract <span style="color: #339933; font-weight: bold;">=</span> outBinGen <span style="background-color: #3cb371;">&quot;sub&quot;</span>
outBin Multiply <span style="color: #339933; font-weight: bold;">=</span> outBinGen <span style="background-color: #3cb371;">&quot;imul&quot;</span>
outBin Divide <span style="color: #339933; font-weight: bold;">=</span> outBinGen <span style="background-color: #3cb371;">&quot;idiv&quot;</span>
&nbsp;
outBinGen op l r <span style="color: #339933; font-weight: bold;">=</span> output l <span style="color: #339933; font-weight: bold;">++</span> output r <span style="color: #339933; font-weight: bold;">++</span>
	<span style="background-color: #3cb371;">&quot;pop ebx<span style="background-color: #3cb371; font-weight: bold;">\n</span>pop eax<span style="background-color: #3cb371; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933; font-weight: bold;">++</span> op <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot; eax,ebx<span style="background-color: #3cb371; font-weight: bold;">\n</span>push eax<span style="background-color: #3cb371; font-weight: bold;">\n</span>&quot;</span></pre></div></div>

<p>And an example of using it:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">main <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">putStr</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">concatMap</span> outFunc <span style="color: green;">&#91;</span>double<span style="color: #339933; font-weight: bold;">,</span>quad<span style="color: green;">&#93;</span>
double <span style="color: #339933; font-weight: bold;">=</span> Func <span style="background-color: #3cb371;">&quot;double&quot;</span> <span style="color: green;">&#40;</span>Param <span style="color: #339933; font-weight: bold;">*</span> <span style="color: red;">2</span><span style="color: green;">&#41;</span>
quad <span style="color: #339933; font-weight: bold;">=</span> Func <span style="background-color: #3cb371;">&quot;quad&quot;</span> <span style="color: green;">&#40;</span>Call <span style="background-color: #3cb371;">&quot;double&quot;</span> <span style="color: green;">&#40;</span><span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">*</span>Param<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre></div></div>

<p>This example outputs:</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;">double<span style="color: #339933;">:</span>
	<span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #00007f;">ebp</span>
	<span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">ebp</span><span style="color: #339933;">,</span><span style="color: #00007f;">esp</span>
	<span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #000000; font-weight: bold;">dword</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ebp</span><span style="color: #339933;">+</span><span style="color: #0000ff;">8</span><span style="color: #009900; font-weight: bold;">&#93;</span>
	<span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #000000; font-weight: bold;">dword</span> <span style="color: #0000ff;">2</span>
	<span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #00007f;">ebx</span>
	<span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #00007f;">eax</span>
	<span style="color: #00007f; font-weight: bold;">imul</span> <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span><span style="color: #00007f;">ebx</span>
	<span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #00007f;">eax</span>
	<span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #00007f;">eax</span>
	<span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #00007f;">ebp</span>
	<span style="color: #00007f; font-weight: bold;">ret</span> <span style="color: #0000ff;">4</span>
&nbsp;
quad<span style="color: #339933;">:</span>
	<span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #00007f;">ebp</span>
	<span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">ebp</span><span style="color: #339933;">,</span><span style="color: #00007f;">esp</span>
	<span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #000000; font-weight: bold;">dword</span> <span style="color: #0000ff;">2</span>
	<span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #000000; font-weight: bold;">dword</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ebp</span><span style="color: #339933;">+</span><span style="color: #0000ff;">8</span><span style="color: #009900; font-weight: bold;">&#93;</span>
	<span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #00007f;">ebx</span>
	<span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #00007f;">eax</span>
	<span style="color: #00007f; font-weight: bold;">imul</span> <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span><span style="color: #00007f;">ebx</span>
	<span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #00007f;">eax</span>
	<span style="color: #00007f; font-weight: bold;">call</span> double
	<span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #00007f;">eax</span>
	<span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #00007f;">eax</span>
	<span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #00007f;">ebp</span>
	<span style="color: #00007f; font-weight: bold;">ret</span> <span style="color: #0000ff;">4</span></pre></div></div>

<p>Not the most efficient code in the world <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_wink.gif" alt="" /> The calling convention is somewhat ad-hoc; the callee must remove its arguments from the stack and returns its value in <code>eax</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/generating-x86-assembly-with-haskell/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple socket programming with Haskell</title>
		<link>http://porg.es/blog/simple-socket-programming-with-haskell</link>
		<comments>http://porg.es/blog/simple-socket-programming-with-haskell#comments</comments>
		<pubDate>Wed, 15 Oct 2008 01:54:35 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[university]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[program]]></category>
		<category><![CDATA[sockets]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=214</guid>
		<description><![CDATA[Here is a simple program showing socket programming with Haskell, created for a University assignment. It allows half-duplex (one way at a time) communication between two (or fewer ) computers: import Network import Data.Char &#40;toLower&#41; import Text.Regex.Posix &#40;&#40;=~&#41;&#41; import System.IO &#40;hGetLine,hClose,hPutStrLn,hSetBuffering,BufferMode&#40;..&#41;,Handle,stdout&#41; &#160; port = 8001 -- a nice port number &#160; main = withSocketsDo $ [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a simple program showing socket programming with Haskell, created for a University assignment. It allows half-duplex (one way at a time) communication between two (or fewer <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_biggrin.gif" alt="" />) computers:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">import</span> Network
<span style="color: #06c; font-weight: bold;">import</span> Data<span style="color: #339933; font-weight: bold;">.</span><span style="color: #cccc00; font-weight: bold;">Char</span> <span style="color: green;">&#40;</span>toLower<span style="color: green;">&#41;</span>
<span style="color: #06c; font-weight: bold;">import</span> Text<span style="color: #339933; font-weight: bold;">.</span>Regex<span style="color: #339933; font-weight: bold;">.</span>Posix <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">=~</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
<span style="color: #06c; font-weight: bold;">import</span> System<span style="color: #339933; font-weight: bold;">.</span><span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span>hGetLine<span style="color: #339933; font-weight: bold;">,</span>hClose<span style="color: #339933; font-weight: bold;">,</span>hPutStrLn<span style="color: #339933; font-weight: bold;">,</span>hSetBuffering<span style="color: #339933; font-weight: bold;">,</span>BufferMode<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">..</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span>Handle<span style="color: #339933; font-weight: bold;">,</span>stdout<span style="color: green;">&#41;</span>
&nbsp;
port <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">8001</span> <span style="color: #5d478b; font-style: italic;">-- a nice port number</span>
&nbsp;
main <span style="color: #339933; font-weight: bold;">=</span> withSocketsDo <span style="color: #339933; font-weight: bold;">$</span> <span style="color: #06c; font-weight: bold;">do</span> <span style="color: #5d478b; font-style: italic;">-- enable sockets under windows</span>
	<span style="font-weight: bold;">putStrLn</span> <span style="background-color: #3cb371;">&quot;Welcome to One-Way Chat version 1.0&quot;</span>
	hSetBuffering stdout NoBuffering <span style="color: #5d478b; font-style: italic;">-- fix buffering under windows</span>
	input <span style="color: #339933; font-weight: bold;">&lt;-</span> untilM   <span style="color: #5d478b; font-style: italic;">-- get input of 'c' or 's'</span>
		<span style="color: green;">&#40;</span>\x <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">not</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">null</span> x<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&amp;&amp;</span> toLower <span style="color: green;">&#40;</span><span style="font-weight: bold;">head</span> x<span style="color: green;">&#41;</span> `<span style="font-weight: bold;">elem</span>` <span style="background-color: #3cb371;">&quot;cs&quot;</span><span style="color: green;">&#41;</span>
		<span style="color: green;">&#40;</span><span style="font-weight: bold;">putStr</span> <span style="background-color: #3cb371;">&quot;Client or server? &quot;</span> <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> <span style="font-weight: bold;">getLine</span><span style="color: green;">&#41;</span>
	<span style="color: green;">&#40;</span><span style="color: #06c; font-weight: bold;">if</span> <span style="color: green;">&#40;</span>toLower <span style="color: green;">&#40;</span><span style="font-weight: bold;">head</span> input<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">==</span> 'c'<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">then</span> client <span style="color: #06c; font-weight: bold;">else</span> server<span style="color: green;">&#41;</span> <span style="color: #5d478b; font-style: italic;">-- start the client or server</span>
		`<span style="font-weight: bold;">catch</span>` <span style="color: green;">&#40;</span><span style="font-weight: bold;">const</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">putStrLn</span> <span style="background-color: #3cb371;">&quot;Connection forced closed.&quot;</span><span style="color: green;">&#41;</span>	
	<span style="font-weight: bold;">putStrLn</span> <span style="background-color: #3cb371;">&quot;Connection closed.&quot;</span>
	<span style="font-weight: bold;">putStrLn</span> <span style="background-color: #3cb371;">&quot;Thanks for using One-Way Chat!&quot;</span> <span style="color: #5d478b; font-style: italic;">-- all done</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- reads in an ip address</span>
readIp <span style="color: #339933; font-weight: bold;">=</span> untilM	<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">=~</span> <span style="background-color: #3cb371;">&quot;[0-9]{1,3}<span style="background-color: #3cb371; font-weight: bold;">\\</span>.[0-9]{1,3}<span style="background-color: #3cb371; font-weight: bold;">\\</span>.[0-9]{1,3}<span style="background-color: #3cb371; font-weight: bold;">\\</span>.[0-9]{1,3}&quot;</span><span style="color: green;">&#41;</span>
	<span style="color: green;">&#40;</span><span style="font-weight: bold;">putStr</span> <span style="background-color: #3cb371;">&quot;Enter IP address: &quot;</span> <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> <span style="font-weight: bold;">getLine</span><span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- monadic `until`</span>
untilM p x <span style="color: #339933; font-weight: bold;">=</span> x <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> <span style="color: green;">&#40;</span>\y <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #06c; font-weight: bold;">if</span> p y <span style="color: #06c; font-weight: bold;">then</span> <span style="font-weight: bold;">return</span> y <span style="color: #06c; font-weight: bold;">else</span> untilM p x<span style="color: green;">&#41;</span> 
<span style="color: #5d478b; font-style: italic;">-- repeats two actions until either returns true</span>
while2 x y <span style="color: #339933; font-weight: bold;">=</span> ifM x <span style="color: green;">&#40;</span><span style="font-weight: bold;">return</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$</span> ifM y <span style="color: green;">&#40;</span><span style="font-weight: bold;">return</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$</span> while2 x y 
<span style="color: #5d478b; font-style: italic;">-- monadic `if`</span>
ifM p t f  <span style="color: #339933; font-weight: bold;">=</span> p <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> <span style="color: green;">&#40;</span>\p' <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #06c; font-weight: bold;">if</span> p' <span style="color: #06c; font-weight: bold;">then</span> t <span style="color: #06c; font-weight: bold;">else</span> f<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- client</span>
client <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
	ip <span style="color: #339933; font-weight: bold;">&lt;-</span> readIp
	<span style="font-weight: bold;">putStrLn</span> <span style="background-color: #3cb371;">&quot;Connecting...&quot;</span>
	h <span style="color: #339933; font-weight: bold;">&lt;-</span> connectTo ip <span style="color: green;">&#40;</span>PortNumber port<span style="color: green;">&#41;</span>
	<span style="font-weight: bold;">putStrLn</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="background-color: #3cb371;">&quot;Connected to &quot;</span> <span style="color: #339933; font-weight: bold;">++</span> ip <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;:&quot;</span> <span style="color: #339933; font-weight: bold;">++</span> <span style="font-weight: bold;">show</span> port
	hSetBuffering h LineBuffering
	while2 <span style="color: green;">&#40;</span>send h<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>receive h<span style="color: green;">&#41;</span>
	hClose h
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- server</span>
server <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
	sock <span style="color: #339933; font-weight: bold;">&lt;-</span> listenOn <span style="color: green;">&#40;</span>PortNumber port<span style="color: green;">&#41;</span>
	<span style="font-weight: bold;">putStrLn</span> <span style="background-color: #3cb371;">&quot;Awaiting connection.&quot;</span>
	<span style="color: green;">&#40;</span>h<span style="color: #339933; font-weight: bold;">,</span>host<span style="color: #339933; font-weight: bold;">,</span>port<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&lt;-</span> accept sock
	<span style="font-weight: bold;">putStrLn</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="background-color: #3cb371;">&quot;Received connection from &quot;</span> <span style="color: #339933; font-weight: bold;">++</span> host <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;:&quot;</span> <span style="color: #339933; font-weight: bold;">++</span> <span style="font-weight: bold;">show</span> port
	hSetBuffering h LineBuffering
	while2 <span style="color: green;">&#40;</span>receive h<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>send h<span style="color: green;">&#41;</span>
	hClose h
	sClose sock
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- sending</span>
send h <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
	<span style="font-weight: bold;">putStr</span> <span style="background-color: #3cb371;">&quot;Send (empty to close): &quot;</span>
	input <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="font-weight: bold;">getLine</span>
	hPutStrLn h input
	<span style="font-weight: bold;">return</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">null</span> input
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- receiving</span>
receive h <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
	<span style="font-weight: bold;">putStr</span> <span style="background-color: #3cb371;">&quot;Receiving: &quot;</span>
	input <span style="color: #339933; font-weight: bold;">&lt;-</span> hGetLine h
	<span style="font-weight: bold;">putStrLn</span> input
	<span style="font-weight: bold;">return</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">null</span> input</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/simple-socket-programming-with-haskell/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Sorted sums of a sorted list</title>
		<link>http://porg.es/blog/sorted-sums-of-a-sorted-list</link>
		<comments>http://porg.es/blog/sorted-sums-of-a-sorted-list#comments</comments>
		<pubDate>Wed, 24 Sep 2008 13:15:44 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=179</guid>
		<description><![CDATA[This is a post in response to this question over at the newly-spawned StackOverflow. Essentially: If we have a sorted list of numbers, how can we efficiently get a sorted list of the sums of every pair of numbers in that list? My initial reply is also there. I decided that, since I have a [...]]]></description>
			<content:encoded><![CDATA[<p>This is a post in response to <a href="http://stackoverflow.com/questions/826/efficiently-get-sorted-sums-of-a-sorted-list">this question</a> over at the newly-spawned StackOverflow. Essentially:</p>
<blockquote><p>If we have a sorted list of numbers, how can we efficiently get a sorted list of the sums of every pair of numbers in that list?</p></blockquote>
<p>My <a href="http://stackoverflow.com/questions/826/efficiently-get-sorted-sums-of-a-sorted-list#97294">initial reply</a> is also there. I decided that, since I have a moment of spare time I should implement this. I&#8217;m semi-reprinting it here, but you might want to read it anyway.</p>
<h3>Algorithm</h3>
<p>The idea is that if you have a sorted list <img src='/blog/wp-content/plugins/latexrender/pictures/9dd4e461268c8034f5c8564e155c67a6.gif' title='x' alt='x' align=absmiddle>, you can write out the sums as a matrix <img src='/blog/wp-content/plugins/latexrender/pictures/69691c7bdcc3ce6d5d8a1361f22d04ac.gif' title='M' alt='M' align=absmiddle> where <img src='/blog/wp-content/plugins/latexrender/pictures/b0c44ac6bfdf643aa5e4853a6fc83165.gif' title='M_{i,j} = x_{i} + x_{j}' alt='M_{i,j} = x_{i} + x_{j}' align=absmiddle>:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/bfeed1df527ce88e56c1a20302314b82.gif' title='\begin{tabular}{r|rrrrrr}&amp; 1 &amp; 4     &amp; 5 &amp; 6 &amp; 8 &amp; 9 \\ \hline 1 &amp; 2 &amp; 5 &amp; 6   &amp; 7 &amp; 9 &amp; 10 \\ 4 &amp;   &amp; 8 &amp; 9  &amp; 10 &amp; 12 &amp; 13 \\ 5 &amp;   &amp;   &amp; 10 &amp; 11 &amp; 13 &amp; 14 \\ 6 &amp;   &amp;   &amp;     &amp; 12 &amp; 14 &amp; 15 \\ 8 &amp;   &amp;   &amp;    &amp;      &amp; 16 &amp; 17 \\ 9 &amp;   &amp;   &amp;    &amp;      &amp;     &amp; 18 \end{tabular}' alt='\begin{tabular}{r|rrrrrr}&amp; 1 &amp; 4     &amp; 5 &amp; 6 &amp; 8 &amp; 9 \\ \hline 1 &amp; 2 &amp; 5 &amp; 6   &amp; 7 &amp; 9 &amp; 10 \\ 4 &amp;   &amp; 8 &amp; 9  &amp; 10 &amp; 12 &amp; 13 \\ 5 &amp;   &amp;   &amp; 10 &amp; 11 &amp; 13 &amp; 14 \\ 6 &amp;   &amp;   &amp;     &amp; 12 &amp; 14 &amp; 15 \\ 8 &amp;   &amp;   &amp;    &amp;      &amp; 16 &amp; 17 \\ 9 &amp;   &amp;   &amp;    &amp;      &amp;     &amp; 18 \end{tabular}' align=absmiddle></p>
<p>I&#8217;ve left out the lower half of the matrix as it is clearly <a href="http://en.wikipedia.org/wiki/Symmetric_matrix">symmetric</a> (<img src='/blog/wp-content/plugins/latexrender/pictures/74f9fe14e5e37b0ce08bd2fc8316023d.gif' title='M_{i,j} = M_{j,i}' alt='M_{i,j} = M_{j,i}' align=absmiddle>).</p>
<p>You can see that clearly <img src='/blog/wp-content/plugins/latexrender/pictures/e27bd51faf9a2b0d4ebdaf8f1957f2e7.gif' title='M_{i,j} \le M_{(i+1),j}' alt='M_{i,j} \le M_{(i+1),j}' align=absmiddle> and <img src='/blog/wp-content/plugins/latexrender/pictures/eb41f9b14d1df7f4e2431c404d842b77.gif' title='M_{i,j} \le M_{i,(j+1)}' alt='M_{i,j} \le M_{i,(j+1)}' align=absmiddle> (since if <img src='/blog/wp-content/plugins/latexrender/pictures/39b35140ebab892c71c2f542d84048a3.gif' title='y \le z' alt='y \le z' align=absmiddle> then <img src='/blog/wp-content/plugins/latexrender/pictures/3e8939e039c7a2a5f22732336caea3ba.gif' title='x + y \le x + z' alt='x + y \le x + z' align=absmiddle>), so we only have to examine the &#8220;top-left corners&#8221; of the matrix. As an example, we would first examine <img src='/blog/wp-content/plugins/latexrender/pictures/1e9000084ba639866e251c9b7a04aa96.gif' title='M_{0,0}' alt='M_{0,0}' align=absmiddle>:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/d3db88ce0aa3e3feb7cfd860924aee9f.gif' title='\begin{tabular}{r|rrrrrr}&amp; 1 &amp; 4     &amp; 5 &amp; 6 &amp; 8 &amp; 9 \\ \hline 1 &amp; \fbox{2} &amp; 5 &amp; 6   &amp; 7 &amp; 9 &amp; 10 \\ 4 &amp;   &amp; 8 &amp; 9  &amp; 10 &amp; 12 &amp; 13 \\ 5 &amp;   &amp;   &amp; 10 &amp; 11 &amp; 13 &amp; 14 \\ 6 &amp;   &amp;   &amp;     &amp; 12 &amp; 14 &amp; 15 \\ 8 &amp;   &amp;   &amp;    &amp;      &amp; 16 &amp; 17 \\ 9 &amp;   &amp;   &amp;    &amp;      &amp;     &amp; 18 \end{tabular}' alt='\begin{tabular}{r|rrrrrr}&amp; 1 &amp; 4     &amp; 5 &amp; 6 &amp; 8 &amp; 9 \\ \hline 1 &amp; \fbox{2} &amp; 5 &amp; 6   &amp; 7 &amp; 9 &amp; 10 \\ 4 &amp;   &amp; 8 &amp; 9  &amp; 10 &amp; 12 &amp; 13 \\ 5 &amp;   &amp;   &amp; 10 &amp; 11 &amp; 13 &amp; 14 \\ 6 &amp;   &amp;   &amp;     &amp; 12 &amp; 14 &amp; 15 \\ 8 &amp;   &amp;   &amp;    &amp;      &amp; 16 &amp; 17 \\ 9 &amp;   &amp;   &amp;    &amp;      &amp;     &amp; 18 \end{tabular}' align=absmiddle></p>
<p>Then accept it since it is the only option, and move on to the next top-left corner, which again is a single choice:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/b6b68467ac7abbbeaa1405ea6d6766dc.gif' title='\begin{tabular}{r|rrrrrr}&amp; 1 &amp; 4     &amp; 5 &amp; 6 &amp; 8 &amp; 9 \\ \hline 1 &amp;  &amp; \fbox{5} &amp; 6   &amp; 7 &amp; 9 &amp; 10 \\ 4 &amp;   &amp; 8 &amp; 9  &amp; 10 &amp; 12 &amp; 13 \\ 5 &amp;   &amp;   &amp; 10 &amp; 11 &amp; 13 &amp; 14 \\ 6 &amp;   &amp;   &amp;     &amp; 12 &amp; 14 &amp; 15 \\ 8 &amp;   &amp;   &amp;    &amp;      &amp; 16 &amp; 17 \\ 9 &amp;   &amp;   &amp;    &amp;      &amp;     &amp; 18 \end{tabular}' alt='\begin{tabular}{r|rrrrrr}&amp; 1 &amp; 4     &amp; 5 &amp; 6 &amp; 8 &amp; 9 \\ \hline 1 &amp;  &amp; \fbox{5} &amp; 6   &amp; 7 &amp; 9 &amp; 10 \\ 4 &amp;   &amp; 8 &amp; 9  &amp; 10 &amp; 12 &amp; 13 \\ 5 &amp;   &amp;   &amp; 10 &amp; 11 &amp; 13 &amp; 14 \\ 6 &amp;   &amp;   &amp;     &amp; 12 &amp; 14 &amp; 15 \\ 8 &amp;   &amp;   &amp;    &amp;      &amp; 16 &amp; 17 \\ 9 &amp;   &amp;   &amp;    &amp;      &amp;     &amp; 18 \end{tabular}' align=absmiddle></p>
<p>Then we get two options:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/1d20e00803ba6d64eb19217b213321b1.gif' title='\begin{tabular}{r|rrrrrr}&amp; 1 &amp; 4     &amp; 5 &amp; 6 &amp; 8 &amp; 9 \\ \hline 1 &amp;  &amp;   &amp; \fbox{6}   &amp; 7 &amp; 9 &amp; 10 \\ 4 &amp;   &amp; \fbox{8} &amp; 9  &amp; 10 &amp; 12 &amp; 13 \\ 5 &amp;   &amp;   &amp; 10 &amp; 11 &amp; 13 &amp; 14 \\ 6 &amp;   &amp;   &amp;     &amp; 12 &amp; 14 &amp; 15 \\ 8 &amp;   &amp;   &amp;    &amp;      &amp; 16 &amp; 17 \\ 9 &amp;   &amp;   &amp;    &amp;      &amp;     &amp; 18 \end{tabular}' alt='\begin{tabular}{r|rrrrrr}&amp; 1 &amp; 4     &amp; 5 &amp; 6 &amp; 8 &amp; 9 \\ \hline 1 &amp;  &amp;   &amp; \fbox{6}   &amp; 7 &amp; 9 &amp; 10 \\ 4 &amp;   &amp; \fbox{8} &amp; 9  &amp; 10 &amp; 12 &amp; 13 \\ 5 &amp;   &amp;   &amp; 10 &amp; 11 &amp; 13 &amp; 14 \\ 6 &amp;   &amp;   &amp;     &amp; 12 &amp; 14 &amp; 15 \\ 8 &amp;   &amp;   &amp;    &amp;      &amp; 16 &amp; 17 \\ 9 &amp;   &amp;   &amp;    &amp;      &amp;     &amp; 18 \end{tabular}' align=absmiddle></p>
<p>And so on:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/39c57ddcf506d38d0a08e86320ecbc13.gif' title='\begin{tabular}{r|rrrrrr}&amp; 1 &amp; 4     &amp; 5 &amp; 6 &amp; 8 &amp; 9 \\ \hline 1 &amp;  &amp;   &amp;   &amp; \fbox{7} &amp; 9 &amp; 10 \\ 4 &amp;   &amp; \fbox{8} &amp; 9  &amp; 10 &amp; 12 &amp; 13 \\ 5 &amp;   &amp;   &amp; 10 &amp; 11 &amp; 13 &amp; 14 \\ 6 &amp;   &amp;   &amp;     &amp; 12 &amp; 14 &amp; 15 \\ 8 &amp;   &amp;   &amp;    &amp;      &amp; 16 &amp; 17 \\ 9 &amp;   &amp;   &amp;    &amp;      &amp;     &amp; 18 \end{tabular}' alt='\begin{tabular}{r|rrrrrr}&amp; 1 &amp; 4     &amp; 5 &amp; 6 &amp; 8 &amp; 9 \\ \hline 1 &amp;  &amp;   &amp;   &amp; \fbox{7} &amp; 9 &amp; 10 \\ 4 &amp;   &amp; \fbox{8} &amp; 9  &amp; 10 &amp; 12 &amp; 13 \\ 5 &amp;   &amp;   &amp; 10 &amp; 11 &amp; 13 &amp; 14 \\ 6 &amp;   &amp;   &amp;     &amp; 12 &amp; 14 &amp; 15 \\ 8 &amp;   &amp;   &amp;    &amp;      &amp; 16 &amp; 17 \\ 9 &amp;   &amp;   &amp;    &amp;      &amp;     &amp; 18 \end{tabular}' align=absmiddle><br />
<img src='/blog/wp-content/plugins/latexrender/pictures/b3e4dfe6936701dfe6ecb0902eece2d4.gif' title='\begin{tabular}{r|rrrrrr}&amp; 1 &amp; 4     &amp; 5 &amp; 6 &amp; 8 &amp; 9 \\ \hline 1 &amp;  &amp;   &amp;   &amp;   &amp; \fbox{9} &amp; 10 \\ 4 &amp;   &amp; \fbox{8} &amp; 9  &amp; 10 &amp; 12 &amp; 13 \\ 5 &amp;   &amp;   &amp; 10 &amp; 11 &amp; 13 &amp; 14 \\ 6 &amp;   &amp;   &amp;     &amp; 12 &amp; 14 &amp; 15 \\ 8 &amp;   &amp;   &amp;    &amp;      &amp; 16 &amp; 17 \\ 9 &amp;   &amp;   &amp;    &amp;      &amp;     &amp; 18 \end{tabular}' alt='\begin{tabular}{r|rrrrrr}&amp; 1 &amp; 4     &amp; 5 &amp; 6 &amp; 8 &amp; 9 \\ \hline 1 &amp;  &amp;   &amp;   &amp;   &amp; \fbox{9} &amp; 10 \\ 4 &amp;   &amp; \fbox{8} &amp; 9  &amp; 10 &amp; 12 &amp; 13 \\ 5 &amp;   &amp;   &amp; 10 &amp; 11 &amp; 13 &amp; 14 \\ 6 &amp;   &amp;   &amp;     &amp; 12 &amp; 14 &amp; 15 \\ 8 &amp;   &amp;   &amp;    &amp;      &amp; 16 &amp; 17 \\ 9 &amp;   &amp;   &amp;    &amp;      &amp;     &amp; 18 \end{tabular}' align=absmiddle><br />
<img src='/blog/wp-content/plugins/latexrender/pictures/03dbe01c1fcdb245ad61e9ba84053758.gif' title='\begin{tabular}{r|rrrrrr}&amp; 1 &amp; 4     &amp; 5 &amp; 6 &amp; 8 &amp; 9 \\ \hline 1 &amp;  &amp;   &amp;   &amp;   &amp; \fbox{9} &amp; 10 \\ 4 &amp;   &amp;  &amp; \fbox{9}  &amp; 10 &amp; 12 &amp; 13 \\ 5 &amp;   &amp;   &amp; 10 &amp; 11 &amp; 13 &amp; 14 \\ 6 &amp;   &amp;   &amp;     &amp; 12 &amp; 14 &amp; 15 \\ 8 &amp;   &amp;   &amp;    &amp;      &amp; 16 &amp; 17 \\ 9 &amp;   &amp;   &amp;    &amp;      &amp;     &amp; 18 \end{tabular}' alt='\begin{tabular}{r|rrrrrr}&amp; 1 &amp; 4     &amp; 5 &amp; 6 &amp; 8 &amp; 9 \\ \hline 1 &amp;  &amp;   &amp;   &amp;   &amp; \fbox{9} &amp; 10 \\ 4 &amp;   &amp;  &amp; \fbox{9}  &amp; 10 &amp; 12 &amp; 13 \\ 5 &amp;   &amp;   &amp; 10 &amp; 11 &amp; 13 &amp; 14 \\ 6 &amp;   &amp;   &amp;     &amp; 12 &amp; 14 &amp; 15 \\ 8 &amp;   &amp;   &amp;    &amp;      &amp; 16 &amp; 17 \\ 9 &amp;   &amp;   &amp;    &amp;      &amp;     &amp; 18 \end{tabular}' align=absmiddle></p>
<p>I think you get the picture <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_smile.gif" alt="" /></p>
<p>One of the points of this post is that I horribly misimplemented this and have spent many wasted hours today trying to figure out why it wasn&#8217;t working. Instead of the last sequence of diagrams, my algorithm was doing the following:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/39c57ddcf506d38d0a08e86320ecbc13.gif' title='\begin{tabular}{r|rrrrrr}&amp; 1 &amp; 4     &amp; 5 &amp; 6 &amp; 8 &amp; 9 \\ \hline 1 &amp;  &amp;   &amp;   &amp; \fbox{7} &amp; 9 &amp; 10 \\ 4 &amp;   &amp; \fbox{8} &amp; 9  &amp; 10 &amp; 12 &amp; 13 \\ 5 &amp;   &amp;   &amp; 10 &amp; 11 &amp; 13 &amp; 14 \\ 6 &amp;   &amp;   &amp;     &amp; 12 &amp; 14 &amp; 15 \\ 8 &amp;   &amp;   &amp;    &amp;      &amp; 16 &amp; 17 \\ 9 &amp;   &amp;   &amp;    &amp;      &amp;     &amp; 18 \end{tabular}' alt='\begin{tabular}{r|rrrrrr}&amp; 1 &amp; 4     &amp; 5 &amp; 6 &amp; 8 &amp; 9 \\ \hline 1 &amp;  &amp;   &amp;   &amp; \fbox{7} &amp; 9 &amp; 10 \\ 4 &amp;   &amp; \fbox{8} &amp; 9  &amp; 10 &amp; 12 &amp; 13 \\ 5 &amp;   &amp;   &amp; 10 &amp; 11 &amp; 13 &amp; 14 \\ 6 &amp;   &amp;   &amp;     &amp; 12 &amp; 14 &amp; 15 \\ 8 &amp;   &amp;   &amp;    &amp;      &amp; 16 &amp; 17 \\ 9 &amp;   &amp;   &amp;    &amp;      &amp;     &amp; 18 \end{tabular}' align=absmiddle><br />
<img src='/blog/wp-content/plugins/latexrender/pictures/5cfbbdfc15bb330b9f61f48a5f9e1d15.gif' title='\begin{tabular}{r|rrrrrr}&amp; 1 &amp; 4     &amp; 5 &amp; 6 &amp; 8 &amp; 9 \\ \hline 1 &amp;  &amp;   &amp;   &amp;   &amp; \fbox{9} &amp; 10 \\ 4 &amp;   &amp; \fbox{8} &amp; 9  &amp; \fbox{10} &amp; 12 &amp; 13 \\ 5 &amp;   &amp;   &amp; 10 &amp; 11 &amp; 13 &amp; 14 \\ 6 &amp;   &amp;   &amp;     &amp; 12 &amp; 14 &amp; 15 \\ 8 &amp;   &amp;   &amp;    &amp;      &amp; 16 &amp; 17 \\ 9 &amp;   &amp;   &amp;    &amp;      &amp;     &amp; 18 \end{tabular}' alt='\begin{tabular}{r|rrrrrr}&amp; 1 &amp; 4     &amp; 5 &amp; 6 &amp; 8 &amp; 9 \\ \hline 1 &amp;  &amp;   &amp;   &amp;   &amp; \fbox{9} &amp; 10 \\ 4 &amp;   &amp; \fbox{8} &amp; 9  &amp; \fbox{10} &amp; 12 &amp; 13 \\ 5 &amp;   &amp;   &amp; 10 &amp; 11 &amp; 13 &amp; 14 \\ 6 &amp;   &amp;   &amp;     &amp; 12 &amp; 14 &amp; 15 \\ 8 &amp;   &amp;   &amp;    &amp;      &amp; 16 &amp; 17 \\ 9 &amp;   &amp;   &amp;    &amp;      &amp;     &amp; 18 \end{tabular}' align=absmiddle></p>
<p>That is, every time a corner was eliminated it was adding both neighbours to the list of corners even if there was a corner further left on the row below it. As such, the following naïve algorithm was beating it by a factor of 4:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">sortedSumSortedList xs <span style="color: #339933; font-weight: bold;">=</span> sort <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">concat</span> <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span>x<span style="color: #339933; font-weight: bold;">+</span>y<span style="color: #339933; font-weight: bold;">|</span>y<span style="color: #339933; font-weight: bold;">&lt;-</span><span style="font-weight: bold;">drop</span> nx xs<span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">|</span>x<span style="color: #339933; font-weight: bold;">&lt;-</span>xs<span style="color: #339933; font-weight: bold;">|</span>nx<span style="color: #339933; font-weight: bold;">&lt;-</span><span style="color: green;">&#91;</span>0<span style="color: #339933; font-weight: bold;">..</span><span style="color: green;">&#93;</span><span style="color: green;">&#93;</span></pre></div></div>

<h3>Implementation</h3>
<p>A preliminary: you&#8217;ll need the following imports for the code below.</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">import</span> Data<span style="color: #339933; font-weight: bold;">.</span>Array<span style="color: #339933; font-weight: bold;">.</span>Unboxed
<span style="color: #06c; font-weight: bold;">import</span> Control<span style="color: #339933; font-weight: bold;">.</span><span style="color: #cccc00; font-weight: bold;">Monad</span>
<span style="color: #06c; font-weight: bold;">import</span> <span style="color: #06c; font-weight: bold;">qualified</span> Data<span style="color: #339933; font-weight: bold;">.</span>IntMap <span style="color: #06c; font-weight: bold;">as</span> IntMap
<span style="color: #06c; font-weight: bold;">import</span> Test<span style="color: #339933; font-weight: bold;">.</span>QuickCheck
<span style="color: #06c; font-weight: bold;">import</span> Data<span style="color: #339933; font-weight: bold;">.</span>List
<span style="color: #06c; font-weight: bold;">import</span> Data<span style="color: #339933; font-weight: bold;">.</span>Time<span style="color: #339933; font-weight: bold;">.</span>Clock</pre></div></div>

<p>First of all we need the sums of the list with itself:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">sumLists <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> UArray <span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#41;</span> <span style="color: #cccc00; font-weight: bold;">Int</span>
sumLists xs ys <span style="color: #339933; font-weight: bold;">=</span> array <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">0</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span><span style="font-weight: bold;">length</span> xs<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span><span style="font-weight: bold;">length</span> ys<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$</span> sumLists' xs ys <span style="color: red;">0</span>
	<span style="color: #06c; font-weight: bold;">where</span>
	sumLists' <span style="color: #339933; font-weight: bold;">_</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
	sumLists' xs' <span style="color: green;">&#40;</span>y:ys'<span style="color: green;">&#41;</span> ny <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>nx<span style="color: #339933; font-weight: bold;">,</span>ny<span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span>x<span style="color: #339933; font-weight: bold;">+</span>y<span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">|</span>x<span style="color: #339933; font-weight: bold;">&lt;-</span><span style="font-weight: bold;">drop</span> ny xs'<span style="color: #339933; font-weight: bold;">|</span>nx<span style="color: #339933; font-weight: bold;">&lt;-</span><span style="color: green;">&#91;</span>ny<span style="color: #339933; font-weight: bold;">..</span><span style="color: green;">&#93;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">++</span> sumLists' xs ys' <span style="color: green;">&#40;</span>ny<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: green;">&#41;</span></pre></div></div>

<p>This does basically the same thing as the code above (<code>concat [[x+y|y<-drop nx xs]|x<-xs|nx<-[0..]]</code>) except that we want an array rather than a list so that we have constant-time addressing. In Haskell an array is created by specifying the boundaries of the array and then the data for the array. Note that the bottom of the array is left empty as in the diagrams; this is fine, we can leave some data unspecified as long as we never access it.</p>
<p>And here's the workhorse. I've commented it more thoroughly so you should be able to understand what's happening.</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">orderedSumsOfOrderedList sums corners row
	<span style="color: #339933; font-weight: bold;">|</span> IntMap<span style="color: #339933; font-weight: bold;">.</span><span style="font-weight: bold;">null</span> corners <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
	<span style="color: #339933; font-weight: bold;">|</span> <span style="font-weight: bold;">otherwise</span> <span style="color: #339933; font-weight: bold;">=</span> replicate <span style="color: green;">&#40;</span><span style="font-weight: bold;">length</span> minCorners<span style="color: green;">&#41;</span> minimumValue <span style="color: #339933; font-weight: bold;">++</span> orderedSumsOfOrderedList sums corners' row'
	<span style="color: #06c; font-weight: bold;">where</span>
	<span style="color: #5d478b; font-style: italic;">-- minCorners = all corners with the same (lowest) value</span>
	<span style="color: #5d478b; font-style: italic;">-- allCorners = all other corners currently being considered (as a IntMap)</span>
	<span style="color: green;">&#40;</span>minCorners<span style="color: #339933; font-weight: bold;">,</span>allCorners<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> IntMap<span style="color: #339933; font-weight: bold;">.</span>deleteFindMin corners
	<span style="color: #5d478b; font-style: italic;">-- minimumValue = the value in the lowest corner(s)</span>
	minimumValue <span style="color: #339933; font-weight: bold;">=</span> sums <span style="color: #339933; font-weight: bold;">!</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">head</span> minCorners<span style="color: green;">&#41;</span>
&nbsp;
	<span style="color: #5d478b; font-style: italic;">-- row is incremented if any of the neighbours has moved down a row</span>
	row' <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">if</span> <span style="font-weight: bold;">any</span> <span style="color: green;">&#40;</span>\<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">_,_,</span>b<span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">-&gt;</span> b<span style="color: green;">&#41;</span> neighbours <span style="color: #06c; font-weight: bold;">then</span> row<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span> <span style="color: #06c; font-weight: bold;">else</span> row
	<span style="color: #5d478b; font-style: italic;">-- update the corners to reflect all neighbours of the just-removed corners</span>
	corners' <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">foldr</span> <span style="color: green;">&#40;</span>\<span style="color: green;">&#40;</span>key<span style="color: #339933; font-weight: bold;">,</span>value<span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">-&gt;</span> IntMap<span style="color: #339933; font-weight: bold;">.</span>insertWith <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">++</span><span style="color: green;">&#41;</span> key <span style="color: green;">&#91;</span>value<span style="color: green;">&#93;</span><span style="color: green;">&#41;</span> allCorners <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">map</span> <span style="color: green;">&#40;</span>\<span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span>y<span style="color: #339933; font-weight: bold;">,_</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span>y<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> neighbours
&nbsp;
	<span style="color: #5d478b; font-style: italic;">-- a list of values, their positions in the array, and whether to increment row</span>
	neighbours <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">concatMap</span> <span style="color: green;">&#40;</span>\p<span style="color: #339933; font-weight: bold;">-&gt;</span> goRight p <span style="color: #339933; font-weight: bold;">++</span> goDown p<span style="color: green;">&#41;</span> minCorners
		<span style="color: #06c; font-weight: bold;">where</span>
			<span style="color: #5d478b; font-style: italic;">-- try to get a neighbour further right, within bounds of array</span>
			goRight <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span>y<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span>	ifM <span style="color: green;">&#40;</span>x <span style="color: #339933; font-weight: bold;">&lt;</span> maxX<span style="color: green;">&#41;</span>                       <span style="color: green;">&#40;</span>sums <span style="color: #339933; font-weight: bold;">!</span> <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span>y<span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span>y<span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span>False<span style="color: green;">&#41;</span>
			<span style="color: #5d478b; font-style: italic;">-- try to get a neighbour further down, within bounds of array and not on the same row as another one</span>
			<span style="color: #5d478b; font-style: italic;">-- (if successful we should update row)</span>
			goDown  <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span>y<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> ifM <span style="color: green;">&#40;</span>y <span style="color: #339933; font-weight: bold;">&lt;</span> x <span style="color: #339933; font-weight: bold;">&amp;&amp;</span> y <span style="color: #339933; font-weight: bold;">&lt;</span> maxY <span style="color: #339933; font-weight: bold;">&amp;&amp;</span> y<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span> <span style="color: #339933; font-weight: bold;">&gt;</span> row<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>sums <span style="color: #339933; font-weight: bold;">!</span> <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span>y<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span>y<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span>True<span style="color: green;">&#41;</span>
			maxX <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">fst</span><span style="color: #339933; font-weight: bold;">$</span>snd<span style="color: #339933; font-weight: bold;">$</span>bounds<span style="color: #339933; font-weight: bold;">$</span>sums<span style="color: green;">&#41;</span>
			maxY <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">snd</span><span style="color: #339933; font-weight: bold;">$</span>snd<span style="color: #339933; font-weight: bold;">$</span>bounds<span style="color: #339933; font-weight: bold;">$</span>sums<span style="color: green;">&#41;</span></pre></div></div>

<p>To efficiently choose the lowest number each time, we keep track of them in something like a binary tree. In Haskell the provided implementation is <code>Data.Map</code> (or <code>Data.IntMap</code> which is specialized for integer keys).</p>
<p>The rest of what this code does is basically:</p>
<ol>
<li>Choose the lowest corner(s) from the tree of available corners.</li>
<li>Find the neighbours of these corner(s), ensuring they are valid (in this case this means checking bounds and checking that no two are on the same row&mdash;it suffices to ensure that no downwards-searching neighbours are added which are on a row less than the maximum row reached so far), and insert these into the tree.</li>
<li>Output the lowest corner(s) and recurse with the new tree and row number.</li>
</ol>
<p><code>ifM</code> is a nice little function I've had cause to use several times, and it looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">ifM <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span>MonadPlus m<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> <span style="color: #cccc00; font-weight: bold;">Bool</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> a <span style="color: #339933; font-weight: bold;">-&gt;</span> m a
ifM <span style="font-weight: bold;">pred</span> res <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">if</span> <span style="font-weight: bold;">pred</span>
		<span style="color: #06c; font-weight: bold;">then</span> <span style="font-weight: bold;">return</span> res
		<span style="color: #06c; font-weight: bold;">else</span> mzero</pre></div></div>

<p>And here's the wrapper function (osool = ‘ordered sums of ordered list’), we take in a list of <code>Int</code>s and return a list of <code>Int</code>s. Note that we do not check the precondition (that the input list is sorted).</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">osool <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#93;</span>
osool <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>
osool xs <span style="color: #339933; font-weight: bold;">=</span> orderedSumsOfOrderedList sums corners row 
	<span style="color: #06c; font-weight: bold;">where</span>
	sums <span style="color: #339933; font-weight: bold;">=</span> sumLists xs xs
	corners <span style="color: #339933; font-weight: bold;">=</span> IntMap<span style="color: #339933; font-weight: bold;">.</span>singleton <span style="color: green;">&#40;</span>sums <span style="color: #339933; font-weight: bold;">!</span> <span style="color: green;">&#40;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">0</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">0</span><span style="color: green;">&#41;</span><span style="color: green;">&#93;</span>
	row <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">0</span></pre></div></div>

<p><code>sums</code> is the sums of the list, and <code>corners</code> is the quick-lookup structure (in this case <code>IntMap</code>) which allows us to find the minimum fast. We initialize it with the value of the top-left corner of the matrix, and its coördinates. Finally, <code>row</code> is a value indicating which row we are up to. As shown above, we don't want to add any new corners which are less than this value when eliminating a square and looking for a neighbouring square in the downwards direction.</p>
<p>And finally, we can test this with the wonderful <a href="http://hackage.haskell.org/packages/archive/QuickCheck/1.1.0.0/doc/html/Test-QuickCheck.html">QuickCheck</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">checkMe <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
	time <span style="background-color: #3cb371;">&quot;osool&quot;</span> <span style="color: #339933; font-weight: bold;">$</span> quickCheck <span style="color: green;">&#40;</span>\xs <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #06c; font-weight: bold;">let</span> sxs <span style="color: #339933; font-weight: bold;">=</span> sort xs <span style="color: #06c; font-weight: bold;">in</span> <span style="font-weight: bold;">length</span> <span style="color: green;">&#40;</span>osool sxs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">==</span> num sxs<span style="color: green;">&#41;</span>
	time <span style="background-color: #3cb371;">&quot;checkSumLists&quot;</span> <span style="color: #339933; font-weight: bold;">$</span> quickCheck <span style="color: green;">&#40;</span>\xs <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #06c; font-weight: bold;">let</span> sxs <span style="color: #339933; font-weight: bold;">=</span> sort xs <span style="color: #06c; font-weight: bold;">in</span> <span style="font-weight: bold;">length</span> <span style="color: green;">&#40;</span>checkSumLists sxs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">==</span> num sxs<span style="color: green;">&#41;</span>
&nbsp;
	time <span style="background-color: #3cb371;">&quot;osool&quot;</span> <span style="color: #339933; font-weight: bold;">$</span> longCheck <span style="color: green;">&#40;</span>\xs <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #06c; font-weight: bold;">let</span> sxs <span style="color: #339933; font-weight: bold;">=</span> sort xs <span style="color: #06c; font-weight: bold;">in</span> <span style="font-weight: bold;">length</span> <span style="color: green;">&#40;</span>osool sxs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">==</span> num sxs<span style="color: green;">&#41;</span>
	time <span style="background-color: #3cb371;">&quot;checkSumLists&quot;</span> <span style="color: #339933; font-weight: bold;">$</span> longCheck <span style="color: green;">&#40;</span>\xs <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #06c; font-weight: bold;">let</span> sxs <span style="color: #339933; font-weight: bold;">=</span> sort xs <span style="color: #06c; font-weight: bold;">in</span> <span style="font-weight: bold;">length</span> <span style="color: green;">&#40;</span>checkSumLists sxs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">==</span> num sxs<span style="color: green;">&#41;</span>
&nbsp;
	time <span style="background-color: #3cb371;">&quot;areEqual&quot;</span> <span style="color: #339933; font-weight: bold;">$</span> quickCheck <span style="color: green;">&#40;</span>\xs <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #06c; font-weight: bold;">let</span> sxs <span style="color: #339933; font-weight: bold;">=</span> sort xs <span style="color: #06c; font-weight: bold;">in</span> checkSumLists sxs <span style="color: #339933; font-weight: bold;">==</span> osool sxs<span style="color: green;">&#41;</span>
	<span style="color: #06c; font-weight: bold;">where</span>
	longCheck p <span style="color: #339933; font-weight: bold;">=</span> check <span style="color: green;">&#40;</span>defaultConfig <span style="color: green;">&#123;</span> configSize <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">const</span> <span style="color: red;">1000</span><span style="color: #339933; font-weight: bold;">,</span> configMaxTest <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">500</span><span style="color: green;">&#125;</span><span style="color: green;">&#41;</span> p
	checkSumLists xs <span style="color: #339933; font-weight: bold;">=</span> sort <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">concat</span> <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span>x<span style="color: #339933; font-weight: bold;">+</span>y<span style="color: #339933; font-weight: bold;">|</span>y<span style="color: #339933; font-weight: bold;">&lt;-</span><span style="font-weight: bold;">drop</span> nx xs<span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">|</span>x<span style="color: #339933; font-weight: bold;">&lt;-</span>xs<span style="color: #339933; font-weight: bold;">|</span>nx<span style="color: #339933; font-weight: bold;">&lt;-</span><span style="color: green;">&#91;</span>0<span style="color: #339933; font-weight: bold;">..</span><span style="color: green;">&#93;</span><span style="color: green;">&#93;</span>
	num xs <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">sum</span> <span style="color: green;">&#91;</span>1<span style="color: #339933; font-weight: bold;">..</span><span style="font-weight: bold;">length</span> xs<span style="color: green;">&#93;</span>
	time s d <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
		<span style="font-weight: bold;">putStrLn</span> <span style="color: #339933; font-weight: bold;">$</span> s <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;:&quot;</span>
		t1 <span style="color: #339933; font-weight: bold;">&lt;-</span> getCurrentTime
		d
		t2 <span style="color: #339933; font-weight: bold;">&lt;-</span> getCurrentTime
		<span style="font-weight: bold;">putStrLn</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">show</span> <span style="color: #339933; font-weight: bold;">$</span> t2 `diffUTCTime` t1</pre></div></div>

<p>Results</p>
<pre>$ ./test
osool:
OK, passed 100 tests.
0.024591s
checkSumLists:
OK, passed 100 tests.
0.014857s
osool:
OK, passed 500 tests.
15.448749s
checkSumLists:
OK, passed 500 tests.
82.871982s
areEqual:
OK, passed 100 tests.
0.007773s</pre>
<p>As you can see, the wins over the naïve version are substantial.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/sorted-sums-of-a-sorted-list/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Things I&#8217;d like to see in C#: Conditional interface implementation</title>
		<link>http://porg.es/blog/things-id-like-to-see-in-c-conditional-interface-implementation</link>
		<comments>http://porg.es/blog/things-id-like-to-see-in-c-conditional-interface-implementation#comments</comments>
		<pubDate>Sat, 20 Sep 2008 02:54:29 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[idea]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=171</guid>
		<description><![CDATA[To explain this, imagine you are designing a type Wrapper&#60;T&#62;, which is just a simple wrapper around a type T. class Wrapper&#60;T&#62; &#123; T value; public Wrapper&#40;T theObject&#41; &#123; value = theObject; &#125; // Some other methods... &#125; Now, since this is a just a wrapper around some type T, we would like to implement [...]]]></description>
			<content:encoded><![CDATA[<p>To explain this, imagine you are designing a type <code>Wrapper&lt;T&gt;</code>, which is just a simple wrapper around a type <code>T</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">class</span> Wrapper<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>
<span style="color: #000000;">&#123;</span>
    T value<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">public</span> Wrapper<span style="color: #000000;">&#40;</span>T theObject<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        value <span style="color: #008000;">=</span> theObject<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #008080; font-style: italic;">// Some other methods...</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Now, since this is a just a wrapper around some type <code>T</code>, we would like to implement some simple interfaces around this object. For example, if <code>T</code> is comparable, we would like the wrapper class to implement <code>IComparable&lt;Wrapper&lt;T&gt;&gt;</code>. However this is not possible with C#. In Haskell we would have something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>Disposable a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> Disposable <span style="color: green;">&#40;</span>Wrapper a<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span> <span style="color: #339933; font-weight: bold;">...</span></pre></div></div>

<p>This says &#8220;if <code>a</code> is a type that is an instance of <code>Disposable</code>, then the type <code>Wrapper a</code> is also an instance of <code>Disposable</code>”. In C#, I&#8217;d like to see something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">class</span> Wrapper<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">:</span> IComparable<span style="color: #008000;">&lt;</span>Wrapper<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span>
        when T <span style="color: #008000;">:</span> IComparable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #008080; font-style: italic;">//this is the syntax extension</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">//...</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>And then in any methods that the interface specifies (in this case <code>int Compare(Wrapper&lt;T&gt; other)</code>), it is assumed that for any object of type <code>T</code> you have access to all the methods that <code>T</code> has when it implements <code>IComparable&lt;T&gt;</code>.</p>
<p>At the moment the best you can do is to <em>always</em> implement <code>IComparable&lt;Wrapper&lt;T&gt;&gt;</code> and just throw a runtime exception when <code>T</code> doesn&#8217;t implement <code>IComparable&lt;T&gt;</code>, which isn&#8217;t very nice.</p>
<p>A simple idea, but it would add great power to C#.</p>
<p>After some (very quick) research I haven&#8217;t found anything that suggests anyone else has attempted to get this into C#, but there is <a href="http://homepages.cwi.nl/~ralf/JavaGI/paper.pdf">JavaGI</a> (see section 2.4 for the equivalent of what this post talks about) where the authors have extended Java to deal with what they term ‘generalized interfaces’—a concept which alters Java (rather radically) to make interface implementations similar to Haskell&#8217;s typeclasses.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/things-id-like-to-see-in-c-conditional-interface-implementation/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A quine in Haskell</title>
		<link>http://porg.es/blog/a-quine-in-haskell</link>
		<comments>http://porg.es/blog/a-quine-in-haskell#comments</comments>
		<pubDate>Wed, 30 Apr 2008 12:32:04 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[quine]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=128</guid>
		<description><![CDATA[Prompted by A Scala Quine, here’s a nice one in Haskell that I haven’t seen anywhere else&#8230; main = &#40;putStr . ap &#40;++&#41; show&#41; &#34;main = (putStr . ap (++) show) &#34; Edit: I have found it somewhere else Here, to be exact.]]></description>
			<content:encoded><![CDATA[<p>Prompted by <a href="http://www.codecommit.com/blog/scala/useless-hackery-a-scala-quine">A Scala Quine</a>, here’s a nice one in Haskell that I haven’t seen anywhere else&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">main <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">putStr</span> <span style="color: #339933; font-weight: bold;">.</span> ap <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">++</span><span style="color: green;">&#41;</span> <span style="font-weight: bold;">show</span><span style="color: green;">&#41;</span> <span style="background-color: #3cb371;">&quot;main = (putStr . ap (++) show) &quot;</span></pre></div></div>

<p>Edit: I have found it somewhere else <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_razz.gif" alt="" /> <a href="http://homepage.mac.com/kpreid/quines.html">Here</a>, to be exact.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/a-quine-in-haskell/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Overengineering</title>
		<link>http://porg.es/blog/overengineering</link>
		<comments>http://porg.es/blog/overengineering#comments</comments>
		<pubDate>Wed, 27 Feb 2008 00:55:09 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[replies]]></category>
		<category><![CDATA[fizzbuzz]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[horrid]]></category>
		<category><![CDATA[humour]]></category>
		<category><![CDATA[overengineered]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://porg.es/blog/overengineering</guid>
		<description><![CDATA[Douglas, you&#8217;re not alone. import Data.List &#40;sortBy&#41; import Data.Function &#40;on&#41; import Data.Maybe &#40;mapMaybe&#41; import Control.Monad.Instances &#160; gizzabuzz pairs combiner = zipWith &#40;$&#41; &#40;cycle funcs&#41; &#91;1..&#93; where sortedPairs = sortBy &#40;compare `on` fst&#41; pairs funcs = map &#40;\n -&#62; display $ mapMaybe &#40;filterOut n&#41; sortedPairs&#41; &#91;1..foldr1 lcm $ map fst $ sortedPairs&#93; display &#91;&#93; = show [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.dougalstanton.net/blog/index.php/2008/02/26/my-shame-is-complete">Douglas</a>, you&#8217;re not alone.</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">import</span> Data<span style="color: #339933; font-weight: bold;">.</span>List <span style="color: green;">&#40;</span>sortBy<span style="color: green;">&#41;</span>
<span style="color: #06c; font-weight: bold;">import</span> Data<span style="color: #339933; font-weight: bold;">.</span>Function <span style="color: green;">&#40;</span>on<span style="color: green;">&#41;</span>
<span style="color: #06c; font-weight: bold;">import</span> Data<span style="color: #339933; font-weight: bold;">.</span><span style="color: #cccc00; font-weight: bold;">Maybe</span> <span style="color: green;">&#40;</span>mapMaybe<span style="color: green;">&#41;</span>
<span style="color: #06c; font-weight: bold;">import</span> Control<span style="color: #339933; font-weight: bold;">.</span><span style="color: #cccc00; font-weight: bold;">Monad</span><span style="color: #339933; font-weight: bold;">.</span>Instances
&nbsp;
gizzabuzz pairs combiner <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">zipWith</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">$</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">cycle</span> funcs<span style="color: green;">&#41;</span> <span style="color: green;">&#91;</span>1<span style="color: #339933; font-weight: bold;">..</span><span style="color: green;">&#93;</span>
	<span style="color: #06c; font-weight: bold;">where</span> 
	sortedPairs <span style="color: #339933; font-weight: bold;">=</span> sortBy <span style="color: green;">&#40;</span><span style="font-weight: bold;">compare</span> `on` <span style="font-weight: bold;">fst</span><span style="color: green;">&#41;</span> pairs
	funcs <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">map</span> <span style="color: green;">&#40;</span>\n <span style="color: #339933; font-weight: bold;">-&gt;</span> display <span style="color: #339933; font-weight: bold;">$</span> mapMaybe <span style="color: green;">&#40;</span>filterOut n<span style="color: green;">&#41;</span> sortedPairs<span style="color: green;">&#41;</span> <span style="color: green;">&#91;</span>1<span style="color: #339933; font-weight: bold;">..</span><span style="font-weight: bold;">foldr1</span> <span style="font-weight: bold;">lcm</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">map</span> <span style="font-weight: bold;">fst</span> <span style="color: #339933; font-weight: bold;">$</span> sortedPairs<span style="color: green;">&#93;</span>
	display <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">show</span>
	display xs <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">foldr1</span> combiner <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">sequence</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">map</span> <span style="font-weight: bold;">const</span> xs<span style="color: green;">&#41;</span>
	filterOut n <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span>y<span style="color: green;">&#41;</span>
		<span style="color: #339933; font-weight: bold;">|</span> n `<span style="font-weight: bold;">mod</span>` x <span style="color: #339933; font-weight: bold;">==</span> <span style="color: red;">0</span> <span style="color: #339933; font-weight: bold;">=</span> Just y
		<span style="color: #339933; font-weight: bold;">|</span> <span style="font-weight: bold;">otherwise</span>      <span style="color: #339933; font-weight: bold;">=</span> Nothing
&nbsp;
fizzbuzz <span style="color: #339933; font-weight: bold;">=</span> gizzabuzz <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span><span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,</span><span style="background-color: #3cb371;">&quot;Fizz&quot;</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span><span style="color: red;">5</span><span style="color: #339933; font-weight: bold;">,</span><span style="background-color: #3cb371;">&quot;Buzz&quot;</span><span style="color: green;">&#41;</span><span style="color: green;">&#93;</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">++</span><span style="color: green;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/overengineering/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Matching checklists using Haskell</title>
		<link>http://porg.es/blog/matching-checklists-using-haskell</link>
		<comments>http://porg.es/blog/matching-checklists-using-haskell#comments</comments>
		<pubDate>Wed, 23 Jan 2008 08:52:55 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[replies]]></category>
		<category><![CDATA[comparison]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Links]]></category>
		<category><![CDATA[lisp]]></category>

		<guid isPermaLink="false">http://porg.es/blog/matching-checklists-using-haskell</guid>
		<description><![CDATA[Our target for this exercise is “Things that other languages should take from Lisp”. Bignum support In Scheme and Common Lisp, by default you can&#8217;t overflow an integer&#8230; Prelude&#62; fac n = product &#91;2..n&#93; Prelude&#62; fac 100 933262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582 51185210916864000000000000000000000000 In Common Lisp, you can force your code to use fixed-size numbers (fixnums) for efficiency&#8230; Prelude&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>Our target for this exercise is “<a href="http://repinvariant.blogspot.com/2008/01/thoughts-on-lisp-things-that-other.html">Things that other languages should take from Lisp</a>”.</p>
<h3>Bignum support</h3>
<blockquote><p>In Scheme and Common Lisp, by default you can&#8217;t overflow an integer&#8230;</p>
</blockquote>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">Prelude<span style="color: #339933; font-weight: bold;">&gt;</span> fac n <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">product</span> <span style="color: green;">&#91;</span>2<span style="color: #339933; font-weight: bold;">..</span>n<span style="color: green;">&#93;</span>
Prelude<span style="color: #339933; font-weight: bold;">&gt;</span> fac <span style="color: red;">100</span>
<span style="color: red;">933262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582</span>
<span style="color: red;">51185210916864000000000000000000000000</span></pre></div></div>

<blockquote><p>In Common Lisp, you can force your code to use fixed-size numbers (fixnums) for efficiency&#8230;</p>
</blockquote>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">Prelude<span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: #06c; font-weight: bold;">let</span> fac n <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">product</span> <span style="color: green;">&#91;</span>2<span style="color: #339933; font-weight: bold;">..</span>n<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span>
Prelude<span style="color: #339933; font-weight: bold;">&gt;</span> fac <span style="color: red;">17</span>
<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">288522240</span></pre></div></div>

<blockquote><p>Ruby and Python, by default, treat language-integers as logical-integers. Java, C, C++, and Perl don&#8217;t.</p>
</blockquote>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">Prelude<span style="color: #339933; font-weight: bold;">&gt;</span> :t <span style="color: red;">15</span>
<span style="color: red;">15</span> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Num</span> t<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> t</pre></div></div>

<h3>Optional type declarations</h3>
<blockquote><p>[Common Lisp] allows, but does not require, type declarations.</p>
</blockquote>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">Prelude<span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: #06c; font-weight: bold;">let</span> doubleApply f x <span style="color: #339933; font-weight: bold;">=</span> f <span style="color: green;">&#40;</span>f x<span style="color: green;">&#41;</span>
Prelude<span style="color: #339933; font-weight: bold;">&gt;</span> :t doubleApply
doubleApply <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span>t <span style="color: #339933; font-weight: bold;">-&gt;</span> t<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> t <span style="color: #339933; font-weight: bold;">-&gt;</span> t</pre></div></div>

<blockquote><p>The compiler can also use type declarations to perform compile-time typechecking. (Sadly, it isn&#8217;t <em>required</em> to do this.)</p>
</blockquote>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">Prelude<span style="color: #339933; font-weight: bold;">&gt;</span> doubleApply <span style="color: red;">3</span> doubleApply
&nbsp;
<span style="color: #339933; font-weight: bold;">&lt;</span>interactive<span style="color: #339933; font-weight: bold;">&gt;</span>:<span style="color: red;">1</span>:<span style="color: red;">12</span>:
    No <span style="color: #06c; font-weight: bold;">instance</span> for <span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Num</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>t <span style="color: #339933; font-weight: bold;">-&gt;</span> t<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> t <span style="color: #339933; font-weight: bold;">-&gt;</span> t<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>t <span style="color: #339933; font-weight: bold;">-&gt;</span> t<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> t <span style="color: #339933; font-weight: bold;">-&gt;</span> t<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
      arising from the literal `<span style="color: red;">3</span>' at <span style="color: #339933; font-weight: bold;">&lt;</span>interactive<span style="color: #339933; font-weight: bold;">&gt;</span>:<span style="color: red;">1</span>:<span style="color: red;">12</span></pre></div></div>

<h3>Tail recursion</h3>
<blockquote><p>Proper (unbounded) tail-recursion (Scheme.)</p>
</blockquote>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">Prelude<span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: #06c; font-weight: bold;">let</span> loop <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span> <span style="color: green;">&#123;</span> <span style="font-weight: bold;">putStr</span> <span style="background-color: #3cb371;">&quot;.&quot;</span>; loop <span style="color: green;">&#125;</span>
Prelude<span style="color: #339933; font-weight: bold;">&gt;</span> loop
<span style="color: #339933; font-weight: bold;">......................................</span>
<span style="color: #339933; font-weight: bold;">......................................</span>
<span style="color: green;">&#91;</span>etc<span style="color: green;">&#93;</span>
<span style="color: #339933; font-weight: bold;">......................................</span>
<span style="color: #339933; font-weight: bold;">..........................</span>Interrupted<span style="color: #339933; font-weight: bold;">.</span></pre></div></div>

<p>(But see <a href="http://www.haskell.org/haskellwiki/Stack_overflow">the Haskell wiki</a>. Laziness introduces a small trick here.)</p>
<h3>Functions which depend upon the type of multiple arguments</h3>
<blockquote><p>Methods with multiple-dispatch (Common Lisp.)</p>
</blockquote>
<p>(This doesn’t <em>exactly</em> match conceptually because Haskell doesn’t have nominal overloading.)</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">multi Nothing Nothing <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">0</span>
multi <span style="color: green;">&#40;</span>Just x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>Just y<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> x <span style="color: #339933; font-weight: bold;">+</span> y
multi Nothing <span style="color: green;">&#40;</span>Just y<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> y
multi <span style="color: green;">&#40;</span>Just x<span style="color: green;">&#41;</span> Nothing <span style="color: #339933; font-weight: bold;">=</span> x</pre></div></div>

<h3>Fast implementations</h3>
<blockquote><p>Some Lisp implementations are fast.</p>
</blockquote>
<p><a href="http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&amp;lang=all&amp;xfullcpu=1&amp;xmem=0&amp;xloc=0&amp;binarytrees=1&amp;chameneosredux=1&amp;fannkuch=1&amp;fasta=1&amp;knucleotide=1&amp;mandelbrot=1&amp;meteor=1&amp;nbody=1&amp;nsieve=1&amp;nsievebits=1&amp;partialsums=1&amp;pidigits=1&amp;recursive=1&amp;regexdna=1&amp;revcomp=1&amp;spectralnorm=1&amp;hello=1&amp;sumcol=1&amp;threadring=1&amp;calc=Calculate">Haskell  #7 on the Great Language Shootout</a> (in terms of speed), and getting faster by the day as the backend of GHC is rewritten.</p>
<h3>Syntactic Simplicity</h3>
<blockquote><p>Syntactic simplicity (Scheme.)</p>
</blockquote>
<p><a href="http://haskell.org/onlinereport/syntax-iso.html#sect9.5">The Haskell <abbr title="Context-Free Grammar">CFG</abbr>.</a></p>
<blockquote><p>Or perhaps Python is another incarnation of syntactic simplicity.</p>
</blockquote>
<p><a href="http://haskell.org/onlinereport/lexemes.html#lexemes-layout">The Layout Rule</a></p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/matching-checklists-using-haskell/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Computing with XSLT</title>
		<link>http://porg.es/blog/computing-with-xslt</link>
		<comments>http://porg.es/blog/computing-with-xslt#comments</comments>
		<pubDate>Thu, 19 Jul 2007 02:34:18 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Cool]]></category>
		<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[XSLT]]></category>

		<guid isPermaLink="false">http://porg.es/blog/computing-with-xslt</guid>
		<description><![CDATA[Since XSLT is basically a pattern-matching functional programming language, we should be able to use it to compute. I&#8217;m going to use it to implement one of the most basic functions: Peano-style addition. First of all we have to have an idea of what the numbers look like. We want to simulate this in XSL: [...]]]></description>
			<content:encoded><![CDATA[<p>Since XSLT is basically a pattern-matching functional programming language, we should be able to use it to compute. I&#8217;m going to use it to implement one of the most basic functions: Peano-style addition.</p>
<p>First of all we have to have an idea of what the numbers look like. We want to simulate this in XSL:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">data</span> Nat <span style="color: #339933; font-weight: bold;">=</span> Zero <span style="color: #339933; font-weight: bold;">|</span> Succ Nat</pre></div></div>

<p>Using XSD we can have something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;complexType</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Nat&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;choice<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Zero&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Succ&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Nat&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/choice<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/complexType<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><small>(Note that this is not tested, and probably wrong!)</small></p>
<p>However, since I don&#8217;t have an XSLT processor on hand that can deal with XSD types, I had to drop this course of action (although I still used the introduced type for the data). The type gives data like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Succ<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;Succ<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;Zero</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/Succ<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/Succ<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>&#8230;in this case standing for the number two. Now the idea of addition on these numbers is given by a function:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">add Zero y <span style="color: #339933; font-weight: bold;">=</span> y
add <span style="color: green;">&#40;</span>Succ x<span style="color: green;">&#41;</span> y <span style="color: #339933; font-weight: bold;">=</span> Succ <span style="color: green;">&#40;</span>add x y<span style="color: green;">&#41;</span></pre></div></div>

<p>Since this function involves pattern-matching it is an ideal candidate for implementation in XSLT, and it looks something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:stylesheet</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.0&quot;</span> </span>
<span style="color: #009900;"><span style="color: #000066;">xmlns:xsl</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/XSL/Transform&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!-- apply the add function to two numbers --&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;add&quot;</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;add&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:apply-templates</span> <span style="color: #000066;">mode</span>=<span style="color: #ff0000;">&quot;adder&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;*[1]&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:with-param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;rest&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;*[2]&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:apply-templates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!-- add Succ(x) y = Succ(add x y) --&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;addSucc&quot;</span> <span style="color: #000066;">mode</span>=<span style="color: #ff0000;">&quot;adder&quot;</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;Succ&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;rest&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Succ<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:apply-templates</span> <span style="color: #000066;">mode</span>=<span style="color: #ff0000;">&quot;adder&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:with-param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;rest&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;$rest&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:apply-templates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Succ<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!-- add Zero y = y --&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;addZero&quot;</span> <span style="color: #000066;">mode</span>=<span style="color: #ff0000;">&quot;adder&quot;</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;Zero&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;rest&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:copy-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;$rest&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:stylesheet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The first template matches any occurances of <code>&lt;add/&gt;</code>, and calls the real &#8216;add&#8217; function on them. It passes the second number as a parameter, while the first is what the function actually recurses on&mdash;exactly as in the Haskell example. The rest proceeds similarly.</p>
<p>You can <a href="http://www.w3.org/2005/08/online_xslt/xslt?xslfile=http%3A%2F%2Fporg.es%2Fprocessor.xsl&#038;xmlfile=http%3A%2F%2Fporg.es%2Fdata.xml&#038;content-type=&#038;submit=transform">test it yourself</a> by using the W3C online XSLT processor, which uses <a href="http://porg.es/processor.xsl">my processor</a> and <a href="http://porg.es/data.xml">some sample input data</a>. It should work for any similar data that you can throw at it as well. In this case it is adding 2 + 2 and coming up with 4, as we expect <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_smile.gif" alt="" /></p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/computing-with-xslt/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Functors aren&#8217;t as hard as they sound</title>
		<link>http://porg.es/blog/functors-arent-as-hard-as-they-sound</link>
		<comments>http://porg.es/blog/functors-arent-as-hard-as-they-sound#comments</comments>
		<pubDate>Tue, 17 Jul 2007 01:45:34 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Types]]></category>

		<guid isPermaLink="false">http://porg.es/blog/functors-arent-as-hard-as-they-sound</guid>
		<description><![CDATA[Suppose we have a functor. Let&#8217;s call it &#8220;My Functor&#8221;, because that&#8217;s a nice friendly name. I&#8217;ll call it for short. Now, a functor is&#8212;according to higher sources&#8212;something which takes any type to a type . Let&#8217;s see how we can accomplish that with our functor, using Haskell code: data MyFunctor a = Construct a [...]]]></description>
			<content:encoded><![CDATA[<p>Suppose we have a functor. Let&#8217;s call it &#8220;My Functor&#8221;, because that&#8217;s a nice friendly name. I&#8217;ll call it <img src='/blog/wp-content/plugins/latexrender/pictures/12c578c9f48dd6727464670d5daa0f9c.gif' title='MF' alt='MF' align=absmiddle> for short.</p>
<p>Now, a functor <img src='/blog/wp-content/plugins/latexrender/pictures/12c578c9f48dd6727464670d5daa0f9c.gif' title='MF' alt='MF' align=absmiddle> is&mdash;according to higher sources&mdash;something which takes any type <img src='/blog/wp-content/plugins/latexrender/pictures/0cc175b9c0f1b6a831c399e269772661.gif' title='a' alt='a' align=absmiddle> to a type <img src='/blog/wp-content/plugins/latexrender/pictures/d643c19abe07b4b5d972095c2a2c90cf.gif' title='MF(a)' alt='MF(a)' align=absmiddle>. Let&#8217;s see how we can accomplish that with our functor, using Haskell code:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">data</span> MyFunctor a <span style="color: #339933; font-weight: bold;">=</span> Construct a</pre></div></div>

<p>This is quite simply a type constructor for our functor for any type <img src='/blog/wp-content/plugins/latexrender/pictures/0cc175b9c0f1b6a831c399e269772661.gif' title='a' alt='a' align=absmiddle>. For example, we can use it on the Integer type as Construct Integer, which will give us the type MyFunctor Integer.</p>
<p>That&#8217;s half the definition of a functor. The other half is that it also converts any function <img src='/blog/wp-content/plugins/latexrender/pictures/392cfb6d2afe518b0a351a7d4561aa8b.gif' title='f \colon a \to b' alt='f \colon a \to b' align=absmiddle> to a function <img src='/blog/wp-content/plugins/latexrender/pictures/1cf4a6d3aabd63c18fd65fa17986f585.gif' title='MF(f) \colon MF(a) \to MF(b)' alt='MF(f) \colon MF(a) \to MF(b)' align=absmiddle>. This means for any function, it converts that function to work on stuff &#8220;inside&#8221; the functor.</p>
<p>In terms of our Haskell code, this means that we have a function that acts on another function to modify it. This function is usually called &#8220;fmap&#8221; (for &#8220;functor map&#8221; or something similar). This function has the type:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="font-weight: bold;">fmap</span> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span>a <span style="color: #339933; font-weight: bold;">-&gt;</span> b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>MyFunctor a <span style="color: #339933; font-weight: bold;">-&gt;</span> MyFunctor b<span style="color: green;">&#41;</span></pre></div></div>

<p>It takes any function <img src='/blog/wp-content/plugins/latexrender/pictures/12261581da48b192397177dd2a6be281.gif' title='f\colon a\to b' alt='f\colon a\to b' align=absmiddle> to <img src='/blog/wp-content/plugins/latexrender/pictures/1cf4a6d3aabd63c18fd65fa17986f585.gif' title='MF(f) \colon MF(a) \to MF(b)' alt='MF(f) \colon MF(a) \to MF(b)' align=absmiddle>. Now we can define the function itself. This is the part which will be the most unique for our functor, and we can define it how we like. For our functor, this isn&#8217;t going to be very interesting:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="font-weight: bold;">fmap</span> f <span style="color: green;">&#40;</span>MyFunctor a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> MyFunctor <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span></pre></div></div>

<p>We unwrap the object <img src='/blog/wp-content/plugins/latexrender/pictures/0cc175b9c0f1b6a831c399e269772661.gif' title='a' alt='a' align=absmiddle> from our MyFunctor, apply the function <img src='/blog/wp-content/plugins/latexrender/pictures/8fa14cdd754f91cc6554c9e71929cce7.gif' title='f' alt='f' align=absmiddle>, then wrap it back up.</p>
<p>Now there are two laws that <img src='/blog/wp-content/plugins/latexrender/pictures/02bd1f8596236925b11b67bbb95c74eb.gif' title='fmap' alt='fmap' align=absmiddle> must satisfy in order for this to be a &#8220;real&#8221; functor. In Haskell these are:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: green;">&#40;</span><span style="font-weight: bold;">fmap</span> f<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">fmap</span> g<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">==</span> <span style="font-weight: bold;">fmap</span> <span style="color: green;">&#40;</span>f <span style="color: #339933; font-weight: bold;">.</span> g<span style="color: green;">&#41;</span>
<span style="font-weight: bold;">id</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">fmap</span> <span style="color: #339933; font-weight: bold;">==</span> <span style="font-weight: bold;">fmap</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">id</span> <span style="color: #339933; font-weight: bold;">==</span> <span style="font-weight: bold;">fmap</span></pre></div></div>

<p>These can&#8217;t be enforced from &#8220;within&#8221; Haskell, but you must keep them in mind when writing your functor. If you&#8217;d like, you can check that these laws hold for our MyFunctor defined above.</p>
<p>Now we have our entire functor:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">data</span> MyFunctor a <span style="color: #339933; font-weight: bold;">=</span> Construct a
<span style="font-weight: bold;">fmap</span> f <span style="color: green;">&#40;</span>MyFunctor a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> MyFunctor <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span></pre></div></div>

<p>&#8230;and in fact, Haskell has a built-in class for functors:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">class</span> <span style="color: #cccc00; font-weight: bold;">Functor</span> f <span style="color: #06c; font-weight: bold;">where</span>
  <span style="font-weight: bold;">fmap</span> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span>a <span style="color: #339933; font-weight: bold;">-&gt;</span> b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>f a <span style="color: #339933; font-weight: bold;">-&gt;</span> f b<span style="color: green;">&#41;</span></pre></div></div>

<p>We can declare our MyFunctor to be a Functor like this:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">instance</span> <span style="color: #cccc00; font-weight: bold;">Functor</span> MyFunctor <span style="color: #06c; font-weight: bold;">where</span>
  <span style="font-weight: bold;">fmap</span> f <span style="color: green;">&#40;</span>MyFunctor a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> MyFunctor <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span></pre></div></div>

<p>&#8230;which still doesn&#8217;t seem very interesting.</p>
<p>An interesting functor is <img src='/blog/wp-content/plugins/latexrender/pictures/4ee29ca12c7d126654bd0e5275de6135.gif' title='List' alt='List' align=absmiddle>. This is defined as:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">data</span> List a <span style="color: #339933; font-weight: bold;">=</span> Empty <span style="color: #339933; font-weight: bold;">|</span> Cons a <span style="color: green;">&#40;</span>List a<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> <span style="color: #cccc00; font-weight: bold;">Functor</span> List <span style="color: #06c; font-weight: bold;">where</span>
  <span style="font-weight: bold;">fmap</span> f <span style="color: green;">&#40;</span>Cons first rest<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> Cons <span style="color: green;">&#40;</span>f first<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">fmap</span> f rest<span style="color: green;">&#41;</span>
  <span style="font-weight: bold;">fmap</span> f Empty <span style="color: #339933; font-weight: bold;">=</span> Empty</pre></div></div>

<p>&#8230;wherein the functor takes a type <img src='/blog/wp-content/plugins/latexrender/pictures/0cc175b9c0f1b6a831c399e269772661.gif' title='a' alt='a' align=absmiddle> to a list of <img src='/blog/wp-content/plugins/latexrender/pictures/0cc175b9c0f1b6a831c399e269772661.gif' title='a' alt='a' align=absmiddle>, and <img src='/blog/wp-content/plugins/latexrender/pictures/02bd1f8596236925b11b67bbb95c74eb.gif' title='fmap' alt='fmap' align=absmiddle> is the familiar map list function, only expressed in a functor context.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/functors-arent-as-hard-as-they-sound/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NullPointerException</title>
		<link>http://porg.es/blog/nullpointerexception</link>
		<comments>http://porg.es/blog/nullpointerexception#comments</comments>
		<pubDate>Thu, 24 May 2007 21:26:41 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Broken]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Critique]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Thought]]></category>
		<category><![CDATA[Types]]></category>

		<guid isPermaLink="false">http://porg.es/blog/nullpointerexception</guid>
		<description><![CDATA[Why is this such an insidious error in Java? (An opinion piece!) A Comparison Firstly, I&#8217;ll show a short comparison between some Java code and some code from a language that doesn&#8217;t have NullPointerExceptions, but does have something that allows you to accomplish anything you might want to do with null pointers. This Java code: [...]]]></description>
			<content:encoded><![CDATA[<p>Why is this such an insidious error in Java? (An opinion piece!)</p>
<h3>A Comparison</h3>
<p>Firstly, I&#8217;ll show a short comparison between some Java code and some code from a language that doesn&#8217;t have <code>NullPointerException</code>s, but does have something that allows you to accomplish anything you might want to do with null pointers.</p>
<p>This Java code:</p>
<pre><code>AnObject thing = someMethod();</code></pre>
<p>is equivalent to the following Haskell:</p>
<pre><code>let thing = Maybe AnObject</code></pre>
<p>That is, every Java object reference is possibly null. What this means is that (using the type notation mentioned in earlier posts) every Java reference has the type:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/80c7aa7402953bdabc5fcf23a7a9b406.gif' title='ObjectReference[Object] = 1 + Object' alt='ObjectReference[Object] = 1 + Object' align=absmiddle></p>
<p><small>(Every object reference for an object is either to &#8216;null&#8217; (<abbr title="also known as">AKA</abbr> &#8216;unit&#8217; or &#8216;singleton&#8217<img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_wink.gif" alt="" />, or the object itself.)</small></p>
<p>&#8230; and when you&#8217;re dealing with so many objects—Java is, after all, an object-oriented language, so (almost) everything is an object—this simple little &#8220;+1&#8243; gets lost easily.</p>
<h3>Why so bad?</h3>
<p>This wouldn&#8217;t seem to be such a big deal, but when contrasted against Java&#8217;s penchant for making things (sometimes painfully) explicit—think <code>public static void main (string argh!)</code>—a small, implicit item like this is easily overlooked. Conversely, one might say that the verbosity of Java <em>increases</em> the cognitive load of understanding its code; thus helping the chances of a small mistake like this sneaking through.</p>
<p>Either way, I think I can say that this is objectively A Bad Thing. C# has gone some of the way to a &#8216;correct&#8217; solution in its introduction of possibly-null primitives, which are explicitly marked with a <code>?</code> (such as <code>int?</code>), but unfortunately this hasn&#8217;t been &#8216;back-ported&#8217; to the rest of the language. If one were able to force between possibly-null and definitely-not-null references in C#—perhaps by the analogous <code>object?</code> vs. <code>object</code>, this would help to reduce the number of errors &#8216;hidden&#8217; by the syntax.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/nullpointerexception/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Surreal and natural numbers</title>
		<link>http://porg.es/blog/surreal-and-natural-numbers</link>
		<comments>http://porg.es/blog/surreal-and-natural-numbers#comments</comments>
		<pubDate>Tue, 17 Apr 2007 22:20:00 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Thought]]></category>

		<guid isPermaLink="false">http://porg.es/blog/surreal-and-natural-numbers</guid>
		<description><![CDATA[After reading a recent post on Good Math, Bad Math concerning surreal numbers, I got to thinking about how to model these in Haskell. I came up with the following formulation: data Surreal = Zero &#124; Plus Surreal &#124; Minus Surreal &#8230; which also seems very similar to the usual construction for natural numbers: data [...]]]></description>
			<content:encoded><![CDATA[<p>After reading <a href="http://scienceblogs.com/goodmath/2007/04/signexpanded_surreal_numbers_1.php">a recent post on Good Math, Bad Math concerning surreal numbers</a>, I got to thinking about how to model these in Haskell. I came up with the following formulation:</p>
<pre><code>data Surreal = Zero | Plus Surreal | Minus Surreal</code></pre>
<p>&#8230; which also seems very similar to the usual construction for natural numbers:</p>
<pre><code>data Nat = Zero | Succ Nat</code></pre>
<p><small>(I noted this in a comment on the original post.)</small></p>
<p>This got me thinking about how this construction (the sign-expanded version) mapped onto the original construction of the surreal numbers, which uses a left and a right set:</p>
<pre><code>data Surreal = Surreal [Surr] [Surr]</code></pre>
<p>These types translate to the following (in order of appearance):</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/eaa6c4cf2842115a727f9910baf907a1.gif' title='\begin{aligned}S &amp;= 1 + S + S\\&amp;= 1 + 2S\\N &amp;= 1 + N\\S &amp;= e^S \times e^S \\&amp;= e^{2S}\end{aligned}' alt='\begin{aligned}S &amp;= 1 + S + S\\&amp;= 1 + 2S\\N &amp;= 1 + N\\S &amp;= e^S \times e^S \\&amp;= e^{2S}\end{aligned}' align=absmiddle></p>
<p>This last type is suggestive for the natural numbers as well, and of course there is a analogous construction for the naturals:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/1bff22f7d420900aca0c5631b811d1b8.gif' title='N = e^N' alt='N = e^N' align=absmiddle></p>
<p>&#8230;which is a set-based construction for the naturals. (Note that in the above formulas multisets are used instead of true sets, but I don&#8217;t think this makes a difference in this context.)</p>
<p>Of course this raises the question of what is possible with three sets (or a 3-enum (3-num?)). More on this in another post.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/surreal-and-natural-numbers/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Types as Functions (and Integration thereof)</title>
		<link>http://porg.es/blog/types-as-functions-and-integration-thereof</link>
		<comments>http://porg.es/blog/types-as-functions-and-integration-thereof#comments</comments>
		<pubDate>Wed, 11 Apr 2007 00:34:19 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Cool]]></category>
		<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Thought]]></category>

		<guid isPermaLink="false">http://porg.es/blog/types-as-functions-and-integration-thereof</guid>
		<description><![CDATA[After reading Differention of Datastructures, I think I finally understand what is meant by &#8220;types as functions&#8221;, and immediately I can see one direction in which to go from what is presented there. If we take integration to be the logical inverse of differentiation (which is defined in the article as &#8216;removal of an element [...]]]></description>
			<content:encoded><![CDATA[<p>After reading <a href="http://homepage.mac.com/sigfpe/Computing/diff.html">Differention of Datastructures</a>, I think I finally understand what is meant by &#8220;types as functions&#8221;, and immediately I can see one direction in which to go from what is presented there.</p>
<p>If we take integration to be the logical inverse of differentiation (which is defined in the article as &#8216;removal of an element from a type&#8217<img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_wink.gif" alt="" />, then we have integration as the addition of an element to a type.</p>
<p>For example, take the type of an array of 3 elements of the same type:</p>
<pre><code>F[T] = T<sup>3</sup></code></pre>
<p>If we are to add an element to this array, we want to integrate it. This gives us:</p>
<pre><code>F[T] = T<sup>4</sup> ÷ 4</code></pre>
<p>Clearly this is not what we expected. If we want to add an element to a 3-element array, we are expecting a 4-element array. However, if we look again at the above result, we can reinterpret it as telling us something: the mere integration of the type is not supplying the expected result; and if we interpret division as &#8216;without&#8217; (just as addition is &#8216;or&#8217; and multiplication is &#8216;and&#8217<img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_wink.gif" alt="" />, then we can understand the result obtained.</p>
<p>What this is in fact telling us is that we need to provide another parameter (an enum in this case), which will give us the required result. In other words (if we do it backwards&#8230<img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_wink.gif" alt="" />:</p>
<pre><code>F[T] = T<sup>4</sup> -- wanted
F'[T] = 4.T<sup>3</sup> -- needed</code></pre>
<p>In order to obtain a 4-element array, we need a 3-element array and a 4-enum. This makes sense, because if we have a 3-element array:</p>
<pre><code>x y z</code></pre>
<p>&#8230;and we wish to add an element &#8216;w&#8217; to it, we need to know where to insert it. The number of insertion points is:</p>
<pre><code>(1) x (2) y (3) z (4)</code></pre>
<p>&#8230;and thus we need to supply a 4-enum to specify exactly where we want the insertion to take place. Once we have done this, we obtain our 4-element array with no &#8216;missing&#8217; parameters.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/types-as-functions-and-integration-thereof/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Return serve: &#8216;compare&#8217; in Haskell</title>
		<link>http://porg.es/blog/return-serve-compare-in-haskell</link>
		<comments>http://porg.es/blog/return-serve-compare-in-haskell#comments</comments>
		<pubDate>Mon, 02 Apr 2007 21:37:33 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://porg.es/blog/return-serve-compare-in-haskell</guid>
		<description><![CDATA[There you go: comp [] _ = False comp _ [] = True comp (_s) (_:ys) = comp xs ys]]></description>
			<content:encoded><![CDATA[<p><a href="http://kunosure.blogspot.com/2007/04/compare-in-erlang.html">There you go</a>:</p>
<pre><code>comp [] _ = False
comp _ [] = True
comp (_<img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_mad.gif" alt="" />s) (_:ys) = comp xs ys
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/return-serve-compare-in-haskell/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Functional programming, APL and Unix pipes</title>
		<link>http://porg.es/blog/functional-programming-apl-and-unix-pipes</link>
		<comments>http://porg.es/blog/functional-programming-apl-and-unix-pipes#comments</comments>
		<pubDate>Sun, 04 Mar 2007 05:27:52 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Cool]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Thought]]></category>
		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://porg.es/blog/functional-programming-apl-and-unix-pipes</guid>
		<description><![CDATA[I&#8217;ve noticed that when reading rather deeply-nested functional code in Haskell, the process taking place in reading the code seems to resemble trying to decipher APL more than anything else. This seems to be because functions which operate over lists are easily concatenated (possibly using the $ operator), because they operate on lists in their [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve noticed that when reading rather deeply-nested functional code in Haskell, the process taking place in reading the code seems to resemble trying to decipher <abbr title="A Programming Language">APL</abbr> more than anything else. This seems to be because functions which operate over lists are easily concatenated (possibly using the <code>$</code> operator), because they operate on lists in their last parameters. For example: </p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="font-weight: bold;">foldr</span> getCounts <span style="color: green;">&#40;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">0</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">map</span> classify <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">concat</span> <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span>20<span style="color: #339933; font-weight: bold;">..</span>30<span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#91;</span>490<span style="color: #339933; font-weight: bold;">..</span>500<span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#91;</span>8120<span style="color: #339933; font-weight: bold;">..</span>8130<span style="color: green;">&#93;</span><span style="color: green;">&#93;</span></pre></div></div>

<p>In order to understand what is happening here, one needs to start from the <abbr title="right-hand side">RHS</abbr> of the expression and then work backwards&mdash;much the same as in this snippet of <abbr>APL</abbr>:</p>
<pre><code>(∼R∈R∘.×R)/R←1↓⍳R</code></pre>
<p><small>(See the <a href="http://en.wikipedia.org/wiki/APL_(programming_language)">Wikipedia article on APL</a> for what is happening here.)</small></p>
<p>In both cases, the code is operating upon a common parameter which is modified by each function in turn. Similarly, there seems to be a rather high-level isomorphism between these kinds of functional code and the concept of Unix pipes:</p>
<pre><code>sort file | uniq -c | sort -n</code></pre>
<p>&#8230;with the difference being that in this case, the common parameter is being passed from the left instead of from the right.</p>
<p>Now, in Haskell I can define a new &#8220;pipe&#8221; operator (<code>|</code> is already taken):</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">~&gt;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">::</span> a <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>a<span style="color: #339933; font-weight: bold;">-&gt;</span>b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> b
<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">~&gt;</span><span style="color: green;">&#41;</span> x f <span style="color: #339933; font-weight: bold;">=</span> f x</pre></div></div>

<p>Which just has the reverse signature to the <code>$</code> operator, and:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="font-weight: bold;">foldr</span> getCounts <span style="color: green;">&#40;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">0</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">map</span> classify <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">concat</span> <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span>20<span style="color: #339933; font-weight: bold;">..</span>30<span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#91;</span>490<span style="color: #339933; font-weight: bold;">..</span>500<span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#91;</span>8120<span style="color: #339933; font-weight: bold;">..</span>8130<span style="color: green;">&#93;</span><span style="color: green;">&#93;</span></pre></div></div>

<p>&#8230;becomes&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: green;">&#91;</span><span style="color: green;">&#91;</span>20<span style="color: #339933; font-weight: bold;">..</span>30<span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#91;</span>490<span style="color: #339933; font-weight: bold;">..</span>500<span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#91;</span>8120<span style="color: #339933; font-weight: bold;">..</span>8130<span style="color: green;">&#93;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">~&gt;</span> <span style="font-weight: bold;">concat</span> <span style="color: #339933; font-weight: bold;">~&gt;</span> <span style="font-weight: bold;">map</span> classify <span style="color: #339933; font-weight: bold;">~&gt;</span> <span style="font-weight: bold;">foldr</span> getCounts <span style="color: green;">&#40;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">0</span><span style="color: green;">&#41;</span></pre></div></div>

<p>&#8230;which makes it a bit easier to read, at least in my (used-to-Linux) eyes.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/functional-programming-apl-and-unix-pipes/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
