<?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; Functional programming</title>
	<atom:link href="http://porg.es/blog/tag/functional-programming/feed" rel="self" type="application/rss+xml" />
	<link>http://porg.es/blog</link>
	<description></description>
	<lastBuildDate>Thu, 12 Jan 2012 23:45:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.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><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">..</span><span style="color: red;">20</span><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>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>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; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">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: #008000;">&#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: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span>
        <span style="color: #0600FF; font-weight: bold;">from</span> it <span style="color: #0600FF; font-weight: bold;">in</span> source
        <span style="color: #0600FF; font-weight: bold;">select</span> func<span style="color: #008000;">&#40;</span>it<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#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; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">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: #008000;">&#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> <span style="color: #0600FF; font-weight: bold;">from</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>F it <span style="color: #0600FF; font-weight: bold;">in</span> <span style="color: #0600FF; font-weight: bold;">from</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> func<span style="color: #008000;">&#40;</span>it<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#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; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> T FoldL<span style="color: #008000;">&lt;</span>F, T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#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: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    T last <span style="color: #008000;">=</span> init<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>F it <span style="color: #0600FF; font-weight: bold;">in</span> source<span style="color: #008000;">&#41;</span>
        last <span style="color: #008000;">=</span> func<span style="color: #008000;">&#40;</span>last, it<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> last<span style="color: #008000;">;</span>
