<?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; comparison</title>
	<atom:link href="http://porg.es/blog/tag/comparison/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>Compare and Contrast</title>
		<link>http://porg.es/blog/compare-and-contrast</link>
		<comments>http://porg.es/blog/compare-and-contrast#comments</comments>
		<pubDate>Thu, 04 Sep 2008 01:40:39 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[music]]></category>
		<category><![CDATA[compare]]></category>
		<category><![CDATA[comparison]]></category>
		<category><![CDATA[contrast]]></category>
		<category><![CDATA[short]]></category>
		<category><![CDATA[sudden]]></category>
		<category><![CDATA[Thought]]></category>
		<category><![CDATA[thoughts]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=162</guid>
		<description><![CDATA[Coldplay’s ‘42’ from Viva la Vida or Death and All His Friends (starting about 1:45), Nine Inch Nails ‘Complication’ from The Fragile.]]></description>
			<content:encoded><![CDATA[<p><img alt="" src="http://upload.wikimedia.org/wikipedia/en/0/06/VivaLaVida.jpg" title="Viva La Vida album cover" class="aligncenter" width="300" height="300" /></p>
<p>Coldplay’s ‘42’ from <i><a href="http://en.wikipedia.org/wiki/Viva_la_Vida">Viva la Vida or Death and All His Friends</a></i> (starting about 1:45), Nine Inch Nails ‘Complication’ from <i><a href="http://en.wikipedia.org/wiki/The_Fragile">The Fragile</a></i>.</p>
<p><img alt="" src="http://upload.wikimedia.org/wikipedia/en/6/6a/Nin-the_fragile800.jpg" title="The Fragile album cover" class="aligncenter" width="300" height="263" /></p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/compare-and-contrast/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>
	</channel>
</rss>
