<?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; code</title>
	<atom:link href="http://porg.es/blog/tag/code/feed" rel="self" type="application/rss+xml" />
	<link>http://porg.es/blog</link>
	<description>... master of none</description>
	<lastBuildDate>Sat, 12 Sep 2009 07:57:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Debugging Turing: An excursion with Scheme</title>
		<link>http://porg.es/blog/debugging-turing-an-excursion-with-scheme</link>
		<comments>http://porg.es/blog/debugging-turing-an-excursion-with-scheme#comments</comments>
		<pubDate>Sun, 09 Aug 2009 15:07:55 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[ikarus]]></category>
		<category><![CDATA[machine]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[scheme]]></category>
		<category><![CDATA[silly]]></category>
		<category><![CDATA[theory]]></category>
		<category><![CDATA[turing]]></category>
		<category><![CDATA[universal]]></category>
		<category><![CDATA[ypsilon]]></category>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

<p>This means we can store exactly 2377 bits (297 bytes) per message (this is <img src='/blog/wp-content/plugins/latexrender/pictures/3d148faa2b961edebbfea91810c1ab28.gif' title='\lfloor\log_2(129775) \times 140\rfloor' alt='\lfloor\log_2(129775) \times 140\rfloor' align=absmiddle>), so if we use a 16-colour palette we can store about 594 pixels (<img src='/blog/wp-content/plugins/latexrender/pictures/cba7624bee8dfdd5dae1931dda7f495d.gif' title='2377/\log_2(16)' alt='2377/\log_2(16)' align=absmiddle>), which can <em>almost</em> reproduce the <i>Mona Lisa</i> thumbnail in the contest page.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/what-can-we-fit-in-140-characters/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rhythmbox Plugin: Stop after current track</title>
		<link>http://porg.es/blog/rhythmbox-plugin-stop-after-current-track</link>
		<comments>http://porg.es/blog/rhythmbox-plugin-stop-after-current-track#comments</comments>
		<pubDate>Tue, 07 Apr 2009 05:18:24 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[rhythmbox]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=324</guid>
		<description><![CDATA[I have wanted this for a while, and my brother linking me to this post was the last straw. So here is a very quick and simple plugin; it simply puts a button on the toolbar that you can click when you want to stop playback after the current song. I based the toolbar button [...]]]></description>
			<content:encoded><![CDATA[<p>I have wanted this for a while, and my brother linking me to <a href="http://unadorned.org/dandruff/archives/2004/02/24/002324.html">this post</a> was the last straw.</p>
<p>So here is a very quick and simple plugin; it simply puts a button on the toolbar that you can click when you want to stop playback after the current song. I based the toolbar button code on <a href="http://code.google.com/p/airmindprojects/">Alexandre Rosenfeld’s lastfm-queue plugin</a>, since I had no idea where to start with that <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_smile.gif" alt="" /></p>
<p>Download it <a href='http://porg.es/blog/wp-content/uploads/2009/04/stop_after_song.zip'>here</a>, and put it into <code>~/.gnome2/rhythmbox/plugins/stop_after_song/</code>. Activate it in Rhythmbox’s plugin dialog.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/rhythmbox-plugin-stop-after-current-track/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Making Maya&#8217;s Python palatable</title>
		<link>http://porg.es/blog/making-mayas-python-palatable</link>
		<comments>http://porg.es/blog/making-mayas-python-palatable#comments</comments>
		<pubDate>Tue, 03 Mar 2009 04:57:12 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[criticism]]></category>
		<category><![CDATA[maya]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[short]]></category>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

<p>You may even discover that the existing <code>f</code> is used in a lot of lambdas that could be avoided by rearranging its arguments!</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/refactoring-in-haskell-adding-an-argument/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Presenting Meteoroids</title>
		<link>http://porg.es/blog/presenting-meteoroids</link>
		<comments>http://porg.es/blog/presenting-meteoroids#comments</comments>
		<pubDate>Wed, 19 Nov 2008 23:23:37 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[self]]></category>
		<category><![CDATA[university]]></category>
		<category><![CDATA[Game]]></category>
		<category><![CDATA[massey]]></category>
		<category><![CDATA[meteoroids]]></category>
		<category><![CDATA[uni]]></category>
		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=244</guid>
		<description><![CDATA[This post brought to you as part of fulfilling the requirements for Massey University’s 143.362. I thought I had posted this earlier, but it turns out I hadn&#8217;t. So, it&#8217;s here now Meteoroids Meteoroids is a simple game (the premise being an homage to the classic Asteroids-based titles), programmed in Processing. Audio is provided via [...]]]></description>
			<content:encoded><![CDATA[<p><i>This post brought to you as part of fulfilling the requirements for Massey University’s <a href="http://www.massey.ac.nz/massey/study/programme-course-and-paper-search/paper.cfm?paper_code=143.362">143.362</a>.</i></p>
<p>I thought I had posted this earlier, but it turns out I hadn&#8217;t. So, it&#8217;s here now <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_razz.gif" alt="" /></p>
<h3>Meteoroids</h3>
<p>Meteoroids is a simple game (the premise being an homage to the classic Asteroids-based titles), programmed in <a href="http://processing.org/">Processing</a>. Audio is provided via <a href="http://chuck.cs.princeton.edu/">ChucK</a>, and input is provided by a Nintendo Wiimote via <a href="http://en.wikipedia.org/wiki/OpenSound_Control">OSC</a> commands generated from <a href="http://code.google.com/p/darwiinosc/">an extension</a> of <a href="http://sourceforge.net/projects/darwiin-remote/">Darwiin Remote</a>.</p>
<p>To explain more, here&#8217;s a short video <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_smile.gif" alt="" /></p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/_NdgujV3z5c&#038;hl=en&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/_NdgujV3z5c&#038;hl=en&#038;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/presenting-meteoroids/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Vacuuming Firefox&#8217;s sqlite3 databases</title>
		<link>http://porg.es/blog/vacuuming-firefoxs-sqlite3-databases</link>
		<comments>http://porg.es/blog/vacuuming-firefoxs-sqlite3-databases#comments</comments>
		<pubDate>Sun, 16 Nov 2008 23:56:39 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[short]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=239</guid>
		<description><![CDATA[Firefox 3 was running very slow for me, so I desperately tried this, thinking that it wouldn&#8217;t make much difference. I was wrong; there was a noticeable speed improvement. From a bash shell: find ~/.mozilla -iname '*.sqlite' -execdir sqlite3 &#123;&#125; 'vacuum;' \;]]></description>
			<content:encoded><![CDATA[<p>Firefox 3 was running very slow for me, so I desperately tried this, thinking that it wouldn&#8217;t make much difference. I was wrong; there was a noticeable speed improvement. From a bash shell:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> ~<span style="color: #000000; font-weight: bold;">/</span>.mozilla <span style="color: #660033;">-iname</span> <span style="color: #ff0000;">'*.sqlite'</span> <span style="color: #660033;">-execdir</span> sqlite3 <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span> <span style="color: #ff0000;">'vacuum;'</span> \;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/vacuuming-firefoxs-sqlite3-databases/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Music Synthesizer Project (in Matlab)</title>
		<link>http://porg.es/blog/music-synthesizer-project-in-matlab</link>
		<comments>http://porg.es/blog/music-synthesizer-project-in-matlab#comments</comments>
		<pubDate>Fri, 24 Oct 2008 08:35:33 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[university]]></category>
		<category><![CDATA[massey]]></category>
		<category><![CDATA[matlab]]></category>
		<category><![CDATA[music]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[sound]]></category>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

		<guid isPermaLink="false">http://porg.es/blog/?p=130</guid>
		<description><![CDATA[‘Counting Characters in UTF-8 Strings Is Fast’ by Kragen Sitaker shows several ways to count characters UTF-8, using both assembly and C. But, with a few assumptions, we can go faster. Assumption One: We are dealing with a valid UTF-8 string Making this assumption means that once we hit the start of a multi-byte character [...]]]></description>
			<content:encoded><![CDATA[<p>‘<a href="http://canonical.org/~kragen/strlen-utf8.html">Counting Characters in UTF-8 Strings Is Fast</a>’ by Kragen Sitaker shows several ways to count characters UTF-8, using both assembly and C. But, with a few assumptions, we can go faster.</p>
<h3>Assumption One: We are dealing with a valid UTF-8 string</h3>
<p>Making this assumption means that once we hit the start of a multi-byte character we can skip forward a few places. It also means we don&#8217;t check for hitting invalid characters (<s>this sends the algorithm into an infinite loop if run on non-valid input</s> it is possible to make the algorithm run past the end of the buffer by supplying malformed data).</p>
<h3>Assumption Two: Most strings are ASCII</h3>
<p>Therefore, run a simple ASCII count routine beforehand. As soon as we hit a non-ASCII character switch into counting UTF-8.</p>
<h3>The code</h3>
<p>Note: The current code relies on chars being signed bytes.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> porges_strlen2<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span>s<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #993333;">int</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//Go fast if string is only ASCII.</span>
        <span style="color: #666666; font-style: italic;">//Loop while not at end of string,</span>
        <span style="color: #666666; font-style: italic;">// and not reading anything with highest bit set.</span>
        <span style="color: #666666; font-style: italic;">//If highest bit is set, number is negative.</span>
        <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span>
                i<span style="color: #339933;">++;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;=</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">65</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// all follower bytes have values below -65</span>
                <span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// invalid</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//Note, however, that the following code does *not*</span>
        <span style="color: #666666; font-style: italic;">// check for invalid characters.</span>
        <span style="color: #666666; font-style: italic;">//The above is just included to bail out on the tests :)</span>
&nbsp;
        <span style="color: #993333;">int</span> count <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span>
        <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">//if ASCII just go to next character</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span>      i <span style="color: #339933;">+=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">else</span>
                <span style="color: #666666; font-style: italic;">//select amongst multi-byte starters</span>
                <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #208080;">0xF0</span> <span style="color: #339933;">&amp;</span> s<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                        <span style="color: #b1b100;">case</span> <span style="color: #208080;">0xE0</span><span style="color: #339933;">:</span> i <span style="color: #339933;">+=</span> <span style="color: #0000dd;">3</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
                        <span style="color: #b1b100;">case</span> <span style="color: #208080;">0xF0</span><span style="color: #339933;">:</span> i <span style="color: #339933;">+=</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
                        <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>   i <span style="color: #339933;">+=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #339933;">++</span>count<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> count<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Results</h3>
<p>I used Kragen’s testing code, but removed all <code>strlen</code>s that didn’t do UTF-8 counting, and added one test for valid UTF-8 text (just the phrase ‘こんにちは’ repeated). Twice as fast on both the ASCII-only and UTF-8 tests. Improvement on ASCII is due to the ASCII-only routine, and improvement on UTF-8 is due to skipping bytes.</p>
<pre><code>"": 0 0 0 0 0
"hello, world": 12 12 12 12 12
"naïve": 5 5 5 5 5
"こんにちは": 5 5 5 5 5
1: all 'a':
1:           porges_strlen2(string) =   33554431: 0.034672
1:         ap_strlen_utf8_s(string) =   33554431: 0.068210
1:         my_strlen_utf8_c(string) =   33554431: 0.071038
1:         my_strlen_utf8_s(string) =   33554431: 0.135856
2: all '\xe3':
2:           porges_strlen2(string) =   11184811: 0.032115
2:         ap_strlen_utf8_s(string) =   33554431: 0.068228
2:         my_strlen_utf8_c(string) =   33554431: 0.071050
2:         my_strlen_utf8_s(string) =   33554431: 0.152513
3: all '\x81':
3:           porges_strlen2(string) =         -1: 0.000001
3:         my_strlen_utf8_s(string) =          0: 0.068339
3:         ap_strlen_utf8_s(string) =          0: 0.068547
3:         my_strlen_utf8_c(string) =          0: 0.071039
4: all konichiwa:
4:           porges_strlen2(string) =   11184810: 0.032143
4:         ap_strlen_utf8_s(string) =   11184810: 0.068271
4:         my_strlen_utf8_c(string) =   11184810: 0.071036
4:         my_strlen_utf8_s(string) =   11184810: 0.089478
</code></pre>
<p>Note also that the invalid UTF-8 gives strange results; this is because the algorithm isn’t meant to work on it! (The first invalid sequence is a list of 3-byte starters, so the result is divided in 3 due to skipping, and the second is a list of follower bytes, so the code bails out.)</p>
<h3>Going faster</h3>
<p>By dropping back to the ASCII counter whenever we hit ASCII again, we go even faster. This will handle the cases (such as in English) where there are many ASCII characters and only a few multibyte ones.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> porges_strlen2<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span>s<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #993333;">int</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
        <span style="color: #993333;">int</span> iBefore <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
        <span style="color: #993333;">int</span> count <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span>
                ascii<span style="color: #339933;">:</span>  i<span style="color: #339933;">++;</span>
&nbsp;
        count <span style="color: #339933;">+=</span> i<span style="color: #339933;">-</span>iBefore<span style="color: #339933;">;</span>
        <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                        iBefore <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span>
                        <span style="color: #b1b100;">goto</span> ascii<span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">else</span>
                <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #208080;">0xF0</span> <span style="color: #339933;">&amp;</span> s<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                        <span style="color: #b1b100;">case</span> <span style="color: #208080;">0xE0</span><span style="color: #339933;">:</span> i <span style="color: #339933;">+=</span> <span style="color: #0000dd;">3</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
                        <span style="color: #b1b100;">case</span> <span style="color: #208080;">0xF0</span><span style="color: #339933;">:</span> i <span style="color: #339933;">+=</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
                        <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>   i <span style="color: #339933;">+=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #339933;">++</span>count<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> count<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>But on the ‘konichiwa’ test the speed improvement happens even though we’re counting pure multibyte, and I’m not sure exactly why&#8230; probably something to do with branch prediction or another arcane CPU topic I don’t understand. <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_smile.gif" alt="" /></p>
<pre><code>4: all konichiwa:
4:           porges_strlen2(string) =   11184810: 0.026017
4:         ap_strlen_utf8_s(string) =   11184810: 0.068320
4:         my_strlen_utf8_c(string) =   11184810: 0.071035
4:         my_strlen_utf8_s(string) =   11184810: 0.089464
5: mixed english:
5:           porges_strlen2(string) =   32435949: 0.040342
5:         my_strlen_utf8_c(string) =   32435949: 0.071035
5:         ap_strlen_utf8_s(string) =   32435949: 0.078233
5:         my_strlen_utf8_s(string) =   32435949: 0.160676</code></pre>
<p>Without the drop-back-to-ASCII modification:</p>
<pre><code>5: mixed english:
5:           porges_strlen2(string) =   32435949: 0.067753</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/counting-characters-in-utf-8-strings-is-faster/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>A quine in Haskell</title>
		<link>http://porg.es/blog/a-quine-in-haskell</link>
		<comments>http://porg.es/blog/a-quine-in-haskell#comments</comments>
		<pubDate>Wed, 30 Apr 2008 12:32:04 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[quine]]></category>
		<category><![CDATA[snippet]]></category>

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

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

<p>Edit: I have found it somewhere else <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_razz.gif" alt="" /> <a href="http://homepage.mac.com/kpreid/quines.html">Here</a>, to be exact.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/a-quine-in-haskell/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