<span style="color: #008000;">&#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; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> T FoldL1<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#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: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    T last <span style="color: #008000;">=</span> source<span style="color: #008000;">.</span><span style="color: #0000FF;">First</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>T it <span style="color: #0600FF; font-weight: bold;">in</span> source<span style="color: #008000;">.</span><span style="color: #0000FF;">Skip</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        last <span style="color: #008000;">=</span> func<span style="color: #008000;">&#40;</span>last, it<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> last<span style="color: #008000;">;</span>
<span style="color: #008000;">&#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; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">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: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">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: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> x <span style="color: #008000;">=&gt;</span> second<span style="color: #008000;">&#40;</span>first<span style="color: #008000;">&#40;</span>x<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#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: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> numbers <span style="color: #008000;">=</span> <span style="color: #008000;">&#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: #008000;">&#125;</span><span style="color: #008000;">;</span>
Func<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span>, <span style="color: #6666cc; font-weight: bold;">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; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var k <span style="color: #0600FF; font-weight: bold;">in</span> FFC<span style="color: #008000;">.</span><span style="color: #0000FF;">Map</span><span style="color: #008000;">&#40;</span>addOne, numbers<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>k <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
Func<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span>,<span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span> addTwo <span style="color: #008000;">=</span> addOne<span style="color: #008000;">.</span><span style="color: #0000FF;">Compose</span><span style="color: #008000;">&#40;</span>addOne<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var k <span style="color: #0600FF; font-weight: bold;">in</span> FFC<span style="color: #008000;">.</span><span style="color: #0000FF;">Map</span><span style="color: #008000;">&#40;</span>addTwo, numbers<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>k <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
var sum <span style="color: #008000;">=</span> FFC<span style="color: #008000;">.</span><span style="color: #0000FF;">FoldL</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>x, y<span style="color: #008000;">&#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: #008000;">&#41;</span><span style="color: #008000;">;</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>sum <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#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; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> FakeLazyList
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">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: #008000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>T, T<span style="color: #008000;">&gt;</span> f, T x<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> x<span style="color: #008000;">;</span>
            x <span style="color: #008000;">=</span> f<span style="color: #008000;">&#40;</span>x<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#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; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Lazy<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> 
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> Lazy<span style="color: #008000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> del<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Del <span style="color: #008000;">=</span> del<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">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; font-weight: bold;">private</span> T PValue<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">bool</span> HasValue <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">implicit</span> <span style="color: #0600FF; font-weight: bold;">operator</span> T<span style="color: #008000;">&#40;</span>Lazy<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> f<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>f<span style="color: #008000;">.</span><span style="color: #0000FF;">HasValue</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            f<span style="color: #008000;">.</span><span style="color: #0000FF;">PValue</span> <span style="color: #008000;">=</span> f<span style="color: #008000;">.</span><span style="color: #0000FF;">Del</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Invoke</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            f<span style="color: #008000;">.</span><span style="color: #0000FF;">HasValue</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> f<span style="color: #008000;">.</span><span style="color: #0000FF;">PValue</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#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; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">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: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> T _First<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">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; font-weight: bold;">public</span> T First <span style="color: #008000;">&#123;</span> get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _First<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> LazyList<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> Rest <span style="color: #008000;">&#123;</span> get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _Rest<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> LazyList<span style="color: #008000;">&#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: #008000;">&#41;</span>
    <span style="color: #008000;">&#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: #008000;">&#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; font-weight: bold;">public</span> IEnumerator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> GetEnumerator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> Enumerator<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    IEnumerator IEnumerable<span style="color: #008000;">.</span><span style="color: #0000FF;">GetEnumerator</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> Enumerator<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">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: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> LazyList<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> curr <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">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; font-weight: bold;">public</span> Enumerator<span style="color: #008000;">&#40;</span>LazyList<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> parent<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            next <span style="color: #008000;">=</span> parent<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080; font-style: italic;">/* must implement both interfaces */</span>
        <span style="color: #6666cc; font-weight: bold;">object</span> IEnumerator<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span> <span style="color: #008000;">&#123;</span> get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> T Current <span style="color: #008000;">&#123;</span>
                get <span style="color: #008000;">&#123;</span>
                    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>curr <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                        <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> InvalidOperationException<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    <span style="color: #0600FF; font-weight: bold;">else</span>
                        <span style="color: #0600FF; font-weight: bold;">return</span> curr<span style="color: #008000;">.</span>_First<span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> MoveNext<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> curr <span style="color: #008000;">=</span> next<span style="color: #008000;">;</span> next <span style="color: #008000;">=</span> next<span style="color: #008000;">.</span>_Rest<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">return</span> curr <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Reset<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> NotSupportedException<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Dispose<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#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; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">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: #008000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>X, X<span style="color: #008000;">&gt;</span> f, X x<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">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: #008000;">&#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: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">delegate</span> <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> Iterate<span style="color: #008000;">&#40;</span>f, f<span style="color: #008000;">&#40;</span>x<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#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; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">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: #008000;">&#40;</span>X x<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">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: #008000;">&#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: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">delegate</span> <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> Repeat<span style="color: #008000;">&#40;</span>x<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#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; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">implicit</span> <span style="color: #0600FF; font-weight: bold;">operator</span> Lazy<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> f<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">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: #008000;">&#40;</span>f<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#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: #008000;">&#40;</span>x, <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> Repeat<span style="color: #008000;">&#40;</span>x<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#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: #6666cc; font-weight: bold;">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: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;.</span><span style="color: #0000FF;">Iterate</span><span style="color: #008000;">&#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: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Take</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">20</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var y <span style="color: #0600FF; font-weight: bold;">in</span> ys<span style="color: #008000;">&#41;</span> Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>y<span style="color: #008000;">&#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: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;.</span><span style="color: #0000FF;">Repeat</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">89</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Take</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">20</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var y <span style="color: #0600FF; font-weight: bold;">in</span> ys<span style="color: #008000;">&#41;</span> Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>y<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#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>Computing with XSLT</title>
		<link>http://porg.es/blog/computing-with-xslt</link>
		<comments>http://porg.es/blog/computing-with-xslt#comments</comments>
		<pubDate>Thu, 19 Jul 2007 02:34:18 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Cool]]></category>
		<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[XSLT]]></category>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

