<?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; Programming</title>
	<atom:link href="http://porg.es/blog/tag/programming/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>Debugging Turing: An excursion with Scheme</title>
		<link>http://porg.es/blog/debugging-turing-an-excursion-with-scheme</link>
		<comments>http://porg.es/blog/debugging-turing-an-excursion-with-scheme#comments</comments>
		<pubDate>Sun, 09 Aug 2009 15:07:55 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[ikarus]]></category>
		<category><![CDATA[machine]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[scheme]]></category>
		<category><![CDATA[silly]]></category>
		<category><![CDATA[theory]]></category>
		<category><![CDATA[turing]]></category>
		<category><![CDATA[universal]]></category>
		<category><![CDATA[ypsilon]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=368</guid>
		<description><![CDATA[So, I thought it would be a fun idea for my first ever Lisp/Scheme program to implement Alan Turing&#8217;s original a-machines from his paper, On Computable Numbers, with an Application to the Entscheidungsproblem (paper available to public). Fun? Oh, I hadn&#8217;t any idea&#8230; Preamble; choice of implementation I decided to go with the latest and [...]]]></description>
			<content:encoded><![CDATA[<p>So, I thought it would be a fun idea for my first ever Lisp/Scheme program to implement Alan Turing&#8217;s original <i>a</i>-machines from his paper, <i><a href="http://plms.oxfordjournals.org/cgi/reprint/s2-42/1/230">On Computable Numbers, with an Application to the Entscheidungsproblem</a></i> (paper available to public). Fun? Oh, I hadn&#8217;t any idea&#8230;</p>
<p><!-- more --></p>
<h3>Preamble; choice of implementation</h3>
<p>I decided to go with the latest and greatest version of Scheme: <a href="http://www.r6rs.org/">R⁶RS</a>. There are currently two implementations available under Ubuntu Linux: <a href="http://ikarus-scheme.org/">Ikarus</a> and <a href="http://www.littlewingpinball.net/mediawiki/index.php/Ypsilon">Ypsilon</a>. I installed both so I wouldn&#8217;t be swayed by any tempting extensions to the standard. <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_smile.gif" alt="" /> Despite this, I ended up using Ikarus for most testing, as it ran quite a lot faster, although Ypsilon gave <em>much</em> better stack traces.</p>
<p>I also used the <code>streams</code> library for dealing with infinite lists (<a href="http://srfi.schemers.org/srfi-41/srfi-41.html">SRFI 41</a>), which is included with both implementations.</p>
<p><i>Note: I&#8217;ll be providing all the code so that you <em>should</em> be able to copy-paste it into a new file and run it.</i></p>

<div class="wp_syntax"><div class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>import <span style="color: #66cc66;">&#40;</span>rnrs<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>rnrs r5rs <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; provides 'delay' &amp; 'force'</span>
    <span style="color: #66cc66;">&#40;</span>streams<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h3>The Machine</h3>
<p>Turing sought to capture the essence of computation. For this purpose he constructed an idealized machine, which can read and write symbols to an infinitely long piece of tape. We are going to model these idealized machines and see what they can do.</p>
<p>First, we want some types to represent the <i>a</i>-machines (the <i>a</i> is for automatic). Each machine has a set of states (<i>m</i>-configurations) it can be in, each of which contains a mapping from a set of <strong>symbols</strong> to a list of <strong>actions</strong> and a new <strong><i>m</i>-configuration</strong>. I decided that the state would be my basic unit of construction, but twiddled the meaning of <i>m</i>-configuration a tiny bit so that instead of having each configuration contain a mapping from symbols, I would instead have a list of configurations, each one with a list of which symbols activate it. I ended up with the following:</p>

<div class="wp_syntax"><div class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">; type for a configuration</span>
<span style="color: #66cc66;">&#40;</span>define<span style="color: #66cc66;">-</span>record<span style="color: #66cc66;">-</span>type m<span style="color: #66cc66;">-</span>cfg
            <span style="color: #66cc66;">&#40;</span>fields symbols operations next<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This defines a new record type called <code>m-cfg</code> with the fields <code>symbols</code>, <code>operations</code> and <code>next</code>. The <code>define-record-type</code> form defines a constructor (<code>make-m-cfg</code>) and accessors for each field (<code>m-cfg-*</code>). Rather than have a &#8216;machine&#8217; type, I decided that this would just be left implicit; if we know what the current state is then we can follow the links to <code>next</code> whenever we want to.</p>
<h3>The Tape</h3>
<p>Now, each machine operates upon an infinitely long &#8216;tape&#8217;. To model this, I use two streams, which are infinite lists. One is the infinite length of the tape to the left of the machine, and the other is the infinite length of the tape to the right of the machine. I decided that the first item in the right list would be the current item that the machine is reading.</p>

<div class="wp_syntax"><div class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">; type for a tape (modeled as two stacks)</span>
<span style="color: #66cc66;">&#40;</span>define<span style="color: #66cc66;">-</span>record<span style="color: #66cc66;">-</span>type tape
            <span style="color: #66cc66;">&#40;</span>fields left right <span style="color: #b1b100;">min</span> <span style="color: #b1b100;">max</span> index<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>You will note that there are also the fields <code>min</code>, <code>max</code>, and <code>index</code>. These are solely used to track how much of the tape the machine has &#8220;visited&#8221;. Without this information, we would not know how much of the tape to show when we want to look at it; and since it is infinitely long, this could be a problem! <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_smile.gif" alt="" /></p>
<h3>Representing operations</h3>
<p>The <code>operations</code> that a machine can perform consist of:</p>
<ul>
<li>Move right</li>
<li>Move left</li>
<li>Print symbol</li>
<li>Erase symbol</li>
<li>Halt</li>
</ul>
<p>I implemented these as an enumeration, just in case, but I didn&#8217;t actually end up utilizing any of the enumeration features.</p>

<div class="wp_syntax"><div class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">; the operations available</span>
<span style="color: #66cc66;">&#40;</span>define<span style="color: #66cc66;">-</span>enumeration op
  <span style="color: #66cc66;">&#40;</span>right left erase print halt<span style="color: #66cc66;">&#41;</span>
  op<span style="color: #66cc66;">-</span>set<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>There is a small difficulty with this representation: the &#8216;print&#8217; operation needs to be able to take an argument. I decided that operations would always be passed around as lists, and that only &#8216;print&#8217; would have a second element in the list: its argument. Here is a little shorthand to make this representation easier:</p>

<div class="wp_syntax"><div class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">; these need to be lists, because print takes an argument</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #b1b100;">L</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>op left<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> R <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>op right<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> P <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>c<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>op print<span style="color: #66cc66;">&#41;</span> c<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #b1b100;">E</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>op erase<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> H <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>op halt<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Thus, we can represent a list of operations like this: <code>(list R R (P #\A) L E H)</code>—that&#8217;s right, right, print &#8216;A&#8217; (Scheme&#8217;s syntax for characters is a little weird), left, erase, halt.</p>
<h3>Moving around on the tape</h3>
<p>Here is a simple tape; it is completely empty. Note that I use the symbol <code>'empty</code> to represent empty places on the tape. <code>stream-constant</code> makes an infinite stream of the value(s) supplied.</p>

<div class="wp_syntax"><div class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> empty<span style="color: #66cc66;">-</span>tape
  <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>tape <span style="color: #66cc66;">&#40;</span>stream<span style="color: #66cc66;">-</span>constant 'empty<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>stream<span style="color: #66cc66;">-</span>constant 'empty<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Next we need code to actually implement the operations described above. It is fairly straightforward, but we also have to keep track of the index and max/min points on the tape:</p>

<div class="wp_syntax"><div class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">; Tape manipulation:</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>current<span style="color: #66cc66;">-</span>symbol tape<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>stream<span style="color: #66cc66;">-</span><span style="color: #b1b100;">car</span> <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span>right tape<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>move<span style="color: #66cc66;">-</span>item from to<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>values <span style="color: #66cc66;">&#40;</span>stream<span style="color: #66cc66;">-</span><span style="color: #b1b100;">cdr</span> from<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>stream<span style="color: #66cc66;">-</span><span style="color: #b1b100;">cons</span> <span style="color: #66cc66;">&#40;</span>stream<span style="color: #66cc66;">-</span><span style="color: #b1b100;">car</span> from<span style="color: #66cc66;">&#41;</span> to<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>move<span style="color: #66cc66;">-</span>right tape<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>let<span style="color: #66cc66;">-</span>values <span style="color: #66cc66;">&#40;</span>
      <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>right left<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>move<span style="color: #66cc66;">-</span>item <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span>right tape<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span>left tape<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
      <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">min</span> <span style="color: #b1b100;">max</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>update<span style="color: #66cc66;">-</span>min<span style="color: #66cc66;">-</span><span style="color: #b1b100;">max</span> <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span><span style="color: #b1b100;">min</span> tape<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span><span style="color: #b1b100;">max</span> tape<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span>index tape<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>tape left right <span style="color: #b1b100;">min</span> <span style="color: #b1b100;">max</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span>index tape<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>move<span style="color: #66cc66;">-</span>left tape<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>let<span style="color: #66cc66;">-</span>values <span style="color: #66cc66;">&#40;</span>
      <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>left right<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>move<span style="color: #66cc66;">-</span>item <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span>left tape<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span>right tape<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
      <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">min</span> <span style="color: #b1b100;">max</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>update<span style="color: #66cc66;">-</span>min<span style="color: #66cc66;">-</span><span style="color: #b1b100;">max</span> <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span><span style="color: #b1b100;">min</span> tape<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span><span style="color: #b1b100;">max</span> tape<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">-</span> <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span>index tape<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>tape left right <span style="color: #b1b100;">min</span> <span style="color: #b1b100;">max</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">-</span> <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span>index tape<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>print tape symbol<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>tape
    <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span>left tape<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>stream<span style="color: #66cc66;">-</span><span style="color: #b1b100;">cons</span> symbol <span style="color: #66cc66;">&#40;</span>stream<span style="color: #66cc66;">-</span><span style="color: #b1b100;">cdr</span> <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span>right tape<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span><span style="color: #b1b100;">min</span> tape<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span><span style="color: #b1b100;">max</span> tape<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span>index tape<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>erase tape<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>print tape 'empty<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>update<span style="color: #66cc66;">-</span>min<span style="color: #66cc66;">-</span><span style="color: #b1b100;">max</span> <span style="color: #b1b100;">min</span> <span style="color: #b1b100;">max</span> i<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> 
    <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&lt;</span> i <span style="color: #b1b100;">min</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>values i <span style="color: #b1b100;">max</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&gt;</span> i <span style="color: #b1b100;">max</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>values <span style="color: #b1b100;">min</span> i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">else</span>      <span style="color: #66cc66;">&#40;</span>values <span style="color: #b1b100;">min</span> <span style="color: #b1b100;">max</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Notice that erase is actually redundant because we can just print <code>'empty</code>. We also want a &#8220;dispatcher&#8221; of sorts that takes a value representing an operation and performs that operation. This is where passing the argument around with the &#8216;print&#8217; came in useful:</p>

<div class="wp_syntax"><div class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">; performs an operation on a tape, returns new tape</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>perform<span style="color: #66cc66;">-</span>op tape oper<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">case</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> oper<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>op left<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>move<span style="color: #66cc66;">-</span>left tape<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>op right<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>move<span style="color: #66cc66;">-</span>right tape<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>op print<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>print tape <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cadr</span> oper<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>op erase<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>erase tape<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>op halt<span style="color: #66cc66;">&#41;</span> #f<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; ungraceful halt!</span></pre></div></div>

<h3>Running the machine</h3>
<p>Now that we have the operations implemented, we can almost run a state against a tape. First we need to figure out just which of the configurations of the state to run. This procedure receives a <strong>list</strong> of configurations (but they are all part of the same ‘state’) and a symbol, and picks the first configuration that has a matching symbol. Note that the configurations can also have the symbol <code>'any</code>, which matches anything.</p>

<div class="wp_syntax"><div class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">; finds the correct rule to follow for this symbol</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>find<span style="color: #66cc66;">-</span>cfg machine symbol<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>find <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>cfg<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">or</span>
                <span style="color: #66cc66;">&#40;</span>find <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">s</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eqv?</span> symbol <span style="color: #b1b100;">s</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
                  <span style="color: #66cc66;">&#40;</span>m<span style="color: #66cc66;">-</span>cfg<span style="color: #66cc66;">-</span>symbols cfg<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>find <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">s</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eqv?</span> 'any <span style="color: #b1b100;">s</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
                  <span style="color: #66cc66;">&#40;</span>m<span style="color: #66cc66;">-</span>cfg<span style="color: #66cc66;">-</span>symbols cfg<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        machine<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Now that we have a way to find out which rule to perform, and how to perform it, we can run it against a tape. This procedure advances the machine to the next state, performing all the operations needed. It returns the new state and a new tape.</p>

<div class="wp_syntax"><div class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">; runs a machine forward one step</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>run<span style="color: #66cc66;">-</span>machine tape machine<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>cfg <span style="color: #66cc66;">&#40;</span>find<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">force</span> machine<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>current<span style="color: #66cc66;">-</span>symbol tape<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>fold<span style="color: #66cc66;">-</span>left perform<span style="color: #66cc66;">-</span>op tape <span style="color: #66cc66;">&#40;</span>m<span style="color: #66cc66;">-</span>cfg<span style="color: #66cc66;">-</span>operations cfg<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span>m<span style="color: #66cc66;">-</span>cfg<span style="color: #66cc66;">-</span>next cfg<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h3>Displaying the tape</h3>
<p>Being able to run the machine against a tape isn&#8217;t much good if we can&#8217;t see the result, so here&#8217;s a procedure to print out what it looks like. This is where we need the indexes we kept track of on the tape, so we know when to stop printing.</p>

<div class="wp_syntax"><div class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>print<span style="color: #66cc66;">-</span>tape tape<span style="color: #66cc66;">&#41;</span>
  <span style="color: #808080; font-style: italic;">; move as far left as possible</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>leftTape <span style="color: #66cc66;">&#40;</span>go<span style="color: #66cc66;">-</span>far<span style="color: #66cc66;">-</span>left tape<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #808080; font-style: italic;">; now go right</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">do</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">t</span> leftTape<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span>index <span style="color: #b1b100;">t</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span><span style="color: #b1b100;">max</span> tape<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">t</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>when <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eqv?</span> <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span>index tape<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span>index <span style="color: #b1b100;">t</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">display</span> <span style="color: #ff0000;">&quot;[&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span>
          <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eqv?</span> <span style="color: #66cc66;">&#40;</span>current<span style="color: #66cc66;">-</span>symbol <span style="color: #b1b100;">t</span><span style="color: #66cc66;">&#41;</span> 'empty<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">display</span> <span style="color: #ff0000;">&quot;.&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
          <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">display</span> <span style="color: #66cc66;">&#40;</span>current<span style="color: #66cc66;">-</span>symbol <span style="color: #b1b100;">t</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>when <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eqv?</span> <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span>index tape<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span>index <span style="color: #b1b100;">t</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">display</span> <span style="color: #ff0000;">&quot;]&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set!</span> <span style="color: #b1b100;">t</span> <span style="color: #66cc66;">&#40;</span>move<span style="color: #66cc66;">-</span>right <span style="color: #b1b100;">t</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">; moves to the far left of the tape (as far as has been travelled)</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>go<span style="color: #66cc66;">-</span>far<span style="color: #66cc66;">-</span>left tape<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">do</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">t</span> tape<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span>index <span style="color: #b1b100;">t</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>tape<span style="color: #66cc66;">-</span><span style="color: #b1b100;">min</span> tape<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">t</span><span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set!</span> <span style="color: #b1b100;">t</span> <span style="color: #66cc66;">&#40;</span>move<span style="color: #66cc66;">-</span>left <span style="color: #b1b100;">t</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>I print out a &#8216;.&#8217; for each blank, and surround the current symbol with square brackets (actually I&#8217;ve changed that to be underlining in this blog post).</p>
<h3>Turing Machines!</h3>
<p>Now we can test it! Here is my definition of Turing&#8217;s first published machine:</p>

<div class="wp_syntax"><div class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">; Turing's first published machine!</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> m1 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">letrec</span> 
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>b <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
       <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>P #\<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> R<span style="color: #66cc66;">&#41;</span> c<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
   <span style="color: #66cc66;">&#91;</span>c <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
       <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">e</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
   <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">e</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
       <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>P #\<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> R<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">f</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
   <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">f</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
       <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R<span style="color: #66cc66;">&#41;</span> b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
  b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Now you see why we needed to import &#8216;delay&#8217;&#8230; since Scheme is a strictly-evaluated language, we can&#8217;t just <code>letrec</code> each state in terms of the others, so I wrap each one up in <code>delay</code>, then <code>force</code> it in one place; just before we use it in the <code>run-machine</code> procedure.</p>
<p>Other than that it is fairly straightforward, each state has a list of <i>m</i>-configurations, each of which has a list of what symbols it accepts, the actions to take, and the next state to move to. After we letrec, we have the initial state defined as the &#8216;machine&#8217;—in this case, <code>b</code>.</p>
<p>We can run this machine like so:</p>

<div class="wp_syntax"><div class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>go <span style="color: #b1b100;">t</span> m<span style="color: #66cc66;">&#41;</span>
 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">do</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>tm <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #b1b100;">t</span> m<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eqv?</span> tm #f<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> tm<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>print<span style="color: #66cc66;">-</span>tape <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> tm<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">newline</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set!</span> tm <span style="color: #66cc66;">&#40;</span>run<span style="color: #66cc66;">-</span>machine <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> tm<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cadr</span> tm<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>go empty<span style="color: #66cc66;">-</span>tape m1<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This gives us the following output:</p>
<pre>
<u>.</u>
0<u>.</u>
0.<u>.</u>
0.1<u>.</u>
0.1.<u>.</u>
0.1.0<u>.</u>
0.1.0.<u>.</u>
0.1.0.1<u>.</u>
0.1.0.1.<u>.</u>
0.1.0.1.0<u>.</u>
...
</pre>
<p>This looks correct: P0, R, R, P1, R, R, etc.</p>
<h3>Machine redux</h3>
<p>The next machine Turing gives is the same as the first one, only in a different form:</p>

<div class="wp_syntax"><div class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">; the same machine, only smaller</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> m2 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">letrec</span>
         <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>b <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
           <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'empty<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>P #\<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> b<span style="color: #66cc66;">&#41;</span>
           <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R R <span style="color: #66cc66;">&#40;</span>P #\<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> b<span style="color: #66cc66;">&#41;</span>
           <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R R <span style="color: #66cc66;">&#40;</span>P #\<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
         b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>It has only one state, but changes depending on what the current symbol is. This produces the same output as the first machine, but in fewer steps:</p>
<pre>
<u>.</u>
<u>0</u>
0.<u>1</u>
0.1.<u>0</u>
0.1.0.<u>1</u>
0.1.0.1.<u>0</u>
0.1.0.1.0.<u>1</u>
0.1.0.1.0.1.<u>0</u>
0.1.0.1.0.1.0.<u>1</u>
0.1.0.1.0.1.0.1.<u>0</u>
...</pre>
<p>You may be wondering why Turing leaves blanks between each printed symbol. He used the convention that only the &#8216;even&#8217; squares (termed <i>F</i>-squares) would be the output. The &#8216;odd&#8217; squares (termed <i>E</i>-squares) would be used as a scratch-pad.</p>
<h3>Machine-à-trois</h3>
<p>The third machine is a little more interesting. Whereas the first two printed out <code>01010101...</code>, this prints <code>01011011101111011111...</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">; Turing's third machine</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> m3 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">letrec</span>
         <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>b <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                   <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>P #\ǝ<span style="color: #66cc66;">&#41;</span> R <span style="color: #66cc66;">&#40;</span>P #\ǝ<span style="color: #66cc66;">&#41;</span> R <span style="color: #66cc66;">&#40;</span>P #\<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> R R <span style="color: #66cc66;">&#40;</span>P #\<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">L</span> <span style="color: #b1b100;">L</span><span style="color: #66cc66;">&#41;</span> o<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
          <span style="color: #66cc66;">&#91;</span>o <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                  <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R <span style="color: #66cc66;">&#40;</span>P #\x<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">L</span> <span style="color: #b1b100;">L</span> <span style="color: #b1b100;">L</span><span style="color: #66cc66;">&#41;</span> o<span style="color: #66cc66;">&#41;</span>
                  <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> q<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
          <span style="color: #66cc66;">&#91;</span>q <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                  <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\<span style="color: #cc66cc;">0</span> #\<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R R<span style="color: #66cc66;">&#41;</span> q<span style="color: #66cc66;">&#41;</span>
                  <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'empty<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>P #\<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">L</span><span style="color: #66cc66;">&#41;</span> p<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
          <span style="color: #66cc66;">&#91;</span>p <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                  <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #b1b100;">E</span> R<span style="color: #66cc66;">&#41;</span> q<span style="color: #66cc66;">&#41;</span>
                  <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\ǝ<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">f</span><span style="color: #66cc66;">&#41;</span>
                  <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'empty<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #b1b100;">L</span> <span style="color: #b1b100;">L</span><span style="color: #66cc66;">&#41;</span> p<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
          <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">f</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                  <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'empty<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>P #\<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">L</span> <span style="color: #b1b100;">L</span><span style="color: #66cc66;">&#41;</span> o<span style="color: #66cc66;">&#41;</span>
                  <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R R<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">f</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
         b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>With the output:</p>
<pre>
<u>.</u>
ǝǝ<u>0</u>.0
ǝǝ<u>0</u>.0
ǝǝ0.<u>0</u>
ǝǝ0.0.<u>.</u>
ǝǝ0.0<u>.</u>1
ǝǝ0<u>.</u>0.1
ǝ<u>ǝ</u>0.0.1
ǝǝ<u>0</u>.0.1
ǝǝ0.<u>0</u>.1
ǝǝ0.0.<u>1</u>
ǝǝ0.0.1.<u>.</u>
ǝǝ0.0.<u>1</u>.0
ǝǝ0.<u>0</u>.1x0
ǝǝ0.<u>0</u>.1x0
ǝǝ0.0.<u>1</u>x0
ǝǝ0.0.1x<u>0</u>
ǝǝ0.0.1x0.<u>.</u>
ǝǝ0.0.1x0<u>.</u>1
ǝǝ0.0.1<u>x</u>0.1
ǝǝ0.0.1.<u>0</u>.1
ǝǝ0.0.1.0.<u>1</u>
ǝǝ0.0.1.0.1.<u>.</u>
ǝǝ0.0.1.0.1<u>.</u>1
ǝǝ0.0.1.0<u>.</u>1.1
ǝǝ0.0.1<u>.</u>0.1.1
ǝǝ0.0<u>.</u>1.0.1.1
ǝǝ0<u>.</u>0.1.0.1.1
ǝ<u>ǝ</u>0.0.1.0.1.1
ǝǝ<u>0</u>.0.1.0.1.1
ǝǝ0.<u>0</u>.1.0.1.1
ǝǝ0.0.<u>1</u>.0.1.1
ǝǝ0.0.1.<u>0</u>.1.1
ǝǝ0.0.1.0.<u>1</u>.1
ǝǝ0.0.1.0.1.<u>1</u>
ǝǝ0.0.1.0.1.1.<u>.</u>
ǝǝ0.0.1.0.1.<u>1</u>.0
ǝǝ0.0.1.0.<u>1</u>.1x0
ǝǝ0.0.1.<u>0</u>.1x1x0
ǝǝ0.0.1.<u>0</u>.1x1x0
ǝǝ0.0.1.0.<u>1</u>x1x0
ǝǝ0.0.1.0.1x<u>1</u>x0
ǝǝ0.0.1.0.1x1x<u>0</u>
ǝǝ0.0.1.0.1x1x0.<u>.</u>
ǝǝ0.0.1.0.1x1x0<u>.</u>1
ǝǝ0.0.1.0.1x1<u>x</u>0.1
ǝǝ0.0.1.0.1x1.<u>0</u>.1
ǝǝ0.0.1.0.1x1.0.<u>1</u>
ǝǝ0.0.1.0.1x1.0.1.<u>.</u>
ǝǝ0.0.1.0.1x1.0.1<u>.</u>1
ǝǝ0.0.1.0.1x1.0<u>.</u>1.1
ǝǝ0.0.1.0.1x1<u>.</u>0.1.1
ǝǝ0.0.1.0.1<u>x</u>1.0.1.1
ǝǝ0.0.1.0.1.<u>1</u>.0.1.1
ǝǝ0.0.1.0.1.1.<u>0</u>.1.1
ǝǝ0.0.1.0.1.1.0.<u>1</u>.1
ǝǝ0.0.1.0.1.1.0.1.<u>1</u>
ǝǝ0.0.1.0.1.1.0.1.1.<u>.</u>
ǝǝ0.0.1.0.1.1.0.1.1<u>.</u>1
ǝǝ0.0.1.0.1.1.0.1<u>.</u>1.1
ǝǝ0.0.1.0.1.1.0<u>.</u>1.1.1
ǝǝ0.0.1.0.1.1<u>.</u>0.1.1.1
ǝǝ0.0.1.0.1<u>.</u>1.0.1.1.1
ǝǝ0.0.1.0<u>.</u>1.1.0.1.1.1
ǝǝ0.0.1<u>.</u>0.1.1.0.1.1.1
ǝǝ0.0<u>.</u>1.0.1.1.0.1.1.1
ǝǝ0<u>.</u>0.1.0.1.1.0.1.1.1
ǝ<u>ǝ</u>0.0.1.0.1.1.0.1.1.1
ǝǝ<u>0</u>.0.1.0.1.1.0.1.1.1
ǝǝ0.<u>0</u>.1.0.1.1.0.1.1.1
ǝǝ0.0.<u>1</u>.0.1.1.0.1.1.1
ǝǝ0.0.1.<u>0</u>.1.1.0.1.1.1
ǝǝ0.0.1.0.<u>1</u>.1.0.1.1.1
ǝǝ0.0.1.0.1.<u>1</u>.0.1.1.1
ǝǝ0.0.1.0.1.1.<u>0</u>.1.1.1
ǝǝ0.0.1.0.1.1.0.<u>1</u>.1.1
ǝǝ0.0.1.0.1.1.0.1.<u>1</u>.1
ǝǝ0.0.1.0.1.1.0.1.1.<u>1</u>
ǝǝ0.0.1.0.1.1.0.1.1.1.<u>.</u>
ǝǝ0.0.1.0.1.1.0.1.1.<u>1</u>.0
ǝǝ0.0.1.0.1.1.0.1.<u>1</u>.1x0
ǝǝ0.0.1.0.1.1.0.<u>1</u>.1x1x0
ǝǝ0.0.1.0.1.1.<u>0</u>.1x1x1x0
ǝǝ0.0.1.0.1.1.<u>0</u>.1x1x1x0
ǝǝ0.0.1.0.1.1.0.<u>1</u>x1x1x0
ǝǝ0.0.1.0.1.1.0.1x<u>1</u>x1x0
ǝǝ0.0.1.0.1.1.0.1x1x<u>1</u>x0
ǝǝ0.0.1.0.1.1.0.1x1x1x<u>0</u>
ǝǝ0.0.1.0.1.1.0.1x1x1x0.<u>.</u>
ǝǝ0.0.1.0.1.1.0.1x1x1x0<u>.</u>1
ǝǝ0.0.1.0.1.1.0.1x1x1<u>x</u>0.1
ǝǝ0.0.1.0.1.1.0.1x1x1.<u>0</u>.1
ǝǝ0.0.1.0.1.1.0.1x1x1.0.<u>1</u>
ǝǝ0.0.1.0.1.1.0.1x1x1.0.1.<u>.</u>
ǝǝ0.0.1.0.1.1.0.1x1x1.0.1<u>.</u>1
ǝǝ0.0.1.0.1.1.0.1x1x1.0<u>.</u>1.1
ǝǝ0.0.1.0.1.1.0.1x1x1<u>.</u>0.1.1
ǝǝ0.0.1.0.1.1.0.1x1<u>x</u>1.0.1.1
ǝǝ0.0.1.0.1.1.0.1x1.<u>1</u>.0.1.1
ǝǝ0.0.1.0.1.1.0.1x1.1.<u>0</u>.1.1
ǝǝ0.0.1.0.1.1.0.1x1.1.0.<u>1</u>.1
ǝǝ0.0.1.0.1.1.0.1x1.1.0.1.<u>1</u>
ǝǝ0.0.1.0.1.1.0.1x1.1.0.1.1.<u>.</u>
ǝǝ0.0.1.0.1.1.0.1x1.1.0.1.1<u>.</u>1
ǝǝ0.0.1.0.1.1.0.1x1.1.0.1<u>.</u>1.1
ǝǝ0.0.1.0.1.1.0.1x1.1.0<u>.</u>1.1.1
ǝǝ0.0.1.0.1.1.0.1x1.1<u>.</u>0.1.1.1
ǝǝ0.0.1.0.1.1.0.1x1<u>.</u>1.0.1.1.1
ǝǝ0.0.1.0.1.1.0.1<u>x</u>1.1.0.1.1.1
ǝǝ0.0.1.0.1.1.0.1.<u>1</u>.1.0.1.1.1
ǝǝ0.0.1.0.1.1.0.1.1.<u>1</u>.0.1.1.1
ǝǝ0.0.1.0.1.1.0.1.1.1.<u>0</u>.1.1.1
ǝǝ0.0.1.0.1.1.0.1.1.1.0.<u>1</u>.1.1
ǝǝ0.0.1.0.1.1.0.1.1.1.0.1.<u>1</u>.1
ǝǝ0.0.1.0.1.1.0.1.1.1.0.1.1.<u>1</u>
ǝǝ0.0.1.0.1.1.0.1.1.1.0.1.1.1.<u>.</u>
ǝǝ0.0.1.0.1.1.0.1.1.1.0.1.1.1<u>.</u>1
ǝǝ0.0.1.0.1.1.0.1.1.1.0.1.1<u>.</u>1.1
ǝǝ0.0.1.0.1.1.0.1.1.1.0.1<u>.</u>1.1.1
ǝǝ0.0.1.0.1.1.0.1.1.1.0<u>.</u>1.1.1.1
ǝǝ0.0.1.0.1.1.0.1.1.1<u>.</u>0.1.1.1.1
ǝǝ0.0.1.0.1.1.0.1.1<u>.</u>1.0.1.1.1.1
ǝǝ0.0.1.0.1.1.0.1<u>.</u>1.1.0.1.1.1.1
ǝǝ0.0.1.0.1.1.0<u>.</u>1.1.1.0.1.1.1.1
ǝǝ0.0.1.0.1.1<u>.</u>0.1.1.1.0.1.1.1.1
ǝǝ0.0.1.0.1<u>.</u>1.0.1.1.1.0.1.1.1.1
ǝǝ0.0.1.0<u>.</u>1.1.0.1.1.1.0.1.1.1.1
ǝǝ0.0.1<u>.</u>0.1.1.0.1.1.1.0.1.1.1.1
ǝǝ0.0<u>.</u>1.0.1.1.0.1.1.1.0.1.1.1.1
ǝǝ0<u>.</u>0.1.0.1.1.0.1.1.1.0.1.1.1.1
ǝ<u>ǝ</u>0.0.1.0.1.1.0.1.1.1.0.1.1.1.1
</pre>
<p>Here we can see the scratch-pad being used. We can also see the beginnings of a pattern of execution; notice how the machine returns to the beginning of the tape after completing each set of <code>1</code>s.</p>
<h3>Opus Magnum</h3>
<p>Next up was my main challenge. One of the major contributions of Turing&#8217;s paper was to display a machine which could emulate <em>any other</em> machine you wanted. In essence, you don&#8217;t actually need lots of different machines. You can just build one, and it can do anything any of the other machines can do! This is the principle behind modern general-purpose computers.</p>
<p>This is a very big, and complex machine. Some of the intricacies not involved in the other machines are:</p>
<ul>
<li><i>m</i>-functions; that is, configurations which accept parameters—luckily, this was surprisingly easy to implement using Scheme; I simply wrap the <code>delay</code>ed code inside another lambda</li>
<li><i>m</i>-configurations which are parametrized over all symbols on the machine—this is solved by just <code>map</code>ping a lambda over the list of symbols</li>
<li>variadic <i>m</i>-functions—solved by the magic of <code>case-lambda</code></li>
<li>poorly-scanned journal document containing Fraktur and Greek letters <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_razz.gif" alt="" /></li>
</ul>
<p>But not only was this by far the biggest and most complex machine supplied by Turing, I had read <a href="http://www.turing.org.uk/turing/scrapbook/machine.html">texts</a> mentioning unspecified <em>bugs</em> in the program.</p>
<p>&#8230; and sure enough, I ran into a &#8216;bug&#8217;. There were some configurations used in the machine which <em>weren&#8217;t defined in the paper</em>! At first I thought this was due to the low resolution of the PDF, but even enhancing the image didn&#8217;t help.</p>
<p>After much supplication and burnt offerings to the God of the Internet, I managed to find that:</p>
<h4>Someone else did the work already</h4>
<p>Yay!</p>
<p>More specifically, I found a paper entitled <i><a href="http://comjnl.oxfordjournals.org/cgi/content/abstract/36/4/351">Understanding Turing&#8217;s Universal Machine — Personal Style in Program Description </a></i> (which is unfortunately not available to the public), a marvelous paper that not only explains the errors made in detail, but also provides a nice, corrected version of Turing&#8217;s exposition of his machine.</p>
<p>After painstakingly re-checking all the states again, I arrived at this:</p>

<div class="wp_syntax"><div class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">; need this to generate a couple of cfgs</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> u<span style="color: #66cc66;">-</span>symbols <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
		   #\A #\C #\<span style="color: #b1b100;">D</span> #\<span style="color: #cc66cc;">0</span> #\<span style="color: #cc66cc;">1</span>
		   #\u #\v #\w #\x #\y #\z
		   #\<span style="color: #808080; font-style: italic;">; #\L #\R #\N</span>
		   #\∷ #\:
		   <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">; yo dawg</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> u <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">letrec</span>
        <span style="color: #66cc66;">&#40;</span>
         <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">f</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>C B a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\ǝ<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #b1b100;">L</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>f1 C B a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #b1b100;">L</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">f</span> C B a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>f1 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>C B a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> C<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'empty<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>f2 C B a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>f1 C B a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>f2 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>C B a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> C<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'empty<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R<span style="color: #66cc66;">&#41;</span> B<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>f1 C B a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>fdash <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>C B a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">f</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">l</span> C<span style="color: #66cc66;">&#41;</span> B a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>fdashdash <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>C B a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">f</span> <span style="color: #66cc66;">&#40;</span>r C<span style="color: #66cc66;">&#41;</span> B a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>r <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>C<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R<span style="color: #66cc66;">&#41;</span> C<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">l</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>C<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #b1b100;">L</span><span style="color: #66cc66;">&#41;</span> C<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>q <span style="color: #66cc66;">&#40;</span>case<span style="color: #66cc66;">-</span><span style="color: #b1b100;">lambda</span>
              <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>C<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'empty<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>q1 C<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>q C<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>C a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>q <span style="color: #66cc66;">&#40;</span>q1 C a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>q1 <span style="color: #66cc66;">&#40;</span>case<span style="color: #66cc66;">-</span><span style="color: #b1b100;">lambda</span>
               <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>C<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'empty<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> C<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>q C<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
               <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>C a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> C<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #b1b100;">L</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>q1 C a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>pe <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>C b<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">f</span> <span style="color: #66cc66;">&#40;</span>pe1 C b<span style="color: #66cc66;">&#41;</span> c #\ǝ<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>pe1 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>C b<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'empty<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>P b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> C<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R R<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>pe1 C b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>pe2 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>C a b<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>pe <span style="color: #66cc66;">&#40;</span>pe C b<span style="color: #66cc66;">&#41;</span> a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>c <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>C B a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>fdash <span style="color: #66cc66;">&#40;</span>c1 C<span style="color: #66cc66;">&#41;</span> B a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>c1 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>C<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">map</span>
                    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>b<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> b<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>pe C b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                      u<span style="color: #66cc66;">-</span>symbols<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>ce <span style="color: #66cc66;">&#40;</span>case<span style="color: #66cc66;">-</span><span style="color: #b1b100;">lambda</span>
               <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>C B a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>c <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">e</span> C B a<span style="color: #66cc66;">&#41;</span> B a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
               <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>B a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>ce <span style="color: #66cc66;">&#40;</span>ce B a<span style="color: #66cc66;">&#41;</span> B a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>ce2 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>B a b<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>ce <span style="color: #66cc66;">&#40;</span>ce B b<span style="color: #66cc66;">&#41;</span> a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>ce3 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>B a b g<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>ce <span style="color: #66cc66;">&#40;</span>ce2 B b g<span style="color: #66cc66;">&#41;</span> a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>ce5 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>B a b g <span style="color: #b1b100;">d</span> <span style="color: #b1b100;">e</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>ce3 <span style="color: #66cc66;">&#40;</span>ce2 B <span style="color: #b1b100;">d</span> <span style="color: #b1b100;">e</span><span style="color: #66cc66;">&#41;</span> a b g<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #808080; font-style: italic;">; added</span>
         <span style="color: #66cc66;">&#91;</span>cp <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>C U <span style="color: #b1b100;">F</span> a b<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>fdash <span style="color: #66cc66;">&#40;</span>cp1 C U b<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">f</span> U <span style="color: #b1b100;">F</span> b<span style="color: #66cc66;">&#41;</span> a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>cp1 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>C U b<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">map</span>
                   <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>g<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> g<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>fdash <span style="color: #66cc66;">&#40;</span>cp2 C U g<span style="color: #66cc66;">&#41;</span> U b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                     u<span style="color: #66cc66;">-</span>symbols<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>cp2 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>C U g<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> g<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> C<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> U<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>cpe <span style="color: #66cc66;">&#40;</span>case<span style="color: #66cc66;">-</span><span style="color: #b1b100;">lambda</span> 
                <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>C U <span style="color: #b1b100;">F</span> a b<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                 <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>cp <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">e</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">e</span> C C b<span style="color: #66cc66;">&#41;</span> C a<span style="color: #66cc66;">&#41;</span> U <span style="color: #b1b100;">F</span> a b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
                <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>U <span style="color: #b1b100;">F</span> a b<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                 <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>cpe <span style="color: #66cc66;">&#40;</span>cpe U <span style="color: #b1b100;">F</span> a b<span style="color: #66cc66;">&#41;</span> U <span style="color: #b1b100;">F</span> a b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">e</span> <span style="color: #66cc66;">&#40;</span>case<span style="color: #66cc66;">-</span><span style="color: #b1b100;">lambda</span>
               <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>C<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\ǝ<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>e1 C<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #b1b100;">L</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">e</span> C<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
               <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>B a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">e</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">e</span> B a<span style="color: #66cc66;">&#41;</span> B a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
               <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>C B a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">f</span> <span style="color: #66cc66;">&#40;</span>e1 C B a<span style="color: #66cc66;">&#41;</span> B a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>e1 <span style="color: #66cc66;">&#40;</span>case<span style="color: #66cc66;">-</span><span style="color: #b1b100;">lambda</span>
               <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>C<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'empty<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> C<span style="color: #66cc66;">&#41;</span> 
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R <span style="color: #b1b100;">E</span> R<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>e1 C<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
               <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>C B a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #b1b100;">E</span><span style="color: #66cc66;">&#41;</span> C<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>con <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>C a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\A<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #b1b100;">L</span> <span style="color: #66cc66;">&#40;</span>P a<span style="color: #66cc66;">&#41;</span> R<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>con1 C a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R R<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>con C a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>con1 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>C a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\A<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R <span style="color: #66cc66;">&#40;</span>P a<span style="color: #66cc66;">&#41;</span> R<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>con1 C a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\<span style="color: #b1b100;">D</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R <span style="color: #66cc66;">&#40;</span>P a<span style="color: #66cc66;">&#41;</span> R<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>con2 C a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>con2 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>C a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\C<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R <span style="color: #66cc66;">&#40;</span>P a<span style="color: #66cc66;">&#41;</span> R<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>con2 C a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R R<span style="color: #66cc66;">&#41;</span> C<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>b <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">f</span> b1 b1 #\∷<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>b1 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R R <span style="color: #66cc66;">&#40;</span>P #\:<span style="color: #66cc66;">&#41;</span> R R <span style="color: #66cc66;">&#40;</span>P #\<span style="color: #b1b100;">D</span><span style="color: #66cc66;">&#41;</span> R R <span style="color: #66cc66;">&#40;</span>P #\A<span style="color: #66cc66;">&#41;</span> R R <span style="color: #66cc66;">&#40;</span>P #\<span style="color: #b1b100;">D</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> anf<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #808080; font-style: italic;">; added &quot;R R PD&quot;</span>
         <span style="color: #66cc66;">&#91;</span>anf <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>q anf1 #\:<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #808080; font-style: italic;">; corrected from &quot;(g ...&quot;</span>
         <span style="color: #66cc66;">&#91;</span>anf1 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>con fom #\y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>fom <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\<span style="color: #808080; font-style: italic;">;) (list R (P #\z) L) (con fmp #\x))</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\z<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #b1b100;">L</span> <span style="color: #b1b100;">L</span><span style="color: #66cc66;">&#41;</span> fom<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\ǝ<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> H<span style="color: #66cc66;">&#41;</span> fom<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #b1b100;">L</span><span style="color: #66cc66;">&#41;</span> fom<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>fmp <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>cpe <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">e</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">e</span> anf #\x<span style="color: #66cc66;">&#41;</span> #\y<span style="color: #66cc66;">&#41;</span> sim #\x #\y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #808080; font-style: italic;">; corrected</span>
         <span style="color: #66cc66;">&#91;</span>sim <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>fdash sim1 sim1 #\z<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>sim1 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>con sim2 'empty<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>sim2 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\A<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> sim3<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #b1b100;">L</span> <span style="color: #66cc66;">&#40;</span>P #\u<span style="color: #66cc66;">&#41;</span> R R R<span style="color: #66cc66;">&#41;</span> sim2<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #808080; font-style: italic;">; corrected from &quot;R ...&quot;</span>
         <span style="color: #66cc66;">&#91;</span>sim3 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\A<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #b1b100;">L</span> <span style="color: #66cc66;">&#40;</span>P #\y<span style="color: #66cc66;">&#41;</span> R R R<span style="color: #66cc66;">&#41;</span> sim3<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #b1b100;">L</span> <span style="color: #66cc66;">&#40;</span>P #\y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">e</span> mf #\z<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>mf <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>q mf1 #\:<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #808080; font-style: italic;">; corrected from &quot;(g mf ...&quot;</span>
         <span style="color: #66cc66;">&#91;</span>mf1 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\A<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #b1b100;">L</span> <span style="color: #b1b100;">L</span> <span style="color: #b1b100;">L</span> <span style="color: #b1b100;">L</span><span style="color: #66cc66;">&#41;</span> mf2<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R R<span style="color: #66cc66;">&#41;</span> mf1<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>mf2 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\C<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R <span style="color: #66cc66;">&#40;</span>P #\x<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">L</span> <span style="color: #b1b100;">L</span> <span style="color: #b1b100;">L</span><span style="color: #66cc66;">&#41;</span> mf2<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\:<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> mf4<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\<span style="color: #b1b100;">D</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R <span style="color: #66cc66;">&#40;</span>P #\x<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">L</span> <span style="color: #b1b100;">L</span> <span style="color: #b1b100;">L</span><span style="color: #66cc66;">&#41;</span> mf3<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>mf3 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\:<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> mf4<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R <span style="color: #66cc66;">&#40;</span>P #\v<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">L</span> <span style="color: #b1b100;">L</span> <span style="color: #b1b100;">L</span><span style="color: #66cc66;">&#41;</span> mf3<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>mf4 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>con <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">l</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">l</span> mf5<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> 'empty<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>mf5 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'empty<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>P #\:<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> sh<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R <span style="color: #66cc66;">&#40;</span>P #\w<span style="color: #66cc66;">&#41;</span> R<span style="color: #66cc66;">&#41;</span> mf5<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>sh <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">f</span> sh1 inst #\u<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>sh1 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #b1b100;">L</span> <span style="color: #b1b100;">L</span> <span style="color: #b1b100;">L</span><span style="color: #66cc66;">&#41;</span> sh2<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>sh2 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\<span style="color: #b1b100;">D</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R R R R<span style="color: #66cc66;">&#41;</span> sh3<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; corrected from &quot;sh2&quot;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> inst<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>sh3 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\C<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R R<span style="color: #66cc66;">&#41;</span> sh4<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> inst<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>sh4 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\C<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R R<span style="color: #66cc66;">&#41;</span> sh5<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>pe2 inst #\<span style="color: #cc66cc;">0</span> #\:<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>sh5 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\C<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> inst<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>pe2 inst #\<span style="color: #cc66cc;">1</span> #\:<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>inst <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>                                 <span style="color: #808080; font-style: italic;">; note that inst1 is forced here!</span>
                                                            <span style="color: #808080; font-style: italic;">; this is because it is a zero-arity varargs</span>
                <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>q <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">l</span> <span style="color: #66cc66;">&#40;</span>inst1<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> #\u<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #808080; font-style: italic;">; corrected from &quot;(g ...&quot; </span>
         <span style="color: #66cc66;">&#91;</span>inst1 <span style="color: #66cc66;">&#40;</span>case<span style="color: #66cc66;">-</span><span style="color: #b1b100;">lambda</span>
                 <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">map</span>
                       <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> R <span style="color: #b1b100;">E</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>inst1 a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                       u<span style="color: #66cc66;">-</span>symbols<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
                 <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">case</span> x
                      <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>#\<span style="color: #b1b100;">L</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>ce5 ov #\v #\y #\x #\u #\w<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
                      <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>#\R<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>ce5 ov #\v #\x #\u #\y #\w<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
                      <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>#\N<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>ce5 ov #\v #\x #\y #\u #\w<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#91;</span>ov <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
               <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'any<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>q <span style="color: #66cc66;">&#40;</span>r <span style="color: #66cc66;">&#40;</span>r ov1<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> #\A<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #808080; font-style: italic;">; changed from original</span>
         <span style="color: #66cc66;">&#91;</span>ov1 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">delay</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
               <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\<span style="color: #b1b100;">D</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">e</span> anf<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
               <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>m<span style="color: #66cc66;">-</span>cfg <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'empty<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>P #\<span style="color: #b1b100;">D</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">e</span> anf<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
         <span style="color: #66cc66;">&#41;</span> b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">;; start in state 'b'</span></pre></div></div>

<p>I don&#8217;t think I can blame Turing much for the errors in the paper. It seems as though some arose through printing typos, and attempting to debug this baroque machine by hand, on paper, would have been a difficult task. (I don&#8217;t think he even had Visual Studio!)</p>
<h3>A machine in a machine</h3>
<p>Of course, the final test of this is to see whether this machine can actually emulate another like it is supposed to. I defined a short procedure to set up a machine on a tape according to Turing&#8217;s ingenious encoding.</p>

<div class="wp_syntax"><div class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>setup<span style="color: #66cc66;">-</span>tape tape inits<span style="color: #66cc66;">&#41;</span> 
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">t</span> <span style="color: #66cc66;">&#40;</span>move<span style="color: #66cc66;">-</span>right <span style="color: #66cc66;">&#40;</span>print <span style="color: #66cc66;">&#40;</span>move<span style="color: #66cc66;">-</span>right <span style="color: #66cc66;">&#40;</span>print tape #\ǝ<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> #\ǝ<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
  	<span style="color: #66cc66;">&#91;</span>inits <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">append</span> inits <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> #\∷<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#40;</span>go<span style="color: #66cc66;">-</span>far<span style="color: #66cc66;">-</span>left <span style="color: #66cc66;">&#40;</span>fold<span style="color: #66cc66;">-</span>left <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>tape init<span style="color: #66cc66;">&#41;</span>
  	       <span style="color: #66cc66;">&#40;</span>move<span style="color: #66cc66;">-</span>right <span style="color: #66cc66;">&#40;</span>move<span style="color: #66cc66;">-</span>right <span style="color: #66cc66;">&#40;</span>print tape init<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> 
	     <span style="color: #b1b100;">t</span> inits<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h4>A quick explanation of Turing&#8217;s encoding</h4>
<p>The idea is to first simplify, then encode the states. Turing noted that many of the <i>m</i>-configurations&#8217; operations could be considered redundant:</p>
<ul>
<li>as mentioned above, instead of having &#8220;erase&#8221; we can simply print the &#8216;blank&#8217; symbol</li>
<li>instead of <em>not</em> modifying the symbol, we simply print the symbol already present</li>
</ul>
<p>There are then only three types of operation each state needs to perform:</p>
<ol>
<li>print something and go left</li>
<li>print something and go right</li>
<li>print something and stay put</li>
</ol>
<p>States which have a sequence of operations can be split into a series of states, each of which transfers control to the next one.</p>
<p>Now, if we encode all the symbols and configurations as numbers, we can write them out. Turing chose to encode them via this scheme:</p>
<ul>
<li>configurations&#8217; numbers are the symbol &#8216;D&#8217; followed by <i>n</i> &#8216;A&#8217;s, where <i>n</i> is the number of the state</li>
<li>symbols&#8217; numbers are similar, with &#8216;D&#8217; followed by <i>n</i> &#8216;C&#8217;s. (Turing set &#8216;blank&#8217; to always be symbol 0, represented as simply “D”)</li>
</ul>
<p>So to encode each configuration, we write down its number, the symbol it accepts, the symbol it outputs, which direction to move, and which state to go to next. We prefix each configuration with &#8216;;&#8217;. (When we input it into the machine we also sandwich the whole thing between &#8216;ǝǝ&#8217; and &#8216;∷&#8217;.)</p>
<p>Here is the example which Turing gives in his paper. I have formatted it to make it easier to read. Can you tell what it does?</p>

<div class="wp_syntax"><div class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> example <span style="color: #66cc66;">&#40;</span>setup<span style="color: #66cc66;">-</span>tape empty<span style="color: #66cc66;">-</span>tape <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 
			     #\<span style="color: #808080; font-style: italic;">; #\D #\A             #\D #\D #\C     #\R #\D #\A #\A</span>
			     #\<span style="color: #808080; font-style: italic;">; #\D #\A #\A         #\D #\D         #\R #\D #\A #\A #\A</span>
			     #\<span style="color: #808080; font-style: italic;">; #\D #\A #\A #\A     #\D #\D #\C #\C #\R #\D #\A #\A #\A #\A</span>
			     #\<span style="color: #808080; font-style: italic;">; #\D #\A #\A #\A #\A #\D #\D         #\R #\D #\A)))</span></pre></div></div>

<p><br/><br />
<br/><br />
<br/></p>
<p>If we translate the symbols to get numbers we have the following:</p>
<pre>
; 1 0 0 R 2
; 2 0 . R 3
; 3 0 1 R 4
; 4 0 . R 1
</pre>
<p>This machine prints alternately <code>0.1.0.1.</code>. In fact, when I first typed it up, I left off an &#8216;A&#8217; on the 3rd state and couldn&#8217;t figure out why the machine was printing <code>0.11111...</code>!</p>
<h3>Just to show it works</h3>
<p>Here is some output of the universal machine running the &#8217;0101&#8242; machine. I have only included a snippet as the machine takes a while to get to this stage. You&#8217;ll also notice the output format is different from the other machines; this one outputs some state information and the output of the emulated machine (in this case, 0 and 1), separated by colons. So far, after a minute or so, the machine has output <code>010</code> <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_biggrin.gif" alt="" /></p>
<pre style="overflow:auto">
.ǝǝ;.D.A.D.DuCuR.DyAyAy;.D.A.A.D.D.R.D.A.A.A.;.D.A.A.A.D.D.C.C.R.D.A.A.A.A.;.D.A.A.A.A.D.D.R.D<u>.</u>A.∷.:.D.A.D.:.0.:.D.C.D.A.A.D.:.D.C.D.D.A.A.A.D.:.1.:.D.C.D.D.C.C.D.A.A.A.A.D.:.D.CvDvDvCvCvDxD.A.D.:.0.:.D.C
.ǝǝ;.D.A.D.DuCuR.DyAyAy;.D.A.A.D.D.R.D.A.A.A.;.D.A.A.A.D.D.C.C.R.D.A.A.A.A.;.D.A.A.A.A.D.D.R.D.<u>A</u>.∷.:.D.A.D.:.0.:.D.C.D.A.A.D.:.D.C.D.D.A.A.A.D.:.1.:.D.C.D.D.C.C.D.A.A.A.A.D.:.D.CvDvDvCvCvDxD.A.D.:.0.:.D.C
.ǝǝ;.D.A.D.DuCuR.DyAyAy;.D.A.A.D.D.R.D.A.A.A.;.D.A.A.A.D.D.C.C.R.D.A.A.A.A.;.D.A.A.A.A.D.D.R.D.A<u>.</u>∷.:.D.A.D.:.0.:.D.C.D.A.A.D.:.D.C.D.D.A.A.A.D.:.1.:.D.C.D.D.C.C.D.A.A.A.A.D.:.D.CvDvDvCvCvDxD.A.D.:.0.:.D.C
.ǝǝ;.D.A.D.DuCuR.DyAyAy;.D.A.A.D.D.R.D.A.A.A.;.D.A.A.A.D.D.C.C.R.D.A.A.A.A.;.D.A.A.A.A.D.D.R.D.A.<u>∷</u>.:.D.A.D.:.0.:.D.C.D.A.A.D.:.D.C.D.D.A.A.A.D.:.1.:.D.C.D.D.C.C.D.A.A.A.A.D.:.D.CvDvDvCvCvDxD.A.D.:.0.:.D.C
.ǝǝ;.D.A.D.DuCuR.DyAyAy;.D.A.A.D.D.R.D.A.A.A.;.D.A.A.A.D.D.C.C.R.D.A.A.A.A.;.D.A.A.A.A.D.D.R.D.A.∷<u>.</u>:.D.A.D.:.0.:.D.C.D.A.A.D.:.D.C.D.D.A.A.A.D.:.1.:.D.C.D.D.C.C.D.A.A.A.A.D.:.D.CvDvDvCvCvDxD.A.D.:.0.:.D.C
.ǝǝ;.D.A.D.DuCuR.DyAyAy;.D.A.A.D.D.R.D.A.A.A.;.D.A.A.A.D.D.C.C.R.D.A.A.A.A.;.D.A.A.A.A.D.D.R.D.A.∷.<u>:</u>.D.A.D.:.0.:.D.C.D.A.A.D.:.D.C.D.D.A.A.A.D.:.1.:.D.C.D.D.C.C.D.A.A.A.A.D.:.D.CvDvDvCvCvDxD.A.D.:.0.:.D.C
.ǝǝ;.D.A.D.DuCuR.DyAyAy;.D.A.A.D.D.R.D.A.A.A.;.D.A.A.A.D.D.C.C.R.D.A.A.A.A.;.D.A.A.A.A.D.D.R.D.A.∷.:<u>.</u>D.A.D.:.0.:.D.C.D.A.A.D.:.D.C.D.D.A.A.A.D.:.1.:.D.C.D.D.C.C.D.A.A.A.A.D.:.D.CvDvDvCvCvDxD.A.D.:.0.:.D.C
.ǝǝ;.D.A.D.DuCuR.DyAyAy;.D.A.A.D.D.R.D.A.A.A.;.D.A.A.A.D.D.C.C.R.D.A.A.A.A.;.D.A.A.A.A.D.D.R.D.A.∷.:.<u>D</u>.A.D.:.0.:.D.C.D.A.A.D.:.D.C.D.D.A.A.A.D.:.1.:.D.C.D.D.C.C.D.A.A.A.A.D.:.D.CvDvDvCvCvDxD.A.D.:.0.:.D.C
.ǝǝ;.D.A.D.DuCuR.DyAyAy;.D.A.A.D.D.R.D.A.A.A.;.D.A.A.A.D.D.C.C.R.D.A.A.A.A.;.D.A.A.A.A.D.D.R.D.A.∷.:.D<u>.</u>A.D.:.0.:.D.C.D.A.A.D.:.D.C.D.D.A.A.A.D.:.1.:.D.C.D.D.C.C.D.A.A.A.A.D.:.D.CvDvDvCvCvDxD.A.D.:.0.:.D.C
.ǝǝ;.D.A.D.DuCuR.DyAyAy;.D.A.A.D.D.R.D.A.A.A.;.D.A.A.A.D.D.C.C.R.D.A.A.A.A.;.D.A.A.A.A.D.D.R.D.A.∷.:.D.<u>A</u>.D.:.0.:.D.C.D.A.A.D.:.D.C.D.D.A.A.A.D.:.1.:.D.C.D.D.C.C.D.A.A.A.A.D.:.D.CvDvDvCvCvDxD.A.D.:.0.:.D.C
.ǝǝ;.D.A.D.DuCuR.DyAyAy;.D.A.A.D.D.R.D.A.A.A.;.D.A.A.A.D.D.C.C.R.D.A.A.A.A.;.D.A.A.A.A.D.D.R.D.A.∷.:.D.A<u>.</u>D.:.0.:.D.C.D.A.A.D.:.D.C.D.D.A.A.A.D.:.1.:.D.C.D.D.C.C.D.A.A.A.A.D.:.D.CvDvDvCvCvDxD.A.D.:.0.:.D.C
.ǝǝ;.D.A.D.DuCuR.DyAyAy;.D.A.A.D.D.R.D.A.A.A.;.D.A.A.A.D.D.C.C.R.D.A.A.A.A.;.D.A.A.A.A.D.D.R.D.A.∷.:.D.A.<u>D</u>.:.0.:.D.C.D.A.A.D.:.D.C.D.D.A.A.A.D.:.1.:.D.C.D.D.C.C.D.A.A.A.A.D.:.D.CvDvDvCvCvDxD.A.D.:.0.:.D.C
.ǝǝ;.D.A.D.DuCuR.DyAyAy;.D.A.A.D.D.R.D.A.A.A.;.D.A.A.A.D.D.C.C.R.D.A.A.A.A.;.D.A.A.A.A.D.D.R.D.A.∷.:.D.A.D<u>.</u>:.0.:.D.C.D.A.A.D.:.D.C.D.D.A.A.A.D.:.1.:.D.C.D.D.C.C.D.A.A.A.A.D.:.D.CvDvDvCvCvDxD.A.D.:.0.:.D.C
.ǝǝ;.D.A.D.DuCuR.DyAyAy;.D.A.A.D.D.R.D.A.A.A.;.D.A.A.A.D.D.C.C.R.D.A.A.A.A.;.D.A.A.A.A.D.D.R.D.A.∷.:.D.A.D.<u>:</u>.0.:.D.C.D.A.A.D.:.D.C.D.D.A.A.A.D.:.1.:.D.C.D.D.C.C.D.A.A.A.A.D.:.D.CvDvDvCvCvDxD.A.D.:.0.:.D.C
.ǝǝ;.D.A.D.DuCuR.DyAyAy;.D.A.A.D.D.R.D.A.A.A.;.D.A.A.A.D.D.C.C.R.D.A.A.A.A.;.D.A.A.A.A.D.D.R.D.A.∷.:.D.A.D.:<u>.</u>0.:.D.C.D.A.A.D.:.D.C.D.D.A.A.A.D.:.1.:.D.C.D.D.C.C.D.A.A.A.A.D.:.D.CvDvDvCvCvDxD.A.D.:.0.:.D.C
.ǝǝ;.D.A.D.DuCuR.DyAyAy;.D.A.A.D.D.R.D.A.A.A.;.D.A.A.A.D.D.C.C.R.D.A.A.A.A.;.D.A.A.A.A.D.D.R.D.A.∷.:.D.A.D.:.<u>0</u>.:.D.C.D.A.A.D.:.D.C.D.D.A.A.A.D.:.1.:.D.C.D.D.C.C.D.A.A.A.A.D.:.D.CvDvDvCvCvDxD.A.D.:.0.:.D.C
.ǝǝ;.D.A.D.DuCuR.DyAyAy;.D.A.A.D.D.R.D.A.A.A.;.D.A.A.A.D.D.C.C.R.D.A.A.A.A.;.D.A.A.A.A.D.D.R.D.A.∷.:.D.A.D.:.0<u>.</u>:.D.C.D.A.A.D.:.D.C.D.D.A.A.A.D.:.1.:.D.C.D.D.C.C.D.A.A.A.A.D.:.D.CvDvDvCvCvDxD.A.D.:.0.:.D.C
.ǝǝ;.D.A.D.DuCuR.DyAyAy;.D.A.A.D.D.R.D.A.A.A.;.D.A.A.A.D.D.C.C.R.D.A.A.A.A.;.D.A.A.A.A.D.D.R.D.A.∷.:.D.A.D.:.0.<u>:</u>.D.C.D.A.A.D.:.D.C.D.D.A.A.A.D.:.1.:.D.C.D.D.C.C.D.A.A.A.A.D.:.D.CvDvDvCvCvDxD.A.D.:.0.:.D.C
.ǝǝ;.D.A.D.DuCuR.DyAyAy;.D.A.A.D.D.R.D.A.A.A.;.D.A.A.A.D.D.C.C.R.D.A.A.A.A.;.D.A.A.A.A.D.D.R.D.A.∷.:.D.A.D.:.0.:<u>.</u>D.C.D.A.A.D.:.D.C.D.D.A.A.A.D.:.1.:.D.C.D.D.C.C.D.A.A.A.A.D.:.D.CvDvDvCvCvDxD.A.D.:.0.:.D.C
.ǝǝ;.D.A.D.DuCuR.DyAyAy;.D.A.A.D.D.R.D.A.A.A.;.D.A.A.A.D.D.C.C.R.D.A.A.A.A.;.D.A.A.A.A.D.D.R.D.A.∷.:.D.A.D.:.0.:.<u>D</u>.C.D.A.A.D.:.D.C.D.D.A.A.A.D.:.1.:.D.C.D.D.C.C.D.A.A.A.A.D.:.D.CvDvDvCvCvDxD.A.D.:.0.:.D.C
.ǝǝ;.D.A.D.DuCuR.DyAyAy;.D.A.A.D.D.R.D.A.A.A.;.D.A.A.A.D.D.C.C.R.D.A.A.A.A.;.D.A.A.A.A.D.D.R.D.A.∷.:.D.A.D.:.0.:.D<u>.</u>C.D.A.A.D.:.D.C.D.D.A.A.A.D.:.1.:.D.C.D.D.C.C.D.A.A.A.A.D.:.D.CvDvDvCvCvDxD.A.D.:.0.:.D.C
</pre>
<p>And that&#8217;s enough for today! Feel free to post corrections, additions, your own Turing machines, and so on <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/debugging-turing-an-excursion-with-scheme/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What can we fit in 140 characters?</title>
		<link>http://porg.es/blog/what-can-we-fit-in-140-characters</link>
		<comments>http://porg.es/blog/what-can-we-fit-in-140-characters#comments</comments>
		<pubDate>Wed, 27 May 2009 07:09:25 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[awk]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[silly]]></category>
		<category><![CDATA[stackoverflow]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[Unicode]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=327</guid>
		<description><![CDATA[This is in reference to the current ‘Twitter image encoding challenge’ running on StackOverflow. If we want to restrict ourselves to assigned, non-control, non-private Unicode characters, then by my reckoning that gives us 129,775 available characters. wget http://unicode.org/Public/UNIDATA/UnicodeData.txt awk -F ';' UnicodeData.txt -f countUnichars.awk &#124; bc countUnichars.awk source: BEGIN { print &#34;ibase=16&#34; } # set [...]]]></description>
			<content:encoded><![CDATA[<p>This is in reference to the current <a href="http://stackoverflow.com/questions/891643/twitter-image-encoding-challenge">‘Twitter image encoding challenge’ running on StackOverflow</a>.</p>
<p>If we want to restrict ourselves to assigned, non-control, non-private Unicode characters, then by my reckoning that gives us 129,775 available characters.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>unicode.org<span style="color: #000000; font-weight: bold;">/</span>Public<span style="color: #000000; font-weight: bold;">/</span>UNIDATA<span style="color: #000000; font-weight: bold;">/</span>UnicodeData.txt
<span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-F</span> <span style="color: #ff0000;">';'</span> UnicodeData.txt <span style="color: #660033;">-f</span> countUnichars.awk <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">bc</span></pre></div></div>

<p><tt>countUnichars.awk</tt> source:</p>

<div class="wp_syntax"><div class="code"><pre class="awk" style="font-family:monospace;">BEGIN { print &quot;ibase=16&quot; } # set bc to hex mode
&nbsp;
$2 ~ /Private/ { # skip any lines with &quot;private&quot; in the description
    getline;
}
&nbsp;
n { # if n is set, then print the range for bc to calculate
    printf(&quot;(%s-%s+1)+&quot;,$1,n);
    n=&quot;&quot;;
}
&nbsp;
$2 ~ /First&gt;/ { # set n if the start of a range
    n=$1;
    getline;
}
&nbsp;
$3 !~ &quot;C.&quot; { # otherwise count anything that isn't some kind of a control character
    i++;
}
&nbsp;
END { # print out the count of everything else
    printf(&quot;%X\n&quot;,i)
}</pre></div></div>

<p>This means we can store exactly 2377 bits (297 bytes) per message (this is <img src='/blog/wp-content/plugins/latexrender/pictures/3d148faa2b961edebbfea91810c1ab28.gif' title='\lfloor\log_2(129775) \times 140\rfloor' alt='\lfloor\log_2(129775) \times 140\rfloor' align=absmiddle>), so if we use a 16-colour palette we can store about 594 pixels (<img src='/blog/wp-content/plugins/latexrender/pictures/cba7624bee8dfdd5dae1931dda7f495d.gif' title='2377/\log_2(16)' alt='2377/\log_2(16)' align=absmiddle>), which can <em>almost</em> reproduce the <i>Mona Lisa</i> thumbnail in the contest page.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/what-can-we-fit-in-140-characters/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making Maya&#8217;s Python palatable</title>
		<link>http://porg.es/blog/making-mayas-python-palatable</link>
		<comments>http://porg.es/blog/making-mayas-python-palatable#comments</comments>
		<pubDate>Tue, 03 Mar 2009 04:57:12 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[criticism]]></category>
		<category><![CDATA[maya]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[short]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=300</guid>
		<description><![CDATA[Maya&#8217;s python interface leaves something to be desired, as it seems like a straight port of their existing MEL interface with no additional thought put into designing a nice OO interface. For example, here is how to set or get the ‘radius’ attribute of a sphere: s = cmds.sphere&#40;&#41; r = cmds.sphere&#40;s, query=True, radius=True&#41; cmds.sphere&#40;s, [...]]]></description>
			<content:encoded><![CDATA[<p>Maya&#8217;s python interface leaves something to be desired, as it seems like a straight port of their existing MEL interface with no additional thought put into designing a nice OO interface. For example, here is how to set or get the ‘<code>radius</code>’ attribute of a sphere:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">s = cmds.<span style="color: black;">sphere</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
r = cmds.<span style="color: black;">sphere</span><span style="color: black;">&#40;</span>s, query=<span style="color: #008000;">True</span>, radius=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
cmds.<span style="color: black;">sphere</span><span style="color: black;">&#40;</span>s, edit=<span style="color: #008000;">True</span>, radius=<span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Nasty.</p>
<p>So, here is a wrapper to make things nicer:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> maya.<span style="color: black;">cmds</span> <span style="color: #ff7700;font-weight:bold;">as</span> cmds
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Wrapper<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, other<span style="color: black;">&#41;</span>:
		<span style="color: #008000;">self</span>.<span style="color: #0000cd;">__dict__</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'me'</span><span style="color: black;">&#93;</span> = other<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__getattr__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, thing<span style="color: black;">&#41;</span>:
		i = <span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">if</span> cmds.<span style="color: black;">attributeQuery</span><span style="color: black;">&#40;</span>thing, node=<span style="color: #008000;">self</span>.<span style="color: #0000cd;">__dict__</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'me'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, exists=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">else</span> <span style="color: #ff4500;">1</span>
		<span style="color: #ff7700;font-weight:bold;">return</span> cmds.<span style="color: black;">getAttr</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: #0000cd;">__dict__</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'me'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> + <span style="color: #483d8b;">&quot;.&quot;</span> + thing<span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__setattr__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, thing, value<span style="color: black;">&#41;</span>:
		i = <span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">if</span> cmds.<span style="color: black;">attributeQuery</span><span style="color: black;">&#40;</span>thing, node=<span style="color: #008000;">self</span>.<span style="color: #0000cd;">__dict__</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'me'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, exists=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">else</span> <span style="color: #ff4500;">1</span>
		cmds.<span style="color: black;">setAttr</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: #0000cd;">__dict__</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'me'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> + <span style="color: #483d8b;">&quot;.&quot;</span> + name, value<span style="color: black;">&#41;</span></pre></div></div>

<p>The above example is now:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">s = Wrapper<span style="color: black;">&#40;</span>cmds.<span style="color: black;">sphere</span><span style="color: black;">&#41;</span>
r = s.<span style="color: black;">radius</span>
s.<span style="color: black;">radius</span> = <span style="color: #ff4500;">10</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/making-mayas-python-palatable/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Cleaning up a set of tags with Awk</title>
		<link>http://porg.es/blog/cleaning-up-a-set-of-tags-with-awk</link>
		<comments>http://porg.es/blog/cleaning-up-a-set-of-tags-with-awk#comments</comments>
		<pubDate>Wed, 28 Jan 2009 02:08:05 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[commentary]]></category>
		<category><![CDATA[replies]]></category>
		<category><![CDATA[utility]]></category>
		<category><![CDATA[awk]]></category>
		<category><![CDATA[inflammatory]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=266</guid>
		<description><![CDATA[Introduction David R. MacIver has recently written this blog post about cleaning up a set of tags. This blog post, on the other hand, is about a nice old Unix tool called ‘awk’. Awk is one of those programs that is often overlooked. It is really a small domain-specific language for processing text. In some [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p><a href="http://www.drmaciver.com/about/">David R. MacIver</a> has recently written <a href="http://www.drmaciver.com/2009/01/cleaning-up-a-set-of-tags-part-1/">this blog post about cleaning up a set of tags</a>. <i>This</i> blog post, on the other hand, is about a nice old Unix tool called ‘awk’.</p>
<p>Awk is one of those programs that is often overlooked. It is really a small domain-specific language for processing text. In some ways it resembles sed, but it is more powerful, and it especially excels at processing line- and field-structured input.</p>
<h3>Processing cite-u-like&#8217;s data</h3>
<p>First of all, before we begin I want to say this this isn&#8217;t a criticism of David&#8217;s work. Using a general-purpose language like Ruby to process data comes with several benefits. This post is to explain the benefits of awk and what it excels at doing.</p>
<p>Now, cite-u-like&#8217;s tag data comes in a pipe-separated format, so we have input like this:</p>
<pre style="overflow:auto">42|61baaeba8de136d9c1aa9c18ec3860e8|2004-11-04 02:25:05.373798+00|ecoli
42|61baaeba8de136d9c1aa9c18ec3860e8|2004-11-04 02:25:05.373798+00|metabolism
42|61baaeba8de136d9c1aa9c18ec3860e8|2004-11-04 02:25:05.373798+00|barabasi
42|61baaeba8de136d9c1aa9c18ec3860e8|2004-11-04 02:25:05.373798+00|networks
43|61baaeba8de136d9c1aa9c18ec3860e8|2004-11-04 02:25:51.839281+00|control
43|61baaeba8de136d9c1aa9c18ec3860e8|2004-11-04 02:25:51.839281+00|engineering
43|61baaeba8de136d9c1aa9c18ec3860e8|2004-11-04 02:25:51.839281+00|robustness
44|61baaeba8de136d9c1aa9c18ec3860e8|2004-11-04 02:26:33.156319+00|networks
44|61baaeba8de136d9c1aa9c18ec3860e8|2004-11-04 02:26:33.156319+00|strogatz
44|61baaeba8de136d9c1aa9c18ec3860e8|2004-11-04 02:26:33.156319+00|survey</pre>
<p>From now on, whenever you see <i>x</i>-separated data, I want you to scream ‘USE AWK!’ <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_razz.gif" alt="" /></p>
<p>Awk scripts look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="awk" style="font-family:monospace;">pattern { expression }</pre></div></div>

<p><i>pattern</i> is used to match against a record, and if successful, the action in the braces (<i>expression</i>) will be carried out. But what is a record? Awk allows you to define what a line is using the variable <code>RS</code> (short for ‘record separator’). By default it is set to <code>\n</code> (so that each line is a record), which is what we want here.</p>
<p>Within the expression you can refer to <i>fields</i> of the current record, using the syntax <code>$<i>n</i></code>. <code>$0</code> refers to the whole record, while <code>$1</code>,<code>$2</code>&#8230; refer to individual fields.</p>
<p>Awk also allows you to define what the <i>field separator</i> will be via the variable <code>FS</code>.</p>
<p>So how do we set these variables? Awk has two special patterns <code>BEGIN</code> and <code>END</code>, which run before and after everything else. In this case, we want the fields to be separated by <code>|</code>, so we use the pattern:</p>

<div class="wp_syntax"><div class="code"><pre class="awk" style="font-family:monospace;">BEGIN { FS = &quot;|&quot; }</pre></div></div>

<p>This is rather long-winded, so mawk (an implementation of awk) also allows you to set <code>FS</code> via a command-line option <code>-F</code>.</p>
<p>For this application, we want the last field of the record (we could hard-code it as <code>$4</code>, but we&#8217;re exploring awk!). Awk provides the number of fields in the record as the variable <code>NF</code> (number of fields), so we want to access this field. We do so using the record syntax <code>$</code> and the variable <code>NF</code>.</p>
<p>So to put all this together, what we want is the command:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-F</span> <span style="color: #ff0000;">&quot;|&quot;</span> <span style="color: #ff0000;">'{ print $NF }'</span></pre></div></div>

<p>We set the field separator to &#8220;|&#8221;, and write an awk expression to print the last field of each record. Since we left the <i>pattern</i> empty, this expression is evaluated on every record.</p>
<h3>Awk vs. Ruby</h3>
<p>So what good is learning all this, anyway? There are a couple of reasons:</p>
<ul>
<li>awk is standard on *nix operating systems. In order to use David&#8217;s code I had to install Ruby; with awk you can generally count on it being there.</li>
<li>awk is <em>fast</em> (I should say at least in the interpreter ‘mawk’ which is the standard for Ubuntu). On my machine the awk version completes in under a third of the time that it takes for David&#8217;s Ruby version to complete. An interesting thing was that the awk version didn&#8217;t even max out the CPU, indicating that it is IO-bound and would go faster if I had faster disks (I&#8217;m currently on a laptop).</li>
<li>Awk is ideal for record- and field-based input, as I hope this post will show you <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_smile.gif" alt="" /></li>
</ul>
<h3>Filtering data with awk</h3>
<p>After the above we use <code>sort</code> and <code>uniq</code> the same as David does to get the results in the following form:</p>
<pre> 212595 bibtex-import
 157136 no-tag
  27926 elegans
  27887 celegans
  27825 c_elegans
  27795 nematode
  27738 wormbase
  27736 caenorhabditis_elegans
  18933 review
  15280 all-articles</pre>
<p>David uses the following to filter out lines with no alphabetical content:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">ruby <span style="color: #660033;">-ne</span> <span style="color: #ff0000;">'puts $_ if !($_ =~ /^[^a-z]+$/)'</span></pre></div></div>

<p>We can use awk&#8217;s patterns to do the same thing:</p>

<div class="wp_syntax"><div class="code"><pre class="awk" style="font-family:monospace;">$0 ~ /[a-zA-Z]/</pre></div></div>

<p>Here we use the <code>~</code> (match) operator to write a pattern that matches only the records with an alphabetical character in them. (Remember that <code>$0</code> refers to the entire record.) Notice also that we can leave off the expression after the pattern, because it defaults to <code>{ print }</code>, which is exactly what we want.</p>
<p>In this case, awk really shines. On my machine it outperforms the Ruby version by a factor of 8–9.</p>
<h3>Programming with awk</h3>
<p>The third task that David does is to consolidate all tags which are differentiated only by hyphens or underscores. That is, ‘a-tag’, ‘atag’, and ‘a_tag’ should all be considered the same. We choose which one to put into the output by whichever one is used the most times (and then we normalize the tag by replacing ‘-’ with ‘_’ so there are only underscores in the output).</p>
<p>Here is David&#8217;s code to do the job:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">tag_counts = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
STDIN.<span style="color:#9900CC;">lines</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>l<span style="color:#006600; font-weight:bold;">|</span> c, t = l.<span style="color:#CC0066; font-weight:bold;">split</span>; tag_counts<span style="color:#006600; font-weight:bold;">&#91;</span>t.<span style="color:#9900CC;">strip</span><span style="color:#006600; font-weight:bold;">&#93;</span> = c.<span style="color:#9900CC;">to_i</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
duplicates = <span style="color:#CC00FF; font-weight:bold;">Hash</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>h, k<span style="color:#006600; font-weight:bold;">|</span> h<span style="color:#006600; font-weight:bold;">&#91;</span>k<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
tag_counts.<span style="color:#9900CC;">keys</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>k<span style="color:#006600; font-weight:bold;">|</span> duplicates<span style="color:#006600; font-weight:bold;">&#91;</span>k.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/-|</span>_<span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">&quot;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> k <span style="color:#006600; font-weight:bold;">&#125;</span>
duplicates.<span style="color:#9900CC;">values</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>vs<span style="color:#006600; font-weight:bold;">|</span> vs.<span style="color:#9900CC;">sort</span>!<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x, y<span style="color:#006600; font-weight:bold;">|</span> tag_counts<span style="color:#006600; font-weight:bold;">&#91;</span>y<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&lt;=&gt;</span> tag_counts<span style="color:#006600; font-weight:bold;">&#91;</span>x<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
new_tag_counts = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
duplicates.<span style="color:#9900CC;">values</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>vs<span style="color:#006600; font-weight:bold;">|</span> new_tag_counts<span style="color:#006600; font-weight:bold;">&#91;</span>vs<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span>_<span style="color:#006600; font-weight:bold;">|-</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">+/</span>, <span style="color:#996600;">&quot;_&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = vs.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>v<span style="color:#006600; font-weight:bold;">|</span> tag_counts<span style="color:#006600; font-weight:bold;">&#91;</span>v<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span>, <span style="color:#006600; font-weight:bold;">&amp;</span>:<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> new_tag_counts.<span style="color:#9900CC;">to_a</span>.<span style="color:#9900CC;">sort</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x, y<span style="color:#006600; font-weight:bold;">|</span> y<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&lt;=&gt;</span> x<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>t, c<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#996600;">&quot; #{c} #{t}&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>I&#8217;m not going to explain it here, because that&#8217;s not the point of the post <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_smile.gif" alt="" /></p>
<p>Here&#8217;s my awk script:</p>

<div class="wp_syntax"><div class="code"><pre class="awk" style="font-family:monospace;">{ tag_counts[$2] = $1 }
END {
	for (tag in tag_counts)
	{
		normtag=tag
		gsub(/-|_/,&quot;&quot;,normtag)
&nbsp;
		count=tag_counts[tag]
		sum[normtag]+=count
&nbsp;
		if (count &gt; max[normtag])
		{
			names[normtag]=tag
			max[normtag]=count
		}
	}
&nbsp;
	for (tag in names)
	{
		finaltag=names[tag]
		gsub(/(-|_)+/,&quot;_&quot;,finaltag)
		print &quot; &quot; sum[tag] &quot; &quot; finaltag
	}
}</pre></div></div>

<p>That&#8217;s right, you can use awk to do some ordinary programming tasks! Arrays are used using the usual syntax, and awk even has a foreach-style loop for looping over them. I&#8217;ll walk through the rest of the script slowly.</p>
<p>First we apply an expression to each record, creating an array of tags and their counts:</p>

<div class="wp_syntax"><div class="code"><pre class="awk" style="font-family:monospace;">{ tag_counts[$2] = $1 }</pre></div></div>

<p>Then, once everything has finished (the <code>END</code> pattern), we process this array. For each tag, we do the following:</p>
<ol>
<li>
<p>Normalize the tag (the gsub function overwrites the variable, so we have to make a copy):</p>

<div class="wp_syntax"><div class="code"><pre class="awk" style="font-family:monospace;">normtag=tag
gsub(/-|_/,&quot;&quot;,normtag)</pre></div></div>

<p>(Notice the similarity between this and Ruby&#8217;s equivalent <code>normtag.gsub!(/-|_/, "")</code>!)</p>
</li>
<li>
<p>Get the count for that tag and add it to the count for the normalized version:</p>

<div class="wp_syntax"><div class="code"><pre class="awk" style="font-family:monospace;">count=tag_counts[tag]
sum[normtag]+=count</pre></div></div>

<p>Like in PHP and Perl, if a value is not present in an array it is automatically added with a default value.</p>
</li>
<li>
<p>Next we check to see if the current tag is the commonest version of the normalized tag, and if so we save its name and count in two other arrays:</p>

<div class="wp_syntax"><div class="code"><pre class="awk" style="font-family:monospace;">if (count &gt; max[normtag])
{
	names[normtag]=tag
	max[normtag]=count
}</pre></div></div>

<p>Notice again the usefulness of a default value for nonexistent keys: <code>count > max[normtag]</code> will be true if <code>max[normtag]</code> doesn&#8217;t exist.</p>
</li>
</ol>
<p>Now we have all we need to print out the answer. For each tag we normalize it to the final version (remembering to make a copy):</p>

<div class="wp_syntax"><div class="code"><pre class="awk" style="font-family:monospace;">finaltag=names[tag]
gsub(/(-|_)+/,&quot;_&quot;,finaltag)</pre></div></div>

<p>Then we print out the line (concatenation is done by simply juxtaposing variables or strings):</p>

<div class="wp_syntax"><div class="code"><pre class="awk" style="font-family:monospace;">print &quot; &quot; sum[tag] &quot; &quot; finaltag</pre></div></div>

<p>If you&#8217;ve been watching closely you&#8217;ll notice there is a small difference between the awk and the Ruby scripts; Ruby sorts before outputting, while the awk version will come out in a non-defined order. This is fine! We can use the standard *nix tool ‘sort’ to sort the lines;</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-f</span> consolidate_tags.awk <span style="color: #000000; font-weight: bold;">&lt;</span> tags <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sort</span> <span style="color: #660033;">-nr</span> <span style="color: #000000; font-weight: bold;">&gt;</span> fixed_tags</pre></div></div>

<p>(Note that this fits in with the *nix philosophy of ‘do one thing well’ and reusing small components.)</p>
<p>Again, the awk version outperforms the Ruby by a factor of 3–4.</p>
<h3>Reading external commands and files</h3>
<p>Unfortunately there doesn&#8217;t seem to be a command-line stemming program (a quick Perl script would suffice but it isn&#8217;t what we&#8217;re here for!), so I&#8217;ll skip that stage (here&#8217;s one of the aforementioned weaknesses of a non-general-purpose language). Instead we&#8217;ll go straight to implementing stopwords.</p>
<p>Here&#8217;s David&#8217;s Ruby code again:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;set&quot;</span>
&nbsp;
stopwords = <span style="color:#CC00FF; font-weight:bold;">Set</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">*</span>
  <span style="color:#CC00FF; font-weight:bold;">IO</span>.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;smart.txt&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">lines</span>.<span style="color:#9900CC;">reject</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x =~ <span style="color:#006600; font-weight:bold;">/</span>^<span style="color:#008000; font-style:italic;">#/}.map(&amp;:strip)</span>
<span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
STDIN.<span style="color:#9900CC;">lines</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>l<span style="color:#006600; font-weight:bold;">|</span>
  c, t = l.<span style="color:#CC0066; font-weight:bold;">split</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> l <span style="color:#9966CC; font-weight:bold;">unless</span> stopwords.<span style="color:#9966CC; font-weight:bold;">include</span>? t.<span style="color:#9900CC;">strip</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>And here&#8217;s my equivalent in awk:</p>

<div class="wp_syntax"><div class="code"><pre class="awk" style="font-family:monospace;">BEGIN {
    while (getline &lt; &quot;smart.txt&quot;)
    { stopwords[$0] = 1 }
}
!($2 in stopwords)</pre></div></div>

<p>Here I use the ‘getline’ function which does as its name suggests. We make an array of all the stopwords, with 1 as a placeholder value. The pattern is then short and simple: Print every record where the tag isn&#8217;t in the stopwords (again, we can leave off the expression to print the whole record).</p>
<p><i>Note: There is a discrepancy here: David claims this eliminated 46 tags, while I get a value of 368 for both my awk code and his Ruby code.</i></p>
<p>Again, the Ruby takes about 8 times as long to execute.</p>
<h3>Conclusion</h3>
<p>Here&#8217;s a couple of points:</p>
<ul>
<li>
<p>Record- or field-oriented data? Think awk.</p>
</li>
<li>
<p>Don&#8217;t discount it just because it&#8217;s <em>venerable</em>. It is very well-suited to its task.</p>
</li>
<li>
<p><code>pattern { expression }</code> syntax is extremely flexible.</p>
</li>
<li>
<p>Awk&#8217;s regular expressions are fast.</p>
</li>
</ul>
<blockquote><p><i>Nowadays everybody wanna talk<br />
&nbsp;&nbsp;&nbsp;&nbsp;like they got something to say<br />
But nothing comes out<br />
&nbsp;&nbsp;&nbsp;&nbsp;when they move their lips<br />
Just a bunch of gibberish<br />
And motherf—kers act<br />
&nbsp;&nbsp;&nbsp;&nbsp;like they forgot about Awk</i></p>
</blockquote>
<p><img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_biggrin.gif" alt="" /></p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/cleaning-up-a-set-of-tags-with-awk/feed</wfw:commentRss>
		<slash:comments>3</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>Music Synthesizer Project (in Matlab)</title>
		<link>http://porg.es/blog/music-synthesizer-project-in-matlab</link>
		<comments>http://porg.es/blog/music-synthesizer-project-in-matlab#comments</comments>
		<pubDate>Fri, 24 Oct 2008 08:35:33 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[university]]></category>
		<category><![CDATA[massey]]></category>
		<category><![CDATA[matlab]]></category>
		<category><![CDATA[music]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[sound]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=221</guid>
		<description><![CDATA[This post brought to you as part of fulfilling the requirements for Massey University&#8217;s 143.363. The brief was to write both an additive and subtractive synthesizer, in addition to an effects processor. I generalized this a little bit so that you can combine random parts together, so the additive/subtractive synthesizers aren&#8217;t really separate any more. [...]]]></description>
			<content:encoded><![CDATA[<p><i>This post brought to you as part of fulfilling the requirements for Massey University&#8217;s <a href="http://www.massey.ac.nz/massey/study/programme-course-and-paper-search/paper.cfm?paper_code=143.363">143.363</a>.</i></p>
<p>The brief was to write both an additive and subtractive synthesizer, in addition to an effects processor. I generalized this a little bit so that you can combine random parts together, so the additive/subtractive synthesizers aren&#8217;t really separate any more.</p>
<p>Basically my code allows you to write ‘scores’ like this:</p>
<pre><code>square wave:square:
glassharp:adsr:square wave,0.1,0.2,0.6,0.6
glassharpDelay:delay:glassharp,0.4
glassharpDelayed:gain:glassharpDelay,0.5
instrument:add:glassharp,glassharpDelayed
!instrument c e8 g c4 e8 g c’4 e’ g’ c e8 g c4 e8 g c’4 e’ g’</code></pre>
<p>The first 5 lines are ‘instrument lines’ and the last one is a ‘score line’.</p>
<h3>Instrument lines</h3>
<p>The instrument lines are somewhat <a href="http://en.wikipedia.org/wiki/Single_static_assignment">SSA</a>-like in that you can only do one thing on one line. Each line has the structure <code>[instrument name]:[source or filter]:[parameters]</code>. The provided sources include <code>sine</code>, <code>square</code>, <code>sawtooth</code>, <code>triangle</code>, and <code>additive</code> (this latter has parameters which are the coefficients for <a href="http://en.wikipedia.org/wiki/Additive_synthesis">additive synthesis</a>).</p>
<p>An example of using additive synthesis (here used to approximate a square wave more and more closely):</p>
<pre><code>sq1:additive:1
sq2:additive:1,0,0.333
sq3:additive:1,0,0.333,0,0.2
sq4:additive:1,0,0.333,0,0.2,0,0.14285714
!sq1 c d e f g r !sq2 c d e f g r !sq3 c d e f g r !sq4 c d e f g r</code></pre>
<p>Filters include (parameters in brackets): <code>delay</code> (source instrument, seconds), <code>gain</code> (source instrument, amount), <a href="http://en.wikipedia.org/wiki/ADSR"><code>adsr</code></a> (source instrument, attack time, decay time, sustain gain, release time), <code>lowpass</code> (instrument, frequency, order), <code>highpass</code> (instrument, frequency, order), <code>add</code> (instrument, instrument), <code>subtract</code> (instrument, instrument).</p>
<h3>Score lines</h3>
<p>Score lines specify the notes for the instruments to play. Notes are specified like this: <code>[note name][optional sharp or flat][optional octave shift][optional length]</code>. For example, to specify a <a href="http://en.wikipedia.org/wiki/C_major">C-major</a> <a href="http://en.wikipedia.org/wiki/Arpeggio">arpeggio</a> using <a href="http://en.wikipedia.org/wiki/Crotchet">crotchets</a>, over two <a href="http://en.wikipedia.org/wiki/Octave">octaves</a>, you would use:</p>
<pre><code>c4 e g c' e' g'</code></pre>
<p>The note length ‘sticks’ for all following notes in the same line, and defaults to 4 (a crotchet) at the start of each line. Note length is specified as <img src='/blog/wp-content/plugins/latexrender/pictures/9ba22ee6c5f55c74af52949dd103f942.gif' title='\frac{1}{n}' alt='\frac{1}{n}' align=absmiddle>, so <code>c8</code> is half as long as <code>c4</code>.</p>
<p>Octave shift is indicated using commas or apostrophes:</p>
<pre><code>c,,, c,, c, c c' c'' c'''</code></pre>
<p>Sharps and flats are indicated using ‘s&#8217; and &#8216;b&#8217;, respectively:</p>
<pre><code>c cs d ds e f gb g ab a as b c'</code></pre>
<p>To change instrument in the middle of the score line, use the syntax ‘!instrument_name’. (This operation takes no time in the score.)</p>
<p>Multiple score lines will be played simultaneously. An example:</p>
<pre><code>g2 a g  g
e2 f f  e
c2 c b, c</code></pre>
<p>Rests are specified using the ‘note name’ ‘r’.</p>
<h3>Caveats</h3>
<ul>
<li>The code isn&#8217;t written for widespread consumption (it is <em>very</em> get-the-job-done).</li>
<li>It is possible to send the program into an infinite loop using a filter that refers to itself. (e.g. <code>infinite:delay:infinite,0.5</code>)</li>
<li>Currently you cannot control filter parameters via the score lines; this means you cannot (e.g.) create sound from white noise using a series of filters, as you cannot change the pitch.</li>
<li>Wavetables aren’t implemented, but should be easy to add.</li>
<li>The tempo is currently fixed at 120 BPM.</li>
<li>Gain for the whole audio file will be reduced automatically to prevent clipping.</li>
<li>The file format itself is not very forgiving. It is best to stick closely to the examples shown above! (In particular, white space at the end of a score line will break things with obscure messages.)</li>
</ul>
<h3>Code &amp; example</h3>
<p>You can listen to an example rendered by my code <a href="http://porg.es/blog/wp-content/uploads/2008/10/example.mp3">here</a>. The code can be downloaded (including the score for that example) <a href='http://porg.es/blog/wp-content/uploads/2008/10/assignment.zip'>here</a>.</p>
<h4>Comments on implementation</h4>
<p>The various files with ‘handle’ in their name are there to support Matlab&#8217;s form of closures (this is what I got from the documentation); if you don&#8217;t do it this way then all the references to ‘synth_options{n}’ end up being to the wrong options as Matlab&#8217;s anonymous functions are in some ways call-by-name.</p>
<p>The file ‘lookup’ is used instead of interfacing directly to <code>java.util.Hashtable</code> because you can&#8217;t (AFAIK) marshal <code>function_handle</code>s across to Java and back.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/music-synthesizer-project-in-matlab/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
<enclosure url="http://porg.es/blog/wp-content/uploads/2008/10/example.mp3" length="720391" type="audio/mpeg" />
		</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>A simple BigNum library for .NET</title>
		<link>http://porg.es/blog/a-simple-bignum-library-for-dot-net</link>
		<comments>http://porg.es/blog/a-simple-bignum-library-for-dot-net#comments</comments>
		<pubDate>Sat, 13 Oct 2007 08:05:51 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://porg.es/blog/a-simple-bignum-library-for-net</guid>
		<description><![CDATA[Update Due to minor demand, the code is also available: BigNum source. Please note that I haven’t actually touched the code since it was first written. I’m sure there’s some things that don’t work properly. If you’re doing anything big with this you probably want to write some ‘destructive’ update functions for adding, etc. At [...]]]></description>
			<content:encoded><![CDATA[<h4>Update</h4>
<p>Due to minor demand, the code is also available: <a href='http://porg.es/blog/wp-content/uploads/2008/04/bignum.zip'>BigNum source</a>.</p>
<p>Please note that I haven’t actually touched the code since it was first written. I’m sure there’s some things that don’t work properly. If you’re doing anything big with this you probably want to write some ‘destructive’ update functions for adding, etc. At the moment every time you add, subtract, etc, a completely new BigInt is returned. To make it faster you’d want to just update the number in-place.</p>
<h4>Original content&#8230;</h4>
<p>I’ve created a simple wrapper for <a href="http://www.gmplib.org">GMP</a>. At the moment only BigInts are implemented, but I expect to have BigRationals and BigFloats coming along soon enough. (<i>Edit: this never happened.</i>)</p>
<h4>Usage</h4>
<p>I think this is very close to as-you’d-expect. The only minor thing that might come as a surprise is that BigInts aren’t value types; since they need destructors, they can’t be. The rest is fairly straightforward:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    BigInt n<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">ulong</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;=</span> <span style="color: #FF0000;">999</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        n <span style="color: #008000;">=</span> BigInt.<span style="color: #0000FF;">Factorial</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>n<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    Console.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This code executes with sub 5-second times, which I’m pretty pleased with. I’m not sure what kind of performance decrease you get through using GMP under managed code.</p>
<h4>Caveats</h4>
<ul>
<li>Consider this alpha software <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_smile.gif" alt="" /></li>
<li>The included GMP DLL is compiled with none of the magical assembly they provide. If you want to, you can compile it yourself with all this enabled, and you should be able to just use it as a drop-in replacement.</li>
</ul>
<h4>Download</h4>
<p>Available here: <a href='http://porg.es/blog/wp-content/uploads/2007/10/bignum.zip' title='BigNum library'>BigNum library</a>. Documentation <a href="http://porg.es/Help/Index.html">is available</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/a-simple-bignum-library-for-dot-net/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Fun(c) with C# 3.0</title>
		<link>http://porg.es/blog/func-with-c-30</link>
		<comments>http://porg.es/blog/func-with-c-30#comments</comments>
		<pubDate>Tue, 09 Oct 2007 02:03:31 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Cool]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://porg.es/blog/func-with-c-30</guid>
		<description><![CDATA[Looking through the list of predefined (or, in Microsoft’s parlance, standard) query operators defined in C# 3.0, there is one that stands out as missing: the ‘map’ function. However, with the new query expression syntax, this is trivial to define: public static IEnumerable&#60;T&#62; Map&#60;F,T&#62;&#40;Func&#60;F,T&#62; func, IEnumerable&#60;F&#62; source&#41; &#123; return from it in source select func&#40;it&#41;; [...]]]></description>
			<content:encoded><![CDATA[<p>Looking through the list of predefined (or, in Microsoft’s parlance, standard) <a href="http://download.microsoft.com/download/5/8/6/5868081c-68aa-40de-9a45-a3803d8134b8/standard_query_operators.doc">query operators defined in C# 3.0</a>, there is one that stands out as missing: the ‘map’ function. However, with the new query expression syntax, this is trivial to define:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> Map<span style="color: #008000;">&lt;</span>F,T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>F,T<span style="color: #008000;">&gt;</span> func, IEnumerable<span style="color: #008000;">&lt;</span>F<span style="color: #008000;">&gt;</span> source<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span>
        from it <span style="color: #0600FF;">in</span> source
        select func<span style="color: #000000;">&#40;</span>it<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Actually, it’s trivial to implement without the new syntax (although I’ve left in <code>Func</code> because it’s nicer than defining a separate delegate):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> Map<span style="color: #008000;">&lt;</span>F,T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>F,T<span style="color: #008000;">&gt;</span> func, IEnumerable<span style="color: #008000;">&lt;</span>F<span style="color: #008000;">&gt;</span> from<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>F it <span style="color: #0600FF;">in</span> from<span style="color: #000000;">&#41;</span>
        yield <span style="color: #0600FF;">return</span> func<span style="color: #000000;">&#40;</span>it<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>But why stop at <code>map</code>? We can also define a fold:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> T FoldL<span style="color: #008000;">&lt;</span>F, T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>T, F, T<span style="color: #008000;">&gt;</span> func, IEnumerable<span style="color: #008000;">&lt;</span>F<span style="color: #008000;">&gt;</span> source, T init<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    T last <span style="color: #008000;">=</span> init<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>F it <span style="color: #0600FF;">in</span> source<span style="color: #000000;">&#41;</span>
        last <span style="color: #008000;">=</span> func<span style="color: #000000;">&#40;</span>last, it<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">return</span> last<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Or the non-empty-list variant. This is made easier through the query operator <code>Skip</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> T FoldL1<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>T, T, T<span style="color: #008000;">&gt;</span> func, IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> source<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    T last <span style="color: #008000;">=</span> source.<span style="color: #0000FF;">First</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>T it <span style="color: #0600FF;">in</span> source.<span style="color: #0000FF;">Skip</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        last <span style="color: #008000;">=</span> func<span style="color: #000000;">&#40;</span>last, it<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">return</span> last<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Finally, a simple compose operator for sticking two functions together, again using the nicer syntax to make this simple:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> Func<span style="color: #008000;">&lt;</span>X,Z<span style="color: #008000;">&gt;</span> Compose<span style="color: #008000;">&lt;</span>X,Y,Z<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> Func<span style="color: #008000;">&lt;</span>Y,Z<span style="color: #008000;">&gt;</span> second, Func<span style="color: #008000;">&lt;</span>X,Y<span style="color: #008000;">&gt;</span> first<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> x <span style="color: #008000;">=&gt;</span> second<span style="color: #000000;">&#40;</span>first<span style="color: #000000;">&#40;</span>x<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h4>Example</h4>
<p>A simple example including functionality from each part above (I put everything into an <code>FFC</code> utility class):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> numbers <span style="color: #008000;">=</span> <span style="color: #000000;">&#123;</span> <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">3</span>, <span style="color: #FF0000;">4</span>, <span style="color: #FF0000;">5</span> <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
Func<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span>, <span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> addOne <span style="color: #008000;">=</span> n <span style="color: #008000;">=&gt;</span> n <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var k <span style="color: #0600FF;">in</span> FFC.<span style="color: #0000FF;">Map</span><span style="color: #000000;">&#40;</span>addOne, numbers<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    Console.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>k <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Console.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
Func<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span>,<span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> addTwo <span style="color: #008000;">=</span> addOne.<span style="color: #0000FF;">Compose</span><span style="color: #000000;">&#40;</span>addOne<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var k <span style="color: #0600FF;">in</span> FFC.<span style="color: #0000FF;">Map</span><span style="color: #000000;">&#40;</span>addTwo, numbers<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    Console.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>k <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Console.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
var sum <span style="color: #008000;">=</span> FFC.<span style="color: #0000FF;">FoldL</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>x, y<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> x <span style="color: #008000;">+</span> y, numbers, <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Console.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>sum <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Console.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/func-with-c-30/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Lazy Lists in C#</title>
		<link>http://porg.es/blog/lazy-lists-in-c</link>
		<comments>http://porg.es/blog/lazy-lists-in-c#comments</comments>
		<pubDate>Thu, 27 Sep 2007 21:23:36 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Cool]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://porg.es/blog/lazy-lists-in-c</guid>
		<description><![CDATA[I had the thought&#8212;while browsing through some old code&#8212;that the code I used to implement futures in C# would be useful for doing things lazily, if you just moved the evaluation phase to when the value was actually demanded&#8230; this started me off thinking about how to implement a proper lazily-evaluated list in C#. A [...]]]></description>
			<content:encoded><![CDATA[<p>I had the thought&mdash;while browsing through some old code&mdash;that the code I used to <a href="http://porg.es/blog/implementing-futures-in-c">implement futures in C#</a> would be useful for doing things lazily, if you just moved the evaluation phase to when the value was actually demanded&#8230; this started me off thinking about how to implement a proper lazily-evaluated list in C#.</p>
<h4>A first attempt</h4>
<p>The first thing I thought of was to implement them using an IEnumerable with yield statements, so I began with a construction borrowed from Haskell: the iterate function.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> FakeLazyList
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> Iterate<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>T, T<span style="color: #008000;">&gt;</span> f, T x<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            yield <span style="color: #0600FF;">return</span> x<span style="color: #008000;">;</span>
            x <span style="color: #008000;">=</span> f<span style="color: #000000;">&#40;</span>x<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>While this works (try <code lang="csharp">FakeLazyList.Iterate(i => i + 1, 0)</code>), it is obviously a poor solution; it is not flexible as it requires a seperate function to be written for every incarnation.</p>
<h4>Lazy data in general</h4>
<p>My next approach was to begin with creating a generic type for lazy data. All it has to do is act as a wrapper for a piece of data. However, since in C# arguments must be evaluated before they are used, the data must in turn be wrapped inside a delegate so that the delegate&#8217;s execution can be postponed. This resulted in:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Lazy<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> 
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> Lazy<span style="color: #000000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> del<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        Del <span style="color: #008000;">=</span> del<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">private</span> Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> Del<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">private</span> T PValue<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">bool</span> HasValue <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">implicit</span> <span style="color: #0600FF;">operator</span> T<span style="color: #000000;">&#40;</span>Lazy<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> f<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>f.<span style="color: #0000FF;">HasValue</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            f.<span style="color: #0000FF;">PValue</span> <span style="color: #008000;">=</span> f.<span style="color: #0000FF;">Del</span>.<span style="color: #0000FF;">Invoke</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            f.<span style="color: #0000FF;">HasValue</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">return</span> f.<span style="color: #0000FF;">PValue</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>As you can see, execution of the data-bearing delegate is delayed until the lazy data is forced to become normal data.</p>
<h4>Implementing the list</h4>
<p>For the list implementation itself, I chose to go with the traditional cons list structure, so that each list item has an object of data, and a pointer to the next item in the list. In order to make the list lazy, the next item of the list should be generated only on-access, so it is wrapped in the above <code>Lazy&lt;T&gt;</code> class.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> LazyList<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #008000;">:</span> IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>, IEnumerable
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> T _First<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">private</span> Lazy<span style="color: #008000;">&lt;</span>LazyList<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span> _Rest<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> T First <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _First<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> LazyList<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> Rest <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _Rest<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> LazyList<span style="color: #000000;">&#40;</span>T first, Lazy<span style="color: #008000;">&lt;</span>LazyList<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span> rest<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        _First <span style="color: #008000;">=</span> first<span style="color: #008000;">;</span> _Rest <span style="color: #008000;">=</span> rest<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span></pre></div></div>

<p>Next comes some boilerplate so that the class can be used as an enumerable structure:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF;">public</span> IEnumerator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> GetEnumerator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> Enumerator<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    IEnumerator IEnumerable.<span style="color: #0000FF;">GetEnumerator</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> Enumerator<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">class</span> Enumerator <span style="color: #008000;">:</span> IEnumerator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>, IEnumerator
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">private</span> LazyList<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> curr <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">private</span> LazyList<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> next<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> Enumerator<span style="color: #000000;">&#40;</span>LazyList<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> parent<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            next <span style="color: #008000;">=</span> parent<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #008080; font-style: italic;">/* must implement both interfaces */</span>
        <span style="color: #FF0000;">object</span> IEnumerator.<span style="color: #0000FF;">Current</span> <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Current</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> T Current <span style="color: #000000;">&#123;</span>
                get <span style="color: #000000;">&#123;</span>
                    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>curr <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                        <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> InvalidOperationException<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    <span style="color: #0600FF;">else</span>
                        <span style="color: #0600FF;">return</span> curr._First<span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> MoveNext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> curr <span style="color: #008000;">=</span> next<span style="color: #008000;">;</span> next <span style="color: #008000;">=</span> next._Rest<span style="color: #008000;">;</span> <span style="color: #0600FF;">return</span> curr <span style="color: #008000;">!=</span> null<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Reset<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> NotSupportedException<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Dispose<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> return<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span></pre></div></div>

<p>Last of all comes a couple of utility functions for constructing lists, both of which I took from Haskell. The first, <code>iterate</code>, generates a list of items by taking a function <code>f</code> and a datum <code>x</code> and generating the list like this <code>[x,f(x),f(f(x)),f(f(f(x))),...]</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> LazyList<span style="color: #008000;">&lt;</span>X<span style="color: #008000;">&gt;</span> Iterate<span style="color: #008000;">&lt;</span>X<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>X, X<span style="color: #008000;">&gt;</span> f, X x<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> LazyList<span style="color: #008000;">&lt;</span>X<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>x, <span style="color: #008000;">new</span> Lazy<span style="color: #008000;">&lt;</span>LazyList<span style="color: #008000;">&lt;</span>X<span style="color: #008000;">&gt;&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">delegate</span> <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> Iterate<span style="color: #000000;">&#40;</span>f, f<span style="color: #000000;">&#40;</span>x<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span></pre></div></div>

<p>The next takes a datum and generates the infinite list of just that datum:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> LazyList<span style="color: #008000;">&lt;</span>X<span style="color: #008000;">&gt;</span> Repeat<span style="color: #008000;">&lt;</span>X<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>X x<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> LazyList<span style="color: #008000;">&lt;</span>X<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>x, <span style="color: #008000;">new</span> Lazy<span style="color: #008000;">&lt;</span>LazyList<span style="color: #008000;">&lt;</span>X<span style="color: #008000;">&gt;&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">delegate</span> <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> Repeat<span style="color: #000000;">&#40;</span>x<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>As you can see, the usage of the <code>LazyList</code> constructor isn&#8217;t the most elegant. I had hoped that I would be able to use an implicit operator on the <code>Lazy</code> class, such as:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">implicit</span> <span style="color: #0600FF;">operator</span> Lazy<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> f<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> Lazy<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>f<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>&#8230; and then use it like this &#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">new</span> LazyList<span style="color: #008000;">&lt;</span>X<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>x, <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> Repeat<span style="color: #000000;">&#40;</span>x<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Unfortunately, this doesn&#8217;t work, giving error <a href="http://msdn2.microsoft.com/en-us/library/hy74she2(vs.80).aspx">CS1660</a>. Apparently the C# compiler cannot use the fact that an implicit cast has been defined. Still, this implementation works well.</p>
<h4>Usage</h4>
<p>The usage of the <code>LazyList</code> in normal code is very simple, and as shown in this example, you can also use the new C# 3.0-defined list operators <code>Take</code>, <code>Drop</code>, <code>Where</code> etc:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> ys<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Lazy list of all positive integers</span>
ys <span style="color: #008000;">=</span> LazyList<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span>.<span style="color: #0000FF;">Iterate</span><span style="color: #000000;">&#40;</span>i <span style="color: #008000;">=&gt;</span> i <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Take</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">20</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var y <span style="color: #0600FF;">in</span> ys<span style="color: #000000;">&#41;</span> Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>y<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//Lazy list of [89, 89, 89, 89, ...]</span>
ys <span style="color: #008000;">=</span> LazyList<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span>.<span style="color: #0000FF;">Repeat</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">89</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Take</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">20</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var y <span style="color: #0600FF;">in</span> ys<span style="color: #000000;">&#41;</span> Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>y<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
Console.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/lazy-lists-in-c/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wanted: Summer of Code [2.auckland]</title>
		<link>http://porg.es/blog/wanted-summer-of-code-2auckland</link>
		<comments>http://porg.es/blog/wanted-summer-of-code-2auckland#comments</comments>
		<pubDate>Thu, 27 Sep 2007 05:20:17 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[self]]></category>
		<category><![CDATA[Default]]></category>
		<category><![CDATA[Me]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://porg.es/blog/wanted-summer-of-code-2auckland</guid>
		<description><![CDATA[I really (really) want to participate in the Wellington &#8220;Summer of Code 2.0&#8221; (blog), but it is Wellington-only&#8230; and I doubt any of the companies involved want to take on an unproven second-year student to work remotely on their software. Argh!]]></description>
			<content:encoded><![CDATA[<p>I really (<em>really</em>) want to participate in the Wellington &#8220;<a href="http://www.summerofcode.co.nz/">Summer of Code 2.0</a>&#8221; (<a href="http://blog.summerofcode.co.nz/">blog</a>), but it is Wellington-only&#8230; and I doubt any of <a href="http://www.summerofcode.co.nz/companies">the companies involved</a> want to take on an <a href="http://porg.es/blog/about-me">unproven second-year student</a> to work remotely on their software. Argh!</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/wanted-summer-of-code-2auckland/feed</wfw:commentRss>
		<slash:comments>2</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>
	</channel>
</rss>