<p>&#8230;wherein the functor takes a type <img src='/blog/wp-content/plugins/latexrender/pictures/0cc175b9c0f1b6a831c399e269772661.gif' title='a' alt='a' align=absmiddle> to a list of <img src='/blog/wp-content/plugins/latexrender/pictures/0cc175b9c0f1b6a831c399e269772661.gif' title='a' alt='a' align=absmiddle>, and <img src='/blog/wp-content/plugins/latexrender/pictures/02bd1f8596236925b11b67bbb95c74eb.gif' title='fmap' alt='fmap' align=absmiddle> is the familiar map list function, only expressed in a functor context.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/functors-arent-as-hard-as-they-sound/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exponential Bags: Integration of Types, continued</title>
		<link>http://porg.es/blog/exponential-bags-integration-of-types-continued</link>
		<comments>http://porg.es/blog/exponential-bags-integration-of-types-continued#comments</comments>
		<pubDate>Wed, 11 Apr 2007 10:49:16 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Cool]]></category>
		<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[Odd]]></category>
		<category><![CDATA[Thought]]></category>

		<guid isPermaLink="false">http://porg.es/blog/exponential-bags-integration-of-types-continued</guid>
		<description><![CDATA[Derivatives of Containers claims to look for the &#8216;container which is its own derivative&#8217;; in other words, a type analogue of ex. They arrive at (approximated in ASCII and the notation used in my previous post): T[X] = Forall(n : N) Xn / Autn This is a little much for me, since I don&#8217;t understand [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.cs.nott.ac.uk/~ctm/derivcont.ps.gz">Derivatives of Containers</a> claims to look for the &#8216;container which is its own derivative&#8217;; in other words, a type analogue of <i>e<sup>x</sup></i>. They arrive at (approximated in ASCII and the notation used in my previous post):</p>
<pre><code>T[<i>X</i>] = Forall(<i>n</i> : N) <i>X</i><sup><i>n</i></sup> / Aut<sub><i>n</i></sub></code></pre>
<p>This is a little much for me, since I don&#8217;t understand the symbolism and terminology used in the other 99% of the paper, either! However, as they note that it closely matches the Taylor-series expansion of <i>e<sup>x</sup></i>. I think that using the simple concepts I linked to in the previous post, I can approach a similar definition.</p>
<p>First of all, start with a simple (identity?) type:</p>
<pre><code>F[X] = X</code></pre>
<p>Now, since what we want is a type that when differentiated, gives itself, we should start by integrating:</p>
<pre><code>F[X]  = X<sup>2</sup> ÷ 2</code></pre>
<p>As shown in the previous post, we can interpret the division operator as &#8216;missing information&#8217;. In this case, the missing information is a 2-enum stating where the new type element was to be inserted&#8230; thus we have a type consisting of two elements, only we don&#8217;t know in what order they should appear. If we repeat this process several times, we obtain:</p>
<pre><code>F[X] = X<sup>3</sup> ÷ (2×3)
F[X] = X<sup>4</sup> ÷ (2×3×4)
F[X] = X<sup>5</sup> ÷ (2×3×4×5)</code></pre>
<p>Now we can begin to see a pattern emerging. Each time we integrate the function, we are adding an element to the type, without saying where it should be inserted&#8212;in other words, we are creating an unordered set of objects (note that these aren&#8217;t true sets, but merely bags: sets which can contain duplicates). Now if we take the literal Taylor expansion of <i>e<sup>x</sup></i>, we can see what it really is as a type:</p>
<pre><code>E[X] = Σ (X<sup>n</sup> ÷ n!)</code></pre>
<p>It is either a bag with one element, or a bag with two elements, or a bag with three elements&#8230; it is in fact the union of all bag types! That is, the exponential function interpreted as a type, is the type of bags.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/exponential-bags-integration-of-types-continued/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Types as Functions (and Integration thereof)</title>
		<link>http://porg.es/blog/types-as-functions-and-integration-thereof</link>
		<comments>http://porg.es/blog/types-as-functions-and-integration-thereof#comments</comments>
		<pubDate>Wed, 11 Apr 2007 00:34:19 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Cool]]></category>
		<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Thought]]></category>

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

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

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

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

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

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

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

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

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

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

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

