<?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/category/code/feed" rel="self" type="application/rss+xml" />
	<link>http://porg.es/blog</link>
	<description></description>
	<lastBuildDate>Sun, 06 May 2012 22:13:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Objects can be collected while their instance methods are still executing</title>
		<link>http://porg.es/blog/objects-can-be-collected-while-their-instance-methods-are-still-executing</link>
		<comments>http://porg.es/blog/objects-can-be-collected-while-their-instance-methods-are-still-executing#comments</comments>
		<pubDate>Wed, 02 May 2012 23:42:56 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=720</guid>
		<description><![CDATA[In Peter Ritchie&#8217;s post Dispose Pattern and “Set large fields to null”, he states the following (my highlighting): At face value, setting a field to null means that the referenced object is now unrooted from the class that owns the field and, if that was the last root of that reference, the Garbage Collector (GC) [...]]]></description>
			<content:encoded><![CDATA[<p>In Peter Ritchie&#8217;s post <a href="http://msmvps.com/blogs/peterritchie/archive/2012/04/26/dispose-pattern-and-set-large-fields-to-null.aspx"><i>Dispose Pattern and “Set large fields to null”</i></a>, he states the following (my highlighting):</p>
<blockquote><p>At face value, setting a field to null means that the referenced object is now unrooted from the class that owns the field and, if that was the last root of that reference, the Garbage Collector (GC) is now free to release the memory used by the object that was referenced by that field.  Although advanced, this seems all very academic because <span style="background:yellow">the amount of time between unrooting the reference and the return from <code>Dispose</code> (and thus the unrooting of the parent object) would seem like a very short amount of time</span>.  Even if the amount of time between these two actions is small, setting a single field to null (i.e. a single assignment) seems like such a minor bit of code to provide no adverse affects.  The prevalent opinion seems to be that the GC “handles” this case and does what is best for you without setting the field to null.</p></blockquote>
<p>In fact, this &#8220;short amount of time&#8221; can be so short as to be negative! A class that nothing else refers to is unrooted as soon as it no longer refers to itself, so objects in .NET can be collected while their instance methods are still executing. I first learned about this from <a href="http://blogs.msdn.com/b/cbrumme/archive/2003/04/19/51365.aspx">Chris Brumme&#8217;s blog</a>. Here is an example that shows this behaviour:</p>
<p><script src="https://gist.github.com/2581933.js"> </script></p>
<p>The output is:</p>
<pre>start method
collected
end method</pre>
<p>Once the method <code>Go()</code> has no references to <code>this</code>, it is eligible for collection. (What this means is that setting fields to <code>null</code> will actually <em>extend</em> the lifetime of the parent object ever-so-slightly.)</p>
<p>However, the code that a <code>using</code> is transformed into will hold onto a reference to the object while its <code>Dispose()</code> method is called, so you won&#8217;t get the same behaviour with the following code:</p>
<p><script src="https://gist.github.com/2581936.js"> </script></p>
<p>The output is:</p>
<pre>start method
end method
collected</pre>
<p>This is because the <code>using</code> statement is transformed to something like this:</p>
<p><script src="https://gist.github.com/2582061.js"> </script></p>
<p>I <em>think</em> (but cannot confirm) that the reference being held is that of <code>Slow</code> in the outer scope. We can restore the previous behaviour if the <code>using</code> transform was written as this instead:</p>
<p><script src="https://gist.github.com/2582077.js"> </script> </p>
<p>Either way, I wouldn&#8217;t bother setting fields to <code>null</code> in the <code>Dispose()</code> method. (For one thing, these fields can no longer be declared <code>readonly</code>.) And most of the time, the GC is smarter than you are.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/objects-can-be-collected-while-their-instance-methods-are-still-executing/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>On i, j, &#8230; as iteration variables (but really a foray into primary sources)</title>
		<link>http://porg.es/blog/on-i-j-as-iteration-variables-but-really-a-foray-into-primary-sources</link>
		<comments>http://porg.es/blog/on-i-j-as-iteration-variables-but-really-a-foray-into-primary-sources#comments</comments>
		<pubDate>Tue, 10 Jan 2012 09:46:10 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[fortran]]></category>
		<category><![CDATA[history]]></category>
		<category><![CDATA[notation]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=480</guid>
		<description><![CDATA[This question was recently asked on StackOverflow: I know this might seem like an absolutely silly question to ask, yet I am too curious not to ask&#8230; Why did &#8220;i&#8221; and &#8220;j&#8221; become THE variables to use as counters in most control structures? The question has generated many answers, from scholarly to spurious &#8212; but [...]]]></description>
			<content:encoded><![CDATA[<p>This question was recently asked on StackOverflow:</p>
<blockquote><p>I know this might seem like an absolutely silly question to ask, yet I am too curious not to ask&#8230;</p>
<p><b><a href="http://stackoverflow.com/questions/4137785/why-are-variables-i-and-j-used-for-counters">Why did &#8220;i&#8221; and &#8220;j&#8221; become THE variables to use as counters in most control structures?</a></b></p></blockquote>
<p>The question has generated many answers, from scholarly to spurious &mdash; but the thing that has struck me is that no one has attempted to cite their sources or do any research. Why is this, when we live in a time when primary sources are more widely available than ever?</p>
<p>Let&#8217;s start with the claims that FORTRAN was the original source for their use in programing languages&mdash;while perhaps not the ultimate origin, it may have been the reason that they became widespread in the programming community.</p>
<p>The original manual for Fortran <a class="simple-footnote" title="It isn&#8217;t written FORTRAN here. I&#8217;m not sure of the nuances of its capitalization." id="return-note-480-1" href="#note-480-1"><sup>1</sup></a> for the IBM 704 is <a href="http://www.fh-jena.de/~kleine/history/languages/FortranAutomaticCodingSystemForTheIBM704.pdf">readily available online</a>. The first thing I notice is the glorious cover:</p>
<p><a href="http://porg.es/blog/wp-content/uploads/2010/12/FORTRAN.jpg"><img src="http://porg.es/blog/wp-content/uploads/2010/12/FORTRAN.jpg" alt="" title="Automatic coding system for the IBM 704" width="563" height="372" class="aligncenter size-full wp-image-482" /></a></p>
<p>And sure enough, we can find the definition for the integral variables:</p>
<p><a href="http://porg.es/blog/wp-content/uploads/2010/12/fortran_variables.jpg"><img src="http://porg.es/blog/wp-content/uploads/2010/12/fortran_variables.jpg" alt="" title="fortran_variables" width="612" height="280" class="aligncenter size-full wp-image-484" /></a></p>
<p>Unfortunately, the path stops here. I can&#8217;t find any references by Backus (or anyone else) as to why they chose IJKLMN as the integer variables. However, due to the fact that integer variables in Fortran &#8220;are somewhat restricted in their use and serve primarily as subscripts or exponents&#8221;, <a class="simple-footnote" title="J.W. Backus, R.J. Beeber, S. Best, R. Goldberg, L.M. Haibt, H.L. Herrick, R.A. Nelson, D. Sayre, P.B. Sheridan, H.J. Stern, I. Ziller, R.A. Hughes, and R. Nutt, The FORTRAN automatic coding system. Pages 188-198. In Proceedings Western Joint Computer Conference, Los Angeles, California, February 1957." id="return-note-480-2" href="#note-480-2"><sup>2</sup></a> I am forced to the conclusion that they were used in imitation of those in mathematics. I don&#8217;t think we&#8217;ll ever know exactly who or when they were introduced to Fortran itself.</p>
<p>What we can do, however, is have a look at when they arose in mathematics. The usual place that i, j, etc. arise is in &#8216;sigma notation&#8217;, using the summation operator Σ. For example, if we write:</p>
<p><a href="http://porg.es/blog/wp-content/uploads/2010/12/sigmanotation.png"><img src="http://porg.es/blog/wp-content/uploads/2010/12/sigmanotation.png" alt="" title="Sigma notation example" width="38" height="52" class="aligncenter size-full wp-image-507" /></a></p>
<p>We mean i (= 1) + i (= 2) + i (= 3), until i = 100, and we can calculate the answer as 1 + 2 + 3 + 4 + &#8230; = 5050. So where did this notation itself come from?</p>
<p>The standard work on the history of mathematical notations is <i>A History of Mathematical Notations</i> by Florian Cajori. <a class="simple-footnote" title="Unfortunately, only the first volume appears to be readily available online. You can see some of the second volume on Google Books." id="return-note-480-3" href="#note-480-3"><sup>3</sup></a> He states that Σ was first used by Euler, in his <a href="http://books.google.co.nz/books?id=sYE_AAAAcAAJ"><i>Institutiones calculi differentialis</i></a> (1755). We can see the part in question here:</p>
<p><a href="http://porg.es/blog/wp-content/uploads/2010/12/euler.jpg"><img src="http://porg.es/blog/wp-content/uploads/2010/12/euler.jpg" alt="" title="euler" width="539" height="233" class="aligncenter size-full wp-image-511" /></a></p>
<p>This reads (translation by Ian Bruce, from <a href="http://17thcenturymaths.com/">17thCentryMaths.com</a>):</p>
<blockquote><p>
26. Just as we have been accustomed to specify the difference by the sign <i>Δ</i>, thus we will indicate the sum by the sign <i>Σ</i>; evidently if the difference of the function <i>y</i> were <i>z</i>, there will be <i>z</i> = <i>Δy</i>; from which, if <i>y</i> may be given, the difference <i>z</i> is found we have shown before. But if moreover the difference <i>z</i>  shall be given and the sum of this <i>y</i> must be found, <i>y</i> = <i>Σz</i> is made and evidently from the equation <i>z</i> = <i>Δy</i> on regressing this equation will have the form <i>y</i> = <i>Σz</i>, where some constant quantity can be added on account of the reasons given above;  [&#8230;]</p></blockquote>
<p>Evidently this is not the Σ we are looking for, as Euler uses it only in opposition to Δ (for finite differencing). In fact, Cajori notes that Euler&#8217;s Σ &#8220;received little attention&#8221;, and it seems that only Lagrange adopted it. Here is an excerpt from his <a href="http://www.archive.org/details/oeuvrespublies03lagruoft">Œuvres</a> (printed <a href="http://www.wolframalpha.com/input/?i=MDCCCLXIX">MDCCCLXIX</a>):</p>
<p><a href="http://porg.es/blog/wp-content/uploads/2010/12/lagrange.jpg"><img src="http://porg.es/blog/wp-content/uploads/2010/12/lagrange.jpg" alt="" title="lagrange" width="383" height="338" class="aligncenter size-full wp-image-519" /></a></p>
<p>Again, we can see Σ is only used in opposition to Δ. Cajori next states that Σ to mean &#8220;sum&#8221; was used by Fourier, in his <a href="http://www.archive.org/details/thorieanalytiq00four"><i>Théorie Analytique de la chaleur</i></a> (1822), and here we find what we&#8217;re looking for:</p>
<p><a href="http://porg.es/blog/wp-content/uploads/2010/12/fourier.jpg"><img src="http://porg.es/blog/wp-content/uploads/2010/12/fourier.jpg" alt="" title="fourier" width="500" height="414" class="aligncenter size-full wp-image-522" /></a></p>
<blockquote><p>The sign Σ affects the number <i>i</i> and indicates that the sum must be taken from <i>i</i>=1 to <i>i</i>=1/0. One can also contain the first term 1 under the sign Σ, and we have: [equation]</p>
<p>It must then have all its integral values from -1/0 up to 1/0; that is what one indicates by writing the limits -1/0 and +1/0 next to the sign Σ, that one of the values of <i>i</i> is 0. This is the most concise expression of the solution. <a class="simple-footnote" title="Note that Fourier has no qualms about writing -1/0 and +1/0!" id="return-note-480-4" href="#note-480-4"><sup>4</sup></a></p></blockquote>
<p>Since Fourier explains Σ several times in the book, and not just once, we can assume that the notation is either new or unfamiliar to most readers. <a class="simple-footnote" title="Knuth also states that the notation arrived with Fourier, so I guess I&#8217;m not in bad company." id="return-note-480-5" href="#note-480-5"><sup>5</sup></a> In any case, it doesn&#8217;t really matter who invented it, because while we have found our Σ, Fourier doesn&#8217;t explain why he uses <i>i</i>. In fact, since he uses it to index sequences in other places it appears it must be an already-existing usage. <a class="simple-footnote" title="While i is often used as (one of) the indices for a matrix, true matrices weren&#8217;t developed until after Fourier&#8217;s book was published, we must look elsewhere." id="return-note-480-6" href="#note-480-6"><sup>6</sup></a></p>
<p>A quick glance at the text by Euler above shows that he uses indexing very rarely (despite the subject of the text being a prime candidate!), and when he does, he uses <i>m</i>.</p>
<p>And, this is as far as I got. Time to publish this.</p>
<div class="simple-footnotes"><p class="notes">Notes:</p><ol><li id="note-480-1">It isn&#8217;t written FORTRAN here. I&#8217;m not sure of the nuances of its capitalization. <a href="#return-note-480-1">&#8617;</a></li><li id="note-480-2">J.W. Backus, R.J. Beeber, S. Best, R. Goldberg, L.M. Haibt, H.L. Herrick, R.A. Nelson, D. Sayre, P.B. Sheridan, H.J. Stern, I. Ziller, R.A. Hughes, and R. Nutt, <a href="http://archive.computerhistory.org/resources/text/Fortran/102663113.05.01.acc.pdf">The FORTRAN automatic coding system</a>. Pages 188-198. In <i>Proceedings Western Joint Computer Conference</i>, Los Angeles, California, February 1957. <a href="#return-note-480-2">&#8617;</a></li><li id="note-480-3">Unfortunately, only <a href="http://www.archive.org/details/historyofmathema031756mbp">the first volume</a> appears to be readily available online. You can see <a href="http://books.google.co.nz/books?id=bT5suOONXlgC">some of the second volume</a> on Google Books. <a href="#return-note-480-3">&#8617;</a></li><li id="note-480-4">Note that Fourier has no qualms about writing -1/0 and +1/0! <a href="#return-note-480-4">&#8617;</a></li><li id="note-480-5">Knuth also states that the notation arrived with Fourier, so I guess I&#8217;m not in bad company</a>. <a href="#return-note-480-5">&#8617;</a></li><li id="note-480-6">While <i>i</i> is often used as (one of) the indices for a matrix, true matrices weren&#8217;t developed until after Fourier&#8217;s book was published, we must look elsewhere. <a href="#return-note-480-6">&#8617;</a></li></ol></div>]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/on-i-j-as-iteration-variables-but-really-a-foray-into-primary-sources/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Casting in .NET via object mutation</title>
		<link>http://porg.es/blog/casting-in-dot-net-via-object-mutation</link>
		<comments>http://porg.es/blog/casting-in-dot-net-via-object-mutation#comments</comments>
		<pubDate>Fri, 13 May 2011 10:37:03 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[horrid]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=624</guid>
		<description><![CDATA[In this post, we will see how to make the following code fail: object it = new SomeStruct &#123; Item = 1 &#125;; &#160; Floatsy&#40;it&#41;; &#160; Console.WriteLine&#40;&#40;&#40;SomeStruct&#41;it&#41;.Item&#41;; At runtime, it will throw an InvalidCastException! Here&#8217;s how. In .NET, each object has also associated with it a value which determines the type of the object. In [...]]]></description>
			<content:encoded><![CDATA[<p>In this post, we will see how to make the following code fail:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">	<span style="color: #6666cc; font-weight: bold;">object</span> it <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SomeStruct <span style="color: #008000;">&#123;</span> Item <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
	Floatsy<span style="color: #008000;">&#40;</span>it<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>SomeStruct<span style="color: #008000;">&#41;</span>it<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Item</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>At runtime, it will throw an <code>InvalidCastException</code>!</p>
<p><span id="more-624"></span></p>
<p>Here&#8217;s how. In .NET, each object has also associated with it a value which determines the type of the object. In memory, this is stored before the object&#8217;s data, like so (I got this information from the MSDN article <a href="http://msdn.microsoft.com/en-us/magazine/cc163791.aspx">Drill Into .NET Framework Internals to See How the CLR Creates Runtime Objects</a>):</p>
<p><center><a href="http://porg.es/blog/wp-content/uploads/2011/05/layout2.png"><img src="http://porg.es/blog/wp-content/uploads/2011/05/layout2.png" alt="" title="layout" width="191" height="176" class="aligncenter size-full wp-image-634" /></a></center></p>
<p>Now, if we can write to that, we can set the type of the object to whatever we want!</p>
<p>There&#8217;s one small problem &mdash; in .NET we can&#8217;t take the address of a managed object (which we need in order to write to the object in memory). There are various reasons for this, one of them being that the garbage collector likes to be able to move objects around. Being able to take arbitrary pointers of objects would mean that the pointers could become invalidated.</p>
<p>What we <em>can</em> do is to pin a struct, which allows us to retrieve the address of it (this facility exists so that users can pass pinned managed structs into unmanaged code as pointers). Here&#8217;s how to pin a struct:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    var handle <span style="color: #008000;">=</span> GCHandle<span style="color: #008000;">.</span><span style="color: #0000FF;">Alloc</span><span style="color: #008000;">&#40;</span>o, GCHandleType<span style="color: #008000;">.</span><span style="color: #0000FF;">Pinned</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    var addr <span style="color: #008000;">=</span> handle<span style="color: #008000;">.</span><span style="color: #0000FF;">AddrOfPinnedObject</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>We can then write to the type handle like so (both the sync block and type handle are 32 bits):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #008000;">*</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">*</span><span style="color: #008000;">&#41;</span>addr<span style="color: #008000;">&#41;</span><span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=</span> someValue<span style="color: #008000;">;</span></pre></div></div>

<p>Another problem is that all of this is only true for values on the heap. As far as I can tell, the static type of the variable is what .NET uses to identify values on the stack. So in order for this to work, we must use a boxed copy of a struct.</p>
<hr/>
<p>Finally, here&#8217;s some demo code, showing runtime changing of types for both a primitive and custom struct:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Runtime.InteropServices</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">unsafe</span> <span style="color: #6666cc; font-weight: bold;">class</span> Program
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">struct</span> SomeStruct <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Item<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">object</span> notAFloat <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
        <span style="color: #6666cc; font-weight: bold;">object</span> me <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SomeStruct <span style="color: #008000;">&#123;</span>Item <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;{0} ({1})&quot;</span>, notAFloat, notAFloat<span style="color: #008000;">.</span><span style="color: #0000FF;">GetType</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Floatify<span style="color: #008000;">&#40;</span>notAFloat<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;{0} ({1})&quot;</span>, notAFloat, notAFloat<span style="color: #008000;">.</span><span style="color: #0000FF;">GetType</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>me<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Floatify<span style="color: #008000;">&#40;</span>me<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>me<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Floatify<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>T o<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        var handle <span style="color: #008000;">=</span> GCHandle<span style="color: #008000;">.</span><span style="color: #0000FF;">Alloc</span><span style="color: #008000;">&#40;</span>o, GCHandleType<span style="color: #008000;">.</span><span style="color: #0000FF;">Pinned</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        var addr <span style="color: #008000;">=</span> handle<span style="color: #008000;">.</span><span style="color: #0000FF;">AddrOfPinnedObject</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #6666cc; font-weight: bold;">object</span> f <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">float</span><span style="color: #008000;">&#41;</span><span style="color: #FF0000;">1.0</span><span style="color: #008000;">;</span>
        var handle2 <span style="color: #008000;">=</span> GCHandle<span style="color: #008000;">.</span><span style="color: #0000FF;">Alloc</span><span style="color: #008000;">&#40;</span>f, GCHandleType<span style="color: #008000;">.</span><span style="color: #0000FF;">Pinned</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        var addr2 <span style="color: #008000;">=</span> handle2<span style="color: #008000;">.</span><span style="color: #0000FF;">AddrOfPinnedObject</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// copy type handle of a float to the object</span>
        <span style="color: #008000;">*</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">*</span><span style="color: #008000;">&#41;</span>addr<span style="color: #008000;">&#41;</span><span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">*</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">*</span><span style="color: #008000;">&#41;</span>addr2<span style="color: #008000;">&#41;</span><span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        handle2<span style="color: #008000;">.</span><span style="color: #0000FF;">Free</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        handle<span style="color: #008000;">.</span><span style="color: #0000FF;">Free</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The output is:</p>
<p><center>
<pre>1 (System.Int32)
1.401298E-45 (System.Single)

Program+SomeStruct
1.401298E-45</pre>
<p></center></p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/casting-in-dot-net-via-object-mutation/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Validating email addresses with .NET regex</title>
		<link>http://porg.es/blog/validating-email-addresses-with-dot-net-regex</link>
		<comments>http://porg.es/blog/validating-email-addresses-with-dot-net-regex#comments</comments>
		<pubDate>Tue, 10 May 2011 04:46:07 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=613</guid>
		<description><![CDATA[I did validation in Haskell a while back, and since I recently discovered .NET&#8217;s &#8220;balancing groups&#8221; regex feature, it seems like it would be a good time to do it for .NET. Here is the code: And here is the regex. Despite supporting comments and a more recent RFC, it&#8217;s about ⅔ of the size [...]]]></description>
			<content:encoded><![CDATA[<p>I did <a href="http://porg.es/blog/email-address-validation-simpler-faster-more-correct">validation in Haskell</a> a while back, and since I recently discovered .NET&#8217;s &#8220;balancing groups&#8221; regex feature, it seems like it would be a good time to do it for .NET.</p>
<p><span id="more-613"></span>
<p>Here is the code:</p>
<div id="gist-1603852" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kt">var</span> <span class="n">crlf</span> <span class="p">=</span> <span class="s">@&quot;(\r\n)&quot;</span><span class="p">;</span></div><div class='line' id='LC2'><span class="kt">var</span> <span class="n">wsp</span> <span class="p">=</span> <span class="s">@&quot;[ \t]&quot;</span><span class="p">;</span></div><div class='line' id='LC3'><span class="kt">var</span> <span class="n">vchar</span> <span class="p">=</span> <span class="s">@&quot;[\u0021-\u007e]&quot;</span><span class="p">;</span></div><div class='line' id='LC4'><span class="kt">var</span> <span class="n">obsNoWsCtl</span> <span class="p">=</span> <span class="s">@&quot;[\u0001-\u0008\u000b\u000c\u000e-\u001f\u007f]&quot;</span><span class="p">;</span></div><div class='line' id='LC5'><span class="kt">var</span> <span class="n">commentText</span> <span class="p">=</span> <span class="s">@&quot;([\u0021-\u0027\u002a-\u005b\u005d-\u007e]|&quot;</span> <span class="p">+</span> <span class="n">obsNoWsCtl</span> <span class="p">+</span> <span class="s">&quot;)&quot;</span><span class="p">;</span></div><div class='line' id='LC6'><span class="kt">var</span> <span class="n">quotedPair</span> <span class="p">=</span> <span class="s">@&quot;\\(&quot;</span> <span class="p">+</span> <span class="n">vchar</span> <span class="p">+</span> <span class="s">&quot;|&quot;</span> <span class="p">+</span> <span class="n">wsp</span> <span class="p">+</span> <span class="s">@&quot;|[\r\n\0]|&quot;</span> <span class="p">+</span> <span class="n">obsNoWsCtl</span> <span class="p">+</span> <span class="s">&quot;)&quot;</span><span class="p">;</span></div><div class='line' id='LC7'><span class="kt">var</span> <span class="n">quotedText</span> <span class="p">=</span> <span class="s">@&quot;([\u0021\u0023-\u005b\u005d-\u007e]|&quot;</span><span class="p">+</span><span class="n">obsNoWsCtl</span><span class="p">+</span><span class="s">&quot;)&quot;</span><span class="p">;</span></div><div class='line' id='LC8'><span class="kt">var</span> <span class="n">domainText</span> <span class="p">=</span> <span class="s">&quot;([\u0021-\u005A\u005e-\u007e]|&quot;</span> <span class="p">+</span> <span class="n">obsNoWsCtl</span> <span class="p">+</span> <span class="s">&quot;)&quot;</span><span class="p">;</span></div><div class='line' id='LC9'><span class="kt">var</span> <span class="n">atomText</span> <span class="p">=</span> <span class="s">&quot;[a-zA-Z0-9!#$%&amp;&#39;*+/=?^_`{|}~-]&quot;</span><span class="p">;</span></div><div class='line' id='LC10'><span class="kt">var</span> <span class="n">fws</span> <span class="p">=</span> <span class="s">&quot;(&quot;</span> <span class="p">+</span> <span class="n">wsp</span> <span class="p">+</span> <span class="s">&quot;+&quot;</span> <span class="p">+</span> <span class="s">&quot;(&quot;</span> <span class="p">+</span> <span class="n">crlf</span> <span class="p">+</span> <span class="n">wsp</span> <span class="p">+</span> <span class="s">&quot;+)?|(&quot;</span> <span class="p">+</span> <span class="n">crlf</span> <span class="p">+</span> <span class="n">wsp</span> <span class="p">+</span> <span class="s">&quot;+)+)&quot;</span><span class="p">;</span>   </div><div class='line' id='LC11'><span class="kt">var</span> <span class="n">comment</span> <span class="p">=</span> <span class="s">@&quot;(\((((?&#39;paren&#39;\()|(?&#39;-paren&#39;\))|&quot;</span><span class="p">+</span><span class="n">commentText</span><span class="p">+</span><span class="s">&quot;|&quot;</span><span class="p">+</span><span class="n">fws</span><span class="p">+</span><span class="s">&quot;|&quot;</span><span class="p">+</span><span class="n">quotedPair</span><span class="p">+</span><span class="s">@&quot;)*(?(paren)(?!)))\))&quot;</span><span class="p">;</span></div><div class='line' id='LC12'><span class="kt">var</span> <span class="n">cfws</span> <span class="p">=</span> <span class="s">&quot;(&quot;</span> <span class="p">+</span> <span class="n">comment</span> <span class="p">+</span> <span class="s">&quot;|&quot;</span> <span class="p">+</span> <span class="n">fws</span> <span class="p">+</span> <span class="s">&quot;)*&quot;</span><span class="p">;</span></div><div class='line' id='LC13'><span class="kt">var</span> <span class="n">atom</span> <span class="p">=</span> <span class="s">&quot;(&quot;</span> <span class="p">+</span> <span class="n">atomText</span> <span class="p">+</span> <span class="s">&quot;+)&quot;</span><span class="p">;</span></div><div class='line' id='LC14'><span class="kt">var</span> <span class="n">quotedContent</span> <span class="p">=</span> <span class="s">&quot;(&quot;</span> <span class="p">+</span> <span class="n">quotedText</span> <span class="p">+</span> <span class="s">&quot;|&quot;</span> <span class="p">+</span> <span class="n">quotedPair</span> <span class="p">+</span> <span class="s">&quot;)&quot;</span><span class="p">;</span></div><div class='line' id='LC15'><span class="kt">var</span> <span class="n">quotedString</span> <span class="p">=</span> <span class="s">&quot;(\&quot;(&quot;</span> <span class="p">+</span> <span class="n">fws</span> <span class="p">+</span><span class="s">&quot;?&quot;</span> <span class="p">+</span> <span class="n">quotedContent</span> <span class="p">+</span> <span class="s">&quot;)*&quot;</span> <span class="p">+</span> <span class="n">fws</span> <span class="p">+</span> <span class="s">&quot;?\&quot;)&quot;</span><span class="p">;</span></div><div class='line' id='LC16'><span class="kt">var</span> <span class="n">dottedAtoms1</span> <span class="p">=</span> <span class="s">&quot;(&quot;</span> <span class="p">+</span> <span class="n">cfws</span> <span class="p">+</span> <span class="s">&quot;?(&quot;</span> <span class="p">+</span> <span class="n">atom</span> <span class="p">+</span> <span class="s">&quot;|&quot;</span> <span class="p">+</span> <span class="n">quotedString</span> <span class="p">+</span> <span class="s">&quot;)&quot;</span>  <span class="p">+</span> <span class="n">cfws</span> <span class="p">+</span> <span class="s">&quot;?)&quot;</span><span class="p">;</span></div><div class='line' id='LC17'><span class="kt">var</span> <span class="n">dottedAtoms</span> <span class="p">=</span> <span class="s">&quot;(&quot;</span> <span class="p">+</span> <span class="n">dottedAtoms1</span> <span class="p">+</span> <span class="s">@&quot;(\.&quot;</span> <span class="p">+</span> <span class="n">dottedAtoms1</span> <span class="p">+</span> <span class="s">&quot;)*)&quot;</span><span class="p">;</span></div><div class='line' id='LC18'><span class="kt">var</span> <span class="n">localPart</span> <span class="p">=</span> <span class="s">&quot;(?&#39;localPart&#39;&quot;</span> <span class="p">+</span> <span class="n">dottedAtoms</span> <span class="p">+</span> <span class="s">&quot;)&quot;</span><span class="p">;</span></div><div class='line' id='LC19'><span class="kt">var</span> <span class="n">domainLiteral</span> <span class="p">=</span> <span class="s">&quot;(&quot;</span> <span class="p">+</span> <span class="n">cfws</span> <span class="p">+</span> <span class="s">@&quot;?\[(&quot;</span> <span class="p">+</span> <span class="n">fws</span> <span class="p">+</span> <span class="s">&quot;?&quot;</span> <span class="p">+</span> <span class="n">domainText</span> <span class="p">+</span> <span class="s">&quot;)*&quot;</span> <span class="p">+</span> <span class="n">fws</span> <span class="p">+</span> <span class="s">@&quot;?\]&quot;</span> <span class="p">+</span> <span class="n">cfws</span> <span class="p">+</span><span class="s">&quot;?)&quot;</span><span class="p">;</span></div><div class='line' id='LC20'><span class="kt">var</span> <span class="n">domain</span> <span class="p">=</span> <span class="s">&quot;(?&#39;domain&#39;&quot;</span> <span class="p">+</span> <span class="n">dottedAtoms</span> <span class="p">+</span> <span class="s">&quot;|&quot;</span> <span class="p">+</span> <span class="n">domainLiteral</span> <span class="p">+</span> <span class="s">&quot;)&quot;</span><span class="p">;</span></div><div class='line' id='LC21'><span class="kt">var</span> <span class="n">email</span> <span class="p">=</span> <span class="s">&quot;^&quot;</span> <span class="p">+</span> <span class="n">localPart</span> <span class="p">+</span> <span class="s">&quot;@&quot;</span> <span class="p">+</span> <span class="n">domain</span> <span class="p">+</span> <span class="s">@&quot;\z&quot;</span><span class="p">;</span></div><div class='line' id='LC22'>&nbsp;</div><div class='line' id='LC23'><span class="c1">// Console.WriteLine(email);</span></div><div class='line' id='LC24'>&nbsp;</div><div class='line' id='LC25'><span class="kt">var</span> <span class="n">valid</span> <span class="p">=</span> <span class="s">@&quot;I.                        </span></div><div class='line' id='LC26'><span class="s"> am.                  </span></div><div class='line' id='LC27'><span class="s"> a.      </span></div><div class='line' id='LC28'><span class="s"> nice.</span></div><div class='line' id='LC29'><span class="s"> guy@(yeah)you.com&quot;</span><span class="p">;</span></div><div class='line' id='LC30'>&nbsp;</div><div class='line' id='LC31'><span class="kt">var</span> <span class="n">success</span> <span class="p">=</span> <span class="n">Regex</span><span class="p">.</span><span class="n">Match</span><span class="p">(</span><span class="n">valid</span><span class="p">,</span> <span class="n">email</span><span class="p">,</span> <span class="n">RegexOptions</span><span class="p">.</span><span class="n">ExplicitCapture</span><span class="p">|</span><span class="n">RegexOptions</span><span class="p">.</span><span class="n">Singleline</span><span class="p">);</span></div><div class='line' id='LC32'><span class="n">Console</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="s">&quot;Parsed: {0} &lt;@&gt; {1}&quot;</span><span class="p">,</span> <span class="n">success</span><span class="p">.</span><span class="n">Groups</span><span class="p">[</span><span class="s">&quot;localPart&quot;</span><span class="p">],</span> <span class="n">success</span><span class="p">.</span><span class="n">Groups</span><span class="p">[</span><span class="s">&quot;domain&quot;</span><span class="p">]);</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1603852/172648a02cf3c779f6dd2b6b513be7345085eed1/gistfile1.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1603852#file_gistfile1.cs" style="float:right;margin-right:10px;color:#666">gistfile1.cs</a>
            <a href="https://gist.github.com/1603852">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>And here is the regex. Despite supporting comments and a more recent RFC, it&#8217;s about ⅔ of the size of Perl&#8217;s <a href="http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html">Mail::RFC822::Address</a> .</p>
<p><code style="white-space:nowrap">^(?'localPart'((((\((((?'paren'\()|(?'-paren'\))|([\u0021-\u<br />
0027\u002a-\u005b\u005d-\u007e]|[\u0001-\u0008\u000b\u000c\u<br />
000e-\u001f\u007f])|([ \t]+((\r\n)[ \t]+)?|((\r\n)[ \t]+)+)|<br />
\\([\u0021-\u007e]|[ \t]|[\r\n\0]|[\u0001-\u0008\u000b\u000c<br />
\u000e-\u001f\u007f]))*(?(paren)(?!)))\))|([ \t]+((\r\n)[ \t<br />
]+)?|((\r\n)[ \t]+)+))*?(([a-zA-Z0-9!#$%&amp;'*+/=?^_`{|}~-]+)|(<br />
"(([ \t]+((\r\n)[ \t]+)?|((\r\n)[ \t]+)+)?(([\u0021\u0023-\u<br />
005b\u005d-\u007e]|[\u0001-\u0008\u000b\u000c\u000e-\u001f\u<br />
007f])|\\([\u0021-\u007e]|[ \t]|[\r\n\0]|[\u0001-\u0008\u000<br />
b\u000c\u000e-\u001f\u007f])))*([ \t]+((\r\n)[ \t]+)?|((\r\n<br />
)[ \t]+)+)?"))((\((((?'paren'\()|(?'-paren'\))|([\u0021-\u00<br />
27\u002a-\u005b\u005d-\u007e]|[\u0001-\u0008\u000b\u000c\u00<br />
0e-\u001f\u007f])|([ \t]+((\r\n)[ \t]+)?|((\r\n)[ \t]+)+)|\\<br />
([\u0021-\u007e]|[ \t]|[\r\n\0]|[\u0001-\u0008\u000b\u000c\u<br />
000e-\u001f\u007f]))*(?(paren)(?!)))\))|([ \t]+((\r\n)[ \t]+<br />
)?|((\r\n)[ \t]+)+))*?)(\.(((\((((?'paren'\()|(?'-paren'\))|<br />
([\u0021-\u0027\u002a-\u005b\u005d-\u007e]|[\u0001-\u0008\u0<br />
00b\u000c\u000e-\u001f\u007f])|([ \t]+((\r\n)[ \t]+)?|((\r\n<br />
)[ \t]+)+)|\\([\u0021-\u007e]|[ \t]|[\r\n\0]|[\u0001-\u0008\<br />
u000b\u000c\u000e-\u001f\u007f]))*(?(paren)(?!)))\))|([ \t]+<br />
((\r\n)[ \t]+)?|((\r\n)[ \t]+)+))*?(([a-zA-Z0-9!#$%&amp;'*+/=?^_<br />
`{|}~-]+)|("(([ \t]+((\r\n)[ \t]+)?|((\r\n)[ \t]+)+)?(([\u00<br />
21\u0023-\u005b\u005d-\u007e]|[\u0001-\u0008\u000b\u000c\u00<br />
0e-\u001f\u007f])|\\([\u0021-\u007e]|[ \t]|[\r\n\0]|[\u0001-<br />
\u0008\u000b\u000c\u000e-\u001f\u007f])))*([ \t]+((\r\n)[ \t<br />
]+)?|((\r\n)[ \t]+)+)?"))((\((((?'paren'\()|(?'-paren'\))|([<br />
\u0021-\u0027\u002a-\u005b\u005d-\u007e]|[\u0001-\u0008\u000<br />
b\u000c\u000e-\u001f\u007f])|([ \t]+((\r\n)[ \t]+)?|((\r\n)[<br />
 \t]+)+)|\\([\u0021-\u007e]|[ \t]|[\r\n\0]|[\u0001-\u0008\u0<br />
00b\u000c\u000e-\u001f\u007f]))*(?(paren)(?!)))\))|([ \t]+((<br />
\r\n)[ \t]+)?|((\r\n)[ \t]+)+))*?))*))@(?'domain'((((\((((?'<br />
paren'\()|(?'-paren'\))|([\u0021-\u0027\u002a-\u005b\u005d-\<br />
u007e]|[\u0001-\u0008\u000b\u000c\u000e-\u001f\u007f])|([ \t<br />
]+((\r\n)[ \t]+)?|((\r\n)[ \t]+)+)|\\([\u0021-\u007e]|[ \t]|<br />
[\r\n\0]|[\u0001-\u0008\u000b\u000c\u000e-\u001f\u007f]))*(?<br />
(paren)(?!)))\))|([ \t]+((\r\n)[ \t]+)?|((\r\n)[ \t]+)+))*?(<br />
([a-zA-Z0-9!#$%&amp;'*+/=?^_`{|}~-]+)|("(([ \t]+((\r\n)[ \t]+)?|<br />
((\r\n)[ \t]+)+)?(([\u0021\u0023-\u005b\u005d-\u007e]|[\u000<br />
1-\u0008\u000b\u000c\u000e-\u001f\u007f])|\\([\u0021-\u007e]<br />
|[ \t]|[\r\n\0]|[\u0001-\u0008\u000b\u000c\u000e-\u001f\u007<br />
f])))*([ \t]+((\r\n)[ \t]+)?|((\r\n)[ \t]+)+)?"))((\((((?'pa<br />
ren'\()|(?'-paren'\))|([\u0021-\u0027\u002a-\u005b\u005d-\u0<br />
07e]|[\u0001-\u0008\u000b\u000c\u000e-\u001f\u007f])|([ \t]+<br />
((\r\n)[ \t]+)?|((\r\n)[ \t]+)+)|\\([\u0021-\u007e]|[ \t]|[\<br />
r\n\0]|[\u0001-\u0008\u000b\u000c\u000e-\u001f\u007f]))*(?(p<br />
aren)(?!)))\))|([ \t]+((\r\n)[ \t]+)?|((\r\n)[ \t]+)+))*?)(\<br />
.(((\((((?'paren'\()|(?'-paren'\))|([\u0021-\u0027\u002a-\u0<br />
05b\u005d-\u007e]|[\u0001-\u0008\u000b\u000c\u000e-\u001f\u0<br />
07f])|([ \t]+((\r\n)[ \t]+)?|((\r\n)[ \t]+)+)|\\([\u0021-\u0<br />
07e]|[ \t]|[\r\n\0]|[\u0001-\u0008\u000b\u000c\u000e-\u001f\<br />
u007f]))*(?(paren)(?!)))\))|([ \t]+((\r\n)[ \t]+)?|((\r\n)[<br />
\t]+)+))*?(([a-zA-Z0-9!#$%&amp;'*+/=?^_`{|}~-]+)|("(([ \t]+((\r\<br />
n)[ \t]+)?|((\r\n)[ \t]+)+)?(([\u0021\u0023-\u005b\u005d-\u0<br />
07e]|[\u0001-\u0008\u000b\u000c\u000e-\u001f\u007f])|\\([\u0<br />
021-\u007e]|[ \t]|[\r\n\0]|[\u0001-\u0008\u000b\u000c\u000e-<br />
\u001f\u007f])))*([ \t]+((\r\n)[ \t]+)?|((\r\n)[ \t]+)+)?"))<br />
((\((((?'paren'\()|(?'-paren'\))|([\u0021-\u0027\u002a-\u005<br />
b\u005d-\u007e]|[\u0001-\u0008\u000b\u000c\u000e-\u001f\u007<br />
f])|([ \t]+((\r\n)[ \t]+)?|((\r\n)[ \t]+)+)|\\([\u0021-\u007<br />
e]|[ \t]|[\r\n\0]|[\u0001-\u0008\u000b\u000c\u000e-\u001f\u0<br />
07f]))*(?(paren)(?!)))\))|([ \t]+((\r\n)[ \t]+)?|((\r\n)[ \t<br />
]+)+))*?))*)|(((\((((?'paren'\()|(?'-paren'\))|([\u0021-\u00<br />
27\u002a-\u005b\u005d-\u007e]|[\u0001-\u0008\u000b\u000c\u00<br />
0e-\u001f\u007f])|([ \t]+((\r\n)[ \t]+)?|((\r\n)[ \t]+)+)|\\<br />
([\u0021-\u007e]|[ \t]|[\r\n\0]|[\u0001-\u0008\u000b\u000c\u<br />
000e-\u001f\u007f]))*(?(paren)(?!)))\))|([ \t]+((\r\n)[ \t]+<br />
)?|((\r\n)[ \t]+)+))*?\[(([ \t]+((\r\n)[ \t]+)?|((\r\n)[ \t]<br />
+)+)?([!-Z^-~]|[\u0001-\u0008\u000b\u000c\u000e-\u001f\u007f<br />
]))*([ \t]+((\r\n)[ \t]+)?|((\r\n)[ \t]+)+)?\]((\((((?'paren<br />
'\()|(?'-paren'\))|([\u0021-\u0027\u002a-\u005b\u005d-\u007e<br />
]|[\u0001-\u0008\u000b\u000c\u000e-\u001f\u007f])|([ \t]+((\<br />
r\n)[ \t]+)?|((\r\n)[ \t]+)+)|\\([\u0021-\u007e]|[ \t]|[\r\n<br />
\0]|[\u0001-\u0008\u000b\u000c\u000e-\u001f\u007f]))*(?(pare<br />
n)(?!)))\))|([ \t]+((\r\n)[ \t]+)?|((\r\n)[ \t]+)+))*?))\z</code></p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/validating-email-addresses-with-dot-net-regex/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>So, it turns out that .NET&#8217;s Regex are more powerful t̖̱̍ͭ͊h̟ͨͨa̞̖̙̔̇n͇̝͚̤̒́ͨ̐ ̯͖̏̌̔Ị̟̮̱̥̇̐̎͂ͬ͗̒ ̪̹̱͙̘ͦ̉ͪͪͣ̉͊o͕̥̝͇͙ͪ͊ͤ̑̂̽́r͔̭̪̮̟͗̍ͨ͗͛ͣḭ̝̜͈ͫ́g̥̹̥̜̦̓̇̓i̪͕̭̞͛ͯ̓͛̔̾ͫn̘̗a̰̜ͨͪ͊l̩͑̐̐́ͥ̚l̜ͨ͋̈ẙͦ́ ̟̬̬̫͙̤ͭ̚t̳͎̱̗̲́h͔͙̰̬̊̈́͊̾o͉ͫ̌̄u͉̲̥g̏ͥ̑̅̽̇h̻͇̥̰̯ͥͯṱ̯̏̄̒͒ͫ̃.͖̟͍̘̼̼̍̐̀͊̓́&#8230;</title>
		<link>http://porg.es/blog/so-it-turns-out-that-dot-nets-regex-are-more-powerful-than-i-originally-thought</link>
		<comments>http://porg.es/blog/so-it-turns-out-that-dot-nets-regex-are-more-powerful-than-i-originally-thought#comments</comments>
		<pubDate>Mon, 09 May 2011 14:09:19 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[horrid]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[silly]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=575</guid>
		<description><![CDATA[Today, thanks to user Lucero on StackOverflow, I learned about .NET&#8217;s &#8220;Balancing Groups&#8221; Regex feature. Basically, any time you use a named capturing group, it actually pushes the capture onto a named stack. You can then pop this stack by using the same capturing group prefixed with a hyphen, like (?). Of course, anyone who [...]]]></description>
			<content:encoded><![CDATA[<p>Today, thanks to user <a href="http://stackoverflow.com/users/88558/lucero">Lucero</a> on StackOverflow, I learned about .NET&#8217;s &#8220;Balancing Groups&#8221; Regex feature.</p>
<p>Basically, any time you use a named capturing group, it actually pushes the capture onto a named stack. You can then pop this stack by using the same capturing group prefixed with a hyphen, like <code>(?<-stackToPop>)</code>.</p>
<hr/>
<p>Of course, anyone who finds themselves in this situation is going to ask: <em>can it match XML?</em></p>
<p><span id="more-575"></span></p>
<p>It&#8217;s possible that I am missing something completely (it is rather late at night), but &#8230; <em>very nearly</em>. I haven&#8217;t quite figured out a nested section in the local DTD subset, but no one uses that feature anyway. (Can you spot it?)</p>
<p>Aside from that, most of the well-formedness criteria are handled (the obvious one being element nesting). Things that require non-local information such as entities aren&#8217;t handled. I think it is possible to handle duplicate attribute names in this form as well (via a lookahead for duplicate names).</p>
<p>Here is the code, and a test file which shows some stuff that is caught by this. Breaking any of the elements should make it fail:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var surrogate <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;([\ud800-\udbff][\udc00-\udfff])&quot;</span><span style="color: #008000;">;</span><span style="color: #008080; font-style: italic;">// .NET can't handle \U10000-\u10FFFF</span>
var c <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;([\u0009\u000a\u000d\u0020-\ud7ff\ue000-\ufffd]|&quot;</span><span style="color: #008000;">+</span>surrogate <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)&quot;</span><span style="color: #008000;">;</span> 
var s <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;([\u0020\u0009\u000d\u000a]+)&quot;</span><span style="color: #008000;">;</span>
var nameStartChar <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|&quot;</span> <span style="color: #008000;">+</span> surrogate <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)&quot;</span><span style="color: #008000;">;</span>
var nameChar <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(&quot;</span> <span style="color: #008000;">+</span> nameStartChar <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;|[-.0-9\u00B7\u0300-\u036F\u203F-\u2040])&quot;</span><span style="color: #008000;">;</span>
var name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'name'&quot;</span> <span style="color: #008000;">+</span> nameStartChar <span style="color: #008000;">+</span> nameChar <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;*)&quot;</span><span style="color: #008000;">;</span>
var names <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'names'&quot;</span> <span style="color: #008000;">+</span> name <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;(\u0020&quot;</span> <span style="color: #008000;">+</span> name <span style="color: #008000;">+</span><span style="color: #666666;">&quot;)*)&quot;</span><span style="color: #008000;">;</span>
var nmtoken <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'nmtoken'&quot;</span> <span style="color: #008000;">+</span> nameChar <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;+)&quot;</span><span style="color: #008000;">;</span>
var nmtokens <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'nmtokens'&quot;</span> <span style="color: #008000;">+</span> nmtoken <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;(\u0020&quot;</span> <span style="color: #008000;">+</span> nmtoken <span style="color: #008000;">+</span><span style="color: #666666;">&quot;)*)&quot;</span><span style="color: #008000;">;</span>
var pereference <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;%&quot;</span> <span style="color: #008000;">+</span> name <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;;&quot;</span><span style="color: #008000;">;</span>
var entityReference<span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'entityRef'&amp;&quot;</span> <span style="color: #008000;">+</span> name <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;;)&quot;</span><span style="color: #008000;">;</span>
var charref <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;&amp;\#([0-9]+|x[0-9a-fA-F]+);&quot;</span><span style="color: #008000;">;</span>
var reference <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'reference'&quot;</span><span style="color: #008000;">+</span> entityReference <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;|&quot;</span> <span style="color: #008000;">+</span> charref <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)&quot;</span><span style="color: #008000;">;</span>
var entityValue <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'entityValue'<span style="color: #008080; font-weight: bold;">\&quot;</span>([^%&amp;<span style="color: #008080; font-weight: bold;">\&quot;</span>]|&quot;</span> <span style="color: #008000;">+</span> pereference <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;|&quot;</span> <span style="color: #008000;">+</span> reference <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)*<span style="color: #008080; font-weight: bold;">\&quot;</span>|'([^%&amp;']|&quot;</span> <span style="color: #008000;">+</span> pereference <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;|&quot;</span> <span style="color: #008000;">+</span> reference <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)*')&quot;</span><span style="color: #008000;">;</span>
var eq <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'eq'&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;?=&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;?)&quot;</span><span style="color: #008000;">;</span>
var versionNum  <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;1\.[0-9]+&quot;</span><span style="color: #008000;">;</span>
var comment <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'comment'&lt;!--((?!--)&quot;</span> <span style="color: #008000;">+</span> c <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)*--&gt;)&quot;</span><span style="color: #008000;">;</span>
var PITarget <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'pitarget'(?![xX][mM][lL])&quot;</span><span style="color: #008000;">+</span>name<span style="color: #008000;">+</span><span style="color: #666666;">&quot;)&quot;</span><span style="color: #008000;">;</span>
var PI<span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;(?'PI'&lt;\?&quot;</span> <span style="color: #008000;">+</span> PITarget <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;(&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;((?!\?&gt;)&quot;</span> <span style="color: #008000;">+</span> c <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;)*)?\?&gt;)&quot;</span><span style="color: #008000;">;</span>
var misc <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'misc'&quot;</span> <span style="color: #008000;">+</span> comment <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;|&quot;</span> <span style="color: #008000;">+</span> PI <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;|&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)&quot;</span><span style="color: #008000;">;</span>
var versionInfo <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'versionInfo'&quot;</span><span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;version&quot;</span> <span style="color: #008000;">+</span> eq <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;('&quot;</span> <span style="color: #008000;">+</span> versionNum <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;'|<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #008000;">+</span> versionNum <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>))&quot;</span><span style="color: #008000;">;</span>
var encName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'encName'[A-Za-z][A-Za-z0-9._-]*)&quot;</span><span style="color: #008000;">;</span>
var encodingDecl <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'encodingDecl'&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;encoding&quot;</span> <span style="color: #008000;">+</span> eq <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;(<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #008000;">+</span> encName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>|'&quot;</span><span style="color: #008000;">+</span> encName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;'))&quot;</span><span style="color: #008000;">;</span>
var sddecl <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'sddecl'&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;standalone&quot;</span> <span style="color: #008000;">+</span> eq <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;(<span style="color: #008080; font-weight: bold;">\&quot;</span>(yes|no)<span style="color: #008080; font-weight: bold;">\&quot;</span>|'(yes|no)'))&quot;</span><span style="color: #008000;">;</span>
var xmlDecl <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;(?'xmlDecl'&lt;\?xml&quot;</span> <span style="color: #008000;">+</span> versionInfo <span style="color: #008000;">+</span> encodingDecl <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;?&quot;</span> <span style="color: #008000;">+</span> sddecl <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;?&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;?\?&gt;)&quot;</span><span style="color: #008000;">;</span> 
var mixed <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;(?'mixed'\(&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;?\#PCDATA&quot;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;(&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;?\|&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;?&quot;</span> <span style="color: #008000;">+</span> name <span style="color: #008000;">+</span><span style="color: #666666;">&quot;)*&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;?&quot;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;\)\*|\(&quot;</span> <span style="color: #008000;">+</span>s <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;?\#PCDATA&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;?\))&quot;</span><span style="color: #008000;">;</span>
var children <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;(?'children'unsureifpossible)&quot;</span><span style="color: #008000;">;</span>
var contentSpec <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'contentspec'EMPTY|ANY|&quot;</span><span style="color: #008000;">+</span>mixed<span style="color: #008000;">+</span><span style="color: #666666;">&quot;|&quot;</span><span style="color: #008000;">+</span>children<span style="color: #008000;">+</span><span style="color: #666666;">&quot;)&quot;</span><span style="color: #008000;">;</span>
var elementDecl <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'elementdecl'&lt;!ELEMENT&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> name <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> contentSpec <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;?&gt;)&quot;</span><span style="color: #008000;">;</span>
var stringType <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;CDATA&quot;</span><span style="color: #008000;">;</span>
var tokenizedType <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(ID(REF(S)?)?|ENTIT(Y|IES)|NMTOKENS?)&quot;</span><span style="color: #008000;">;</span>
var notationType <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'notation'NOTATION&quot;</span> <span style="color: #008000;">+</span>s <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;\(&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;?&quot;</span> <span style="color: #008000;">+</span> name <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;(&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;?\|&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;?&quot;</span> <span style="color: #008000;">+</span> name <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)*&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;?\))&quot;</span><span style="color: #008000;">;</span>
var enumeration <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;(?'enumeration'\(&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;?&quot;</span> <span style="color: #008000;">+</span> nmtoken <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;(&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;?\|&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;?&quot;</span> <span style="color: #008000;">+</span> nmtoken <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)*&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;?\))&quot;</span><span style="color: #008000;">;</span>
var enumeratedType <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'enumType'&quot;</span> <span style="color: #008000;">+</span> notationType <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;|&quot;</span> <span style="color: #008000;">+</span> enumeration <span style="color: #008000;">+</span><span style="color: #666666;">&quot;)&quot;</span><span style="color: #008000;">;</span>
var attType <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'attType'&quot;</span> <span style="color: #008000;">+</span> stringType <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;|&quot;</span> <span style="color: #008000;">+</span> tokenizedType <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;|&quot;</span> <span style="color: #008000;">+</span> enumeratedType <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)&quot;</span><span style="color: #008000;">;</span>
var attValue <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'attValue'<span style="color: #008080; font-weight: bold;">\&quot;</span>([^&lt;&amp;<span style="color: #008080; font-weight: bold;">\&quot;</span>]|&quot;</span> <span style="color: #008000;">+</span>reference<span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)*<span style="color: #008080; font-weight: bold;">\&quot;</span>|'([^&lt;&amp;']|&quot;</span> <span style="color: #008000;">+</span> reference <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)*')&quot;</span><span style="color: #008000;">;</span>
var defaultDecl <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;(?'defaultDecl'\#REQUIRED|\#IMPLIED|(\#FIXED&quot;</span><span style="color: #008000;">+</span>s<span style="color: #008000;">+</span><span style="color: #666666;">&quot;)?&quot;</span> <span style="color: #008000;">+</span> attValue <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)&quot;</span><span style="color: #008000;">;</span>
var attDef <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'attDef'&quot;</span><span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> name <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> attType <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> defaultDecl <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)&quot;</span><span style="color: #008000;">;</span>
var attListDecl <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'attlist'&lt;!ATTLIST&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> name <span style="color: #008000;">+</span> attDef <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;*&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;?&gt;)&quot;</span><span style="color: #008000;">;</span>
var systemLiteral <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'systemLiteral'<span style="color: #008080; font-weight: bold;">\&quot;</span>[^<span style="color: #008080; font-weight: bold;">\&quot;</span>]*<span style="color: #008080; font-weight: bold;">\&quot;</span>|'[^']*')&quot;</span><span style="color: #008000;">;</span>
var pubIdChar <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;[a-zA-Z0-9'()+,./:=?;!*#@$_%\u0020\u000d\u000a-]&quot;</span><span style="color: #008000;">;</span>
var pubidLiteral <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'pubIdLiteral'<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #008000;">+</span> pubIdChar <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;*<span style="color: #008080; font-weight: bold;">\&quot;</span>|'((?!')&quot;</span> <span style="color: #008000;">+</span> pubIdChar <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)*')&quot;</span><span style="color: #008000;">;</span>
var externalID <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'externalID'SYSTEM&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> systemLiteral <span style="color: #008000;">+</span><span style="color: #666666;">&quot;|PUBLIC&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> pubidLiteral <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> systemLiteral<span style="color: #008000;">+</span><span style="color: #666666;">&quot;)&quot;</span><span style="color: #008000;">;</span>
var nDataDecl <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'ndatadecl'&quot;</span><span style="color: #008000;">+</span>s <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;NDATA&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> name <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)&quot;</span><span style="color: #008000;">;</span>
var entityDef  <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'entityDef'&quot;</span> <span style="color: #008000;">+</span> entityValue <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;|(&quot;</span> <span style="color: #008000;">+</span>externalID <span style="color: #008000;">+</span> nDataDecl <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;?))&quot;</span><span style="color: #008000;">;</span>
var peDef <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'pedef'&quot;</span> <span style="color: #008000;">+</span> entityValue <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;|&quot;</span>  <span style="color: #008000;">+</span> externalID <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)&quot;</span><span style="color: #008000;">;</span>
var GEDecl <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'gedecl'&lt;!ENTITY&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> name <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> entityDef <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;?&gt;)&quot;</span><span style="color: #008000;">;</span>
var PEDecl <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'gedecl'&lt;!ENTITY&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;%&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> name <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> peDef <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;?&gt;)&quot;</span><span style="color: #008000;">;</span>
var entityDecl <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'entityDecl'&quot;</span><span style="color: #008000;">+</span> GEDecl <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;|&quot;</span> <span style="color: #008000;">+</span> PEDecl <span style="color: #008000;">+</span><span style="color: #666666;">&quot;)&quot;</span><span style="color: #008000;">;</span>
var publicID <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'publicID'PUBLIC&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> pubidLiteral <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)&quot;</span><span style="color: #008000;">;</span>
var notationDecl <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'notationDecl'&lt;!NOTATION&quot;</span> <span style="color: #008000;">+</span>  s <span style="color: #008000;">+</span> name <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;(&quot;</span> <span style="color: #008000;">+</span> externalID <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;|&quot;</span> <span style="color: #008000;">+</span> publicID <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;?&gt;)&quot;</span><span style="color: #008000;">;</span>
var markupDecl <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'markupdecl'&quot;</span> <span style="color: #008000;">+</span> elementDecl <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;|&quot;</span> <span style="color: #008000;">+</span> attListDecl <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;|&quot;</span> <span style="color: #008000;">+</span> entityDecl <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;|&quot;</span> <span style="color: #008000;">+</span> notationDecl <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;|&quot;</span> <span style="color: #008000;">+</span> PI <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;|&quot;</span> <span style="color: #008000;">+</span> comment <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)&quot;</span><span style="color: #008000;">;</span>
var DeclSep <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'declSep'&quot;</span> <span style="color: #008000;">+</span> pereference <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;|&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)&quot;</span><span style="color: #008000;">;</span>
var intSubSet <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;(?'intSubSet'(&quot;</span> <span style="color: #008000;">+</span> markupDecl <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;|&quot;</span> <span style="color: #008000;">+</span> DeclSep <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)*)&quot;</span><span style="color: #008000;">;</span> 
var docTypeDecl <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'doctypedecl'&lt;!DOCTYPE&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> name <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;(&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> externalID<span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)?&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;?(\[&quot;</span> <span style="color: #008000;">+</span> intSubSet <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;\]&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;?)?&gt;)&quot;</span><span style="color: #008000;">;</span> 
var prolog <span style="color: #008000;">=</span> xmlDecl <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;?&quot;</span> <span style="color: #008000;">+</span> misc <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;*(&quot;</span> <span style="color: #008000;">+</span> docTypeDecl <span style="color: #008000;">+</span> misc <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;*)?&quot;</span><span style="color: #008000;">;</span> 
var attribute <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(?'attribute'&quot;</span> <span style="color: #008000;">+</span>name <span style="color: #008000;">+</span> eq <span style="color: #008000;">+</span> attValue <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)&quot;</span><span style="color: #008000;">;</span>
var CDSect <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;(?'CDSect'&lt;!\[CDATA\[((?!\]\]&gt;)&quot;</span><span style="color: #008000;">+</span>c<span style="color: #008000;">+</span><span style="color: #666666;">@&quot;)*\]\]&gt;)&quot;</span><span style="color: #008000;">;</span>
var charData <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;(((?!\]\]&gt;)[^&lt;&amp;])*)&quot;</span><span style="color: #008000;">;</span>
var content <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;(?&gt;&quot;</span> <span style="color: #008000;">+</span> <span style="color: #008080; font-style: italic;">// minor optimization... don't backtrack over this (makes failing faster)</span>
		<span style="color: #666666;">@&quot;&lt;(?'openclose'&quot;</span> <span style="color: #008000;">+</span> name <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;)(&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> attribute <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)*&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;?/&gt;|&quot;</span><span style="color: #008000;">+</span>
		<span style="color: #666666;">@&quot;&lt;(?'open'&quot;</span><span style="color: #008000;">+</span> name <span style="color: #008000;">+</span><span style="color: #666666;">@&quot;)(&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> attribute <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)*&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;?&gt;|&quot;</span><span style="color: #008000;">+</span>
		<span style="color: #666666;">@&quot;&lt;/(?=\k'open'&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;?&gt;)(?'close-open'&quot;</span> <span style="color: #008000;">+</span> name <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span><span style="color: #666666;">@&quot;?&gt;|&quot;</span>
		<span style="color: #008000;">+</span>reference<span style="color: #008000;">+</span><span style="color: #666666;">@&quot;|&quot;</span>
		<span style="color: #008000;">+</span>PI<span style="color: #008000;">+</span><span style="color: #666666;">@&quot;|&quot;</span>
		<span style="color: #008000;">+</span>comment<span style="color: #008000;">+</span><span style="color: #666666;">@&quot;|&quot;</span>
		<span style="color: #008000;">+</span>CDSect<span style="color: #008000;">+</span><span style="color: #666666;">@&quot;|&quot;</span>
		<span style="color: #008000;">+</span>charData<span style="color: #008000;">+</span><span style="color: #666666;">@&quot;)*&quot;</span> <span style="color: #008000;">+</span> 
	<span style="color: #666666;">&quot;(?(open)(?!))&quot;</span><span style="color: #008000;">;</span>
var rootElement <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;(?'root'(&lt;(?'rootName'&quot;</span> <span style="color: #008000;">+</span> name <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)(&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> attribute <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)*&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;?&gt;&quot;</span> <span style="color: #008000;">+</span> content <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;&lt;/\k'rootName'&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;?&gt;)|(&lt;(?'rootName'&quot;</span> <span style="color: #008000;">+</span> name <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)(&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> attribute <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)*&quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;?/&gt;))&quot;</span><span style="color: #008000;">;</span>
&nbsp;
var document <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;^&quot;</span> <span style="color: #008000;">+</span> prolog <span style="color: #008000;">+</span> rootElement <span style="color: #008000;">+</span> misc <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;*&quot;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;$&quot;</span><span style="color: #008000;">;</span>
&nbsp;
var testDoc <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;&lt;?xml version='1.0' encoding=&quot;</span><span style="color: #666666;">&quot;utf-8&quot;</span><span style="color: #666666;">&quot;?&gt;&lt;!DOCTYPE nothtml []&gt;&lt;items&gt;
	&lt;item available=&quot;</span><span style="color: #666666;">&quot;yes&quot;</span><span style="color: #666666;">&quot; &gt;
		&lt;name&gt; laptop  &lt;/name&gt;
		&lt;![CDATA[something14!$]] 1412]]&gt;
		&lt;&quot;</span><span style="color: #008000;">+</span><span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\U</span>00010000&quot;</span><span style="color: #008000;">+</span><span style="color: #666666;">@&quot;quantity&gt;  2 &amp;amp; &amp;#121; &amp;#x234f; &lt;/&quot;</span><span style="color: #008000;">+</span><span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\U</span>00010000&quot;</span><span style="color: #008000;">+</span><span style="color: #666666;">@&quot;quantity&gt;
	&lt;/item&gt;&lt;?notxml?&gt;&quot;</span> <span style="color: #008080; font-style: italic;">/* or &lt;?xml?&gt; here */</span> <span style="color: #008000;">+</span><span style="color: #666666;">@&quot;
	&lt;item available=&quot;</span><span style="color: #666666;">&quot;yes&quot;</span><span style="color: #666666;">&quot; x='' y=&quot;</span><span style="color: #666666;">&quot;&amp;amp;&quot;</span><span style="color: #666666;">&quot;&gt;
		&lt;name&gt; mouse &lt;/name &gt;
		&lt;quantity&gt; 1 &quot;</span> <span style="color: #008000;">+</span> <span style="color: #008080; font-style: italic;">/* or ]]&gt; invalid here */</span>  <span style="color: #666666;">@&quot; &lt;/quantity&gt;
	&lt;/item&gt;
	&lt;item available=&quot;</span><span style="color: #666666;">&quot;no&quot;</span><span style="color: #666666;">&quot; &gt;
		&lt;!----&gt; &lt;!-- --&gt; &lt;!-- - --&gt;&quot;</span> <span style="color: #008000;">+</span> <span style="color: #008080; font-style: italic;">/* or &lt;!-- -- --&gt; here */</span> <span style="color: #666666;">@&quot;
		&lt;name&gt; keyboad &lt;/name&gt;
		&lt;quantity&gt; 0&lt;/quantity&gt;
	&lt;/item&gt;
&lt;/items&gt;&lt;!-- stuff can go here --&gt; &lt;!-- yup --&gt; &lt;?pi aasd as!@*&amp;$^!*@&amp;$!@ ?&gt;&quot;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//Console.WriteLine(document);</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>Regex<span style="color: #008000;">.</span><span style="color: #0000FF;">Match</span><span style="color: #008000;">&#40;</span>testDoc, document, RegexOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">IgnorePatternWhitespace</span><span style="color: #008000;">|</span>RegexOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">Singleline</span><span style="color: #008000;">|</span>RegexOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">ExplicitCapture</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<hr/>
<p>And here&#8217;s the regex (with apologies to <a href="http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html">Mail::RFC822::Address</a>):</p>
<p><code style="white-space:nowrap">^(?'xmlDecl'&lt;\?xml(?'versionInfo'([\u0020\u0009\u000d\u000a]+)version(?'eq'([\u0<br />
020\u0009\u000d\u000a]+)?=([\u0020\u0009\u000d\u000a]+)?)('1\.[0-9]+'|"1\.[0-9]+<br />
"))(?'encodingDecl'([\u0020\u0009\u000d\u000a]+)encoding(?'eq'([\u0020\u0009\u00<br />
0d\u000a]+)?=([\u0020\u0009\u000d\u000a]+)?)("(?'encName'[A-Za-z][A-Za-z0-9._-]*<br />
)"|'(?'encName'[A-Za-z][A-Za-z0-9._-]*)'))?(?'sddecl'([\u0020\u0009\u000d\u000a]<br />
+)standalone(?'eq'([\u0020\u0009\u000d\u000a]+)?=([\u0020\u0009\u000d\u000a]+)?)<br />
("(yes|no)"|'(yes|no)'))?([\u0020\u0009\u000d\u000a]+)?\?>)?(?'misc'(?'comment'&lt;<br />
!--((?!--)([\u0009\u000a\u000d\u0020-\ud7ff\ue000-\ufffd]|([\ud800-\udbff][\udc0<br />
0-\udfff])))*-->)|(?'PI'&lt;\?(?'pitarget'(?![xX][mM][lL])(?'name'([:A-Z_a-z\u00C0-<br />
\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u<br />
218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc0<br />
0-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F<br />
-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\<br />
uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u203F-\u2040]<br />
)*))(([\u0020\u0009\u000d\u000a]+)((?!\?>)([\u0009\u000a\u000d\u0020-\ud7ff\ue00<br />
0-\ufffd]|([\ud800-\udbff][\udc00-\udfff])))*)?\?>)|([\u0020\u0009\u000d\u000a]+<br />
))*((?'doctypedecl'&lt;!DOCTYPE([\u0020\u0009\u000d\u000a]+)(?'name'([:A-Z_a-z\u00C<br />
0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-<br />
\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\ud<br />
c00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u03<br />
7F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0<br />
-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u203F-\u204<br />
0])*)(([\u0020\u0009\u000d\u000a]+)(?'externalID'SYSTEM([\u0020\u0009\u000d\u000<br />
a]+)(?'systemLiteral'"[^"]*"|'[^']*')|PUBLIC([\u0020\u0009\u000d\u000a]+)(?'pubI<br />
dLiteral'"[a-zA-Z0-9'()+,./:=?;!*#@$_%\u0020\u000d\u000a-]*"|'((?!')[a-zA-Z0-9'(<br />
)+,./:=?;!*#@$_%\u0020\u000d\u000a-])*')([\u0020\u0009\u000d\u000a]+)(?'systemLi<br />
teral'"[^"]*"|'[^']*')))?([\u0020\u0009\u000d\u000a]+)?(\[(?'intSubSet'((?'marku<br />
pdecl'(?'elementdecl'&lt;!ELEMENT([\u0020\u0009\u000d\u000a]+)(?'name'([:A-Z_a-z\u0<br />
0C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u207<br />
0-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\<br />
udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u<br />
037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFD<br />
F0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u203F-\u2<br />
040])*)([\u0020\u0009\u000d\u000a]+)(?'contentspec'EMPTY|ANY|(?'mixed'\(([\u0020<br />
\u0009\u000d\u000a]+)?\#PCDATA(([\u0020\u0009\u000d\u000a]+)?\|([\u0020\u0009\u0<br />
00d\u000a]+)?(?'name'([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u0<br />
37D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDC<br />
F\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-<br />
\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u<br />
2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[<br />
-.0-9\u00B7\u0300-\u036F\u203F-\u2040])*))*([\u0020\u0009\u000d\u000a]+)?\)\*|\(<br />
([\u0020\u0009\u000d\u000a]+)?\#PCDATA([\u0020\u0009\u000d\u000a]+)?\))|(?'child<br />
ren'unsureifpossible))([\u0020\u0009\u000d\u000a]+)?>)|(?'attlist'&lt;!ATTLIST([\u0<br />
020\u0009\u000d\u000a]+)(?'name'([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02F<br />
F\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\<br />
uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u<br />
00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u21<br />
8F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-<br />
\udfff]))|[-.0-9\u00B7\u0300-\u036F\u203F-\u2040])*)(?'attDef'([\u0020\u0009\u00<br />
0d\u000a]+)(?'name'([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037<br />
D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\<br />
uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u<br />
00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2F<br />
EF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.<br />
0-9\u00B7\u0300-\u036F\u203F-\u2040])*)([\u0020\u0009\u000d\u000a]+)(?'attType'C<br />
DATA|(ID(REF(S)?)?|ENTIT(Y|IES)|NMTOKENS?)|(?'enumType'(?'notation'NOTATION([\u0<br />
020\u0009\u000d\u000a]+)\(([\u0020\u0009\u000d\u000a]+)?(?'name'([:A-Z_a-z\u00C0<br />
-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\<br />
u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc<br />
00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037<br />
F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-<br />
\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u203F-\u2040<br />
])*)(([\u0020\u0009\u000d\u000a]+)?\|([\u0020\u0009\u000d\u000a]+)?(?'name'([:A-<br />
Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u2<br />
00D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\<br />
udbff][\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-<br />
\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\u<br />
FDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u<br />
203F-\u2040])*))*([\u0020\u0009\u000d\u000a]+)?\))|(?'enumeration'\(([\u0020\u00<br />
09\u000d\u000a]+)?(?'nmtoken'(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\<br />
u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF<br />
900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u<br />
036F\u203F-\u2040])+)(([\u0020\u0009\u000d\u000a]+)?\|([\u0020\u0009\u000d\u000a<br />
]+)?(?'nmtoken'(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u<br />
037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFD<br />
F0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u203F-\u2<br />
040])+))*([\u0020\u0009\u000d\u000a]+)?\))))([\u0020\u0009\u000d\u000a]+)(?'defa<br />
ultDecl'\#REQUIRED|\#IMPLIED|(\#FIXED([\u0020\u0009\u000d\u000a]+))?(?'attValue'<br />
"([^&lt;&amp;"]|(?'reference'(?'entityRef'&amp;(?'name'([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6<br />
\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u<br />
3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))(([:A-Z_<br />
a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200<br />
D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\ud<br />
bff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u203F-\u2040])*);())|&amp;\#([0-9]+|<br />
x[0-9a-fA-F]+);()))*"|'([^&lt;&amp;']|(?'reference'(?'entityRef'&amp;(?'name'([:A-Z_a-z\u00<br />
C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070<br />
-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\u<br />
dc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u0<br />
37F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF<br />
0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u203F-\u20<br />
40])*);())|&amp;\#([0-9]+|x[0-9a-fA-F]+);()))*')))*([\u0020\u0009\u000d\u000a]+)?>)|<br />
(?'entityDecl'(?'gedecl'&lt;!ENTITY([\u0020\u0009\u000d\u000a]+)(?'name'([:A-Z_a-z\<br />
u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2<br />
070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff]<br />
[\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D<br />
\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\u<br />
FDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u203F-\<br />
u2040])*)([\u0020\u0009\u000d\u000a]+)(?'entityDef'(?'entityValue'"([^%&amp;"]|%(?'n<br />
ame'([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\<br />
u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|(<br />
[\ud800-\udbff][\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02<br />
FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF<br />
\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300<br />
-\u036F\u203F-\u2040])*);|(?'reference'(?'entityRef'&amp;(?'name'([:A-Z_a-z\u00C0-\u<br />
00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u21<br />
8F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-<br />
\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\<br />
u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uF<br />
FFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u203F-\u2040])*<br />
);())|&amp;\#([0-9]+|x[0-9a-fA-F]+);()))*"|'([^%&amp;']|%(?'name'([:A-Z_a-z\u00C0-\u00D6<br />
\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u<br />
2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udf<br />
ff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FF<br />
F\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]<br />
|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u203F-\u2040])*);|(<br />
?'reference'(?'entityRef'&amp;(?'name'([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u0<br />
2FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7F<br />
F\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))(([:A-Z_a-z\u00C0-<br />
\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u<br />
218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc0<br />
0-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u203F-\u2040])*);())|&amp;\#([0-9]+|x[0-9a-fA-<br />
F]+);()))*')|((?'externalID'SYSTEM([\u0020\u0009\u000d\u000a]+)(?'systemLiteral'<br />
"[^"]*"|'[^']*')|PUBLIC([\u0020\u0009\u000d\u000a]+)(?'pubIdLiteral'"[a-zA-Z0-9'<br />
()+,./:=?;!*#@$_%\u0020\u000d\u000a-]*"|'((?!')[a-zA-Z0-9'()+,./:=?;!*#@$_%\u002<br />
0\u000d\u000a-])*')([\u0020\u0009\u000d\u000a]+)(?'systemLiteral'"[^"]*"|'[^']*'<br />
))(?'ndatadecl'([\u0020\u0009\u000d\u000a]+)NDATA([\u0020\u0009\u000d\u000a]+)(?<br />
'name'([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FF<br />
F\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]<br />
|([\ud800-\udbff][\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u<br />
02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7<br />
FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u03<br />
00-\u036F\u203F-\u2040])*))?))([\u0020\u0009\u000d\u000a]+)?>)|(?'gedecl'&lt;!ENTIT<br />
Y([\u0020\u0009\u000d\u000a]+)%([\u0020\u0009\u000d\u000a]+)(?'name'([:A-Z_a-z\u<br />
00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u20<br />
70-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][<br />
\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\<br />
u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uF<br />
DF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u203F-\u<br />
2040])*)([\u0020\u0009\u000d\u000a]+)(?'pedef'(?'entityValue'"([^%&amp;"]|%(?'name'(<br />
[:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C<br />
-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud8<br />
00-\udbff][\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0<br />
370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF90<br />
0-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u03<br />
6F\u203F-\u2040])*);|(?'reference'(?'entityRef'&amp;(?'name'([:A-Z_a-z\u00C0-\u00D6\<br />
u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2<br />
C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udff<br />
f]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF<br />
\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|<br />
([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u203F-\u2040])*);())<br />
|&amp;\#([0-9]+|x[0-9a-fA-F]+);()))*"|'([^%&amp;']|%(?'name'([:A-Z_a-z\u00C0-\u00D6\u00D<br />
8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-<br />
\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))<br />
(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u20<br />
0C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\u<br />
d800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u203F-\u2040])*);|(?'ref<br />
erence'(?'entityRef'&amp;(?'name'([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u<br />
0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF9<br />
00-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D<br />
6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\<br />
u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\ud<br />
fff]))|[-.0-9\u00B7\u0300-\u036F\u203F-\u2040])*);())|&amp;\#([0-9]+|x[0-9a-fA-F]+);<br />
()))*')|(?'externalID'SYSTEM([\u0020\u0009\u000d\u000a]+)(?'systemLiteral'"[^"]*<br />
"|'[^']*')|PUBLIC([\u0020\u0009\u000d\u000a]+)(?'pubIdLiteral'"[a-zA-Z0-9'()+,./<br />
:=?;!*#@$_%\u0020\u000d\u000a-]*"|'((?!')[a-zA-Z0-9'()+,./:=?;!*#@$_%\u0020\u000<br />
d\u000a-])*')([\u0020\u0009\u000d\u000a]+)(?'systemLiteral'"[^"]*"|'[^']*')))([\<br />
u0020\u0009\u000d\u000a]+)?>))|(?'notationDecl'&lt;!NOTATION([\u0020\u0009\u000d\u0<br />
00a]+)(?'name'([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u03<br />
7F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0<br />
-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\<br />
u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3<br />
001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u<br />
00B7\u0300-\u036F\u203F-\u2040])*)([\u0020\u0009\u000d\u000a]+)((?'externalID'SY<br />
STEM([\u0020\u0009\u000d\u000a]+)(?'systemLiteral'"[^"]*"|'[^']*')|PUBLIC([\u002<br />
0\u0009\u000d\u000a]+)(?'pubIdLiteral'"[a-zA-Z0-9'()+,./:=?;!*#@$_%\u0020\u000d\<br />
u000a-]*"|'((?!')[a-zA-Z0-9'()+,./:=?;!*#@$_%\u0020\u000d\u000a-])*')([\u0020\u0<br />
009\u000d\u000a]+)(?'systemLiteral'"[^"]*"|'[^']*'))|(?'publicID'PUBLIC([\u0020\<br />
u0009\u000d\u000a]+)(?'pubIdLiteral'"[a-zA-Z0-9'()+,./:=?;!*#@$_%\u0020\u000d\u0<br />
00a-]*"|'((?!')[a-zA-Z0-9'()+,./:=?;!*#@$_%\u0020\u000d\u000a-])*')))([\u0020\u0<br />
009\u000d\u000a]+)?>)|(?'PI'&lt;\?(?'pitarget'(?![xX][mM][lL])(?'name'([:A-Z_a-z\u0<br />
0C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u207<br />
0-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\<br />
udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u<br />
037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFD<br />
F0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u203F-\u2<br />
040])*))(([\u0020\u0009\u000d\u000a]+)((?!\?>)([\u0009\u000a\u000d\u0020-\ud7ff\<br />
ue000-\ufffd]|([\ud800-\udbff][\udc00-\udfff])))*)?\?>)|(?'comment'&lt;!--((?!--)([<br />
\u0009\u000a\u000d\u0020-\ud7ff\ue000-\ufffd]|([\ud800-\udbff][\udc00-\udfff])))<br />
*-->))|(?'declSep'%(?'name'([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u03<br />
70-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900<br />
-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\<br />
u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2<br />
C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udff<br />
f]))|[-.0-9\u00B7\u0300-\u036F\u203F-\u2040])*);|([\u0020\u0009\u000d\u000a]+)))<br />
*)\]([\u0020\u0009\u000d\u000a]+)?)?>)(?'misc'(?'comment'&lt;!--((?!--)([\u0009\u00<br />
0a\u000d\u0020-\ud7ff\ue000-\ufffd]|([\ud800-\udbff][\udc00-\udfff])))*-->)|(?'P<br />
I'&lt;\?(?'pitarget'(?![xX][mM][lL])(?'name'([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u0<br />
0F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u300<br />
1-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))(([:A-Z_a-z<br />
\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u<br />
2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff<br />
][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u203F-\u2040])*))(([\u0020\u0009\u0<br />
00d\u000a]+)((?!\?>)([\u0009\u000a\u000d\u0020-\ud7ff\ue000-\ufffd]|([\ud800-\ud<br />
bff][\udc00-\udfff])))*)?\?>)|([\u0020\u0009\u000d\u000a]+))*)?(?'root'(&lt;(?'root<br />
Name'(?'name'([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037<br />
F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-<br />
\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u<br />
00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u30<br />
01-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u0<br />
0B7\u0300-\u036F\u203F-\u2040])*))(([\u0020\u0009\u000d\u000a]+)(?'attribute'(?'<br />
name'([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF<br />
\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|<br />
([\ud800-\udbff][\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u0<br />
2FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7F<br />
F\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u030<br />
0-\u036F\u203F-\u2040])*)(?'eq'([\u0020\u0009\u000d\u000a]+)?=([\u0020\u0009\u00<br />
0d\u000a]+)?)(?'attValue'"([^&lt;&amp;"]|(?'reference'(?'entityRef'&amp;(?'name'([:A-Z_a-z\<br />
u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2<br />
070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff]<br />
[\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D<br />
\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\u<br />
FDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u203F-\<br />
u2040])*);())|&amp;\#([0-9]+|x[0-9a-fA-F]+);()))*"|'([^&lt;&amp;']|(?'reference'(?'entityRe<br />
f'&amp;(?'name'([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-<br />
\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\u<br />
FFFD]|([\ud800-\udbff][\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00<br />
F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001<br />
-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B<br />
7\u0300-\u036F\u203F-\u2040])*);())|&amp;\#([0-9]+|x[0-9a-fA-F]+);()))*')))*([\u0020<br />
\u0009\u000d\u000a]+)?>(?>&lt;(?'openclose'(?'name'([:A-Z_a-z\u00C0-\u00D6\u00D8-\u<br />
00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2F<br />
EF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))(([:<br />
A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\<br />
u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800<br />
-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u203F-\u2040])*))(([\u0020\u<br />
0009\u000d\u000a]+)(?'attribute'(?'name'([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00<br />
F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001<br />
-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))(([:A-Z_a-z\<br />
u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2<br />
070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff]<br />
[\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u203F-\u2040])*)(?'eq'([\u0020\u0009<br />
\u000d\u000a]+)?=([\u0020\u0009\u000d\u000a]+)?)(?'attValue'"([^&lt;&amp;"]|(?'referenc<br />
e'(?'entityRef'&amp;(?'name'([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-<br />
\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\u<br />
FDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00<br />
D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00<br />
-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff])<br />
)|[-.0-9\u00B7\u0300-\u036F\u203F-\u2040])*);())|&amp;\#([0-9]+|x[0-9a-fA-F]+);()))*<br />
"|'([^&lt;&amp;']|(?'reference'(?'entityRef'&amp;(?'name'([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00<br />
F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF<br />
\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))(([:A-<br />
Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u2<br />
00D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\<br />
udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u203F-\u2040])*);())|&amp;\#([0-9]<br />
+|x[0-9a-fA-F]+);()))*')))*([\u0020\u0009\u000d\u000a]+)?/>|&lt;(?'open'(?'name'([:<br />
A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\<br />
u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800<br />
-\udbff][\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u037<br />
0-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-<br />
\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F<br />
\u203F-\u2040])*))(([\u0020\u0009\u000d\u000a]+)(?'attribute'(?'name'([:A-Z_a-z\<br />
u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2<br />
070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff]<br />
[\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D<br />
\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\u<br />
FDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u203F-\<br />
u2040])*)(?'eq'([\u0020\u0009\u000d\u000a]+)?=([\u0020\u0009\u000d\u000a]+)?)(?'<br />
attValue'"([^&lt;&amp;"]|(?'reference'(?'entityRef'&amp;(?'name'([:A-Z_a-z\u00C0-\u00D6\u00<br />
D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00<br />
-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff])<br />
)(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u2<br />
00C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\<br />
ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u203F-\u2040])*);())|&amp;\<br />
#([0-9]+|x[0-9a-fA-F]+);()))*"|'([^&lt;&amp;']|(?'reference'(?'entityRef'&amp;(?'name'([:A-<br />
Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u2<br />
00D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\<br />
udbff][\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-<br />
\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\u<br />
FDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u<br />
203F-\u2040])*);())|&amp;\#([0-9]+|x[0-9a-fA-F]+);()))*')))*([\u0020\u0009\u000d\u00<br />
0a]+)?>|&lt;/(?=\k'open'([\u0020\u0009\u000d\u000a]+)?>)(?'close-open'(?'name'([:A-<br />
Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u2<br />
00D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\<br />
udbff][\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-<br />
\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\u<br />
FDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u<br />
203F-\u2040])*))([\u0020\u0009\u000d\u000a]+)?>|(?'reference'(?'entityRef'&amp;(?'na<br />
me'([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u<br />
200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([<br />
\ud800-\udbff][\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02F<br />
F\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\<br />
uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-<br />
\u036F\u203F-\u2040])*);())|&amp;\#([0-9]+|x[0-9a-fA-F]+);())|(?'PI'&lt;\?(?'pitarget'(<br />
?![xX][mM][lL])(?'name'([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\<br />
u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uF<br />
DCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D<br />
8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-<br />
\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))<br />
|[-.0-9\u00B7\u0300-\u036F\u203F-\u2040])*))(([\u0020\u0009\u000d\u000a]+)((?!\?<br />
>)([\u0009\u000a\u000d\u0020-\ud7ff\ue000-\ufffd]|([\ud800-\udbff][\udc00-\udfff<br />
])))*)?\?>)|(?'comment'&lt;!--((?!--)([\u0009\u000a\u000d\u0020-\ud7ff\ue000-\ufffd<br />
]|([\ud800-\udbff][\udc00-\udfff])))*-->)|(?'CDSect'&lt;!\[CDATA\[((?!\]\]>)([\u000<br />
9\u000a\u000d\u0020-\ud7ff\ue000-\ufffd]|([\ud800-\udbff][\udc00-\udfff])))*\]\]<br />
>)|(((?!\]\]>)[^&lt;&amp;])*))*(?(open)(?!))&lt;/\k'rootName'([\u0020\u0009\u000d\u000a]+)<br />
?>)|(&lt;(?'rootName'(?'name'([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u037<br />
0-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-<br />
\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u<br />
00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C<br />
00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff<br />
]))|[-.0-9\u00B7\u0300-\u036F\u203F-\u2040])*))(([\u0020\u0009\u000d\u000a]+)(?'<br />
attribute'(?'name'([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D<br />
\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\u<br />
FDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u0<br />
0F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FE<br />
F\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0<br />
-9\u00B7\u0300-\u036F\u203F-\u2040])*)(?'eq'([\u0020\u0009\u000d\u000a]+)?=([\u0<br />
020\u0009\u000d\u000a]+)?)(?'attValue'"([^&lt;&amp;"]|(?'reference'(?'entityRef'&amp;(?'nam<br />
e'([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u2<br />
00C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\<br />
ud800-\udbff][\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF<br />
\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\u<br />
F900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\<br />
u036F\u203F-\u2040])*);())|&amp;\#([0-9]+|x[0-9a-fA-F]+);()))*"|'([^&lt;&amp;']|(?'referenc<br />
e'(?'entityRef'&amp;(?'name'([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-<br />
\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\u<br />
FDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))(([:A-Z_a-z\u00C0-\u00D6\u00<br />
D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00<br />
-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff])<br />
)|[-.0-9\u00B7\u0300-\u036F\u203F-\u2040])*);())|&amp;\#([0-9]+|x[0-9a-fA-F]+);()))*<br />
')))*([\u0020\u0009\u000d\u000a]+)?/>))(?'misc'(?'comment'&lt;!--((?!--)([\u0009\u0<br />
00a\u000d\u0020-\ud7ff\ue000-\ufffd]|([\ud800-\udbff][\udc00-\udfff])))*-->)|(?'<br />
PI'&lt;\?(?'pitarget'(?![xX][mM][lL])(?'name'([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u<br />
00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u30<br />
01-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbff][\udc00-\udfff]))(([:A-Z_a-<br />
z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\<br />
u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|([\ud800-\udbf<br />
f][\udc00-\udfff]))|[-.0-9\u00B7\u0300-\u036F\u203F-\u2040])*))(([\u0020\u0009\u<br />
000d\u000a]+)((?!\?>)([\u0009\u000a\u000d\u0020-\ud7ff\ue000-\ufffd]|([\ud800-\u<br />
dbff][\udc00-\udfff])))*)?\?>)|([\u0020\u0009\u000d\u000a]+))*$</code></p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/so-it-turns-out-that-dot-nets-regex-are-more-powerful-than-i-originally-thought/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Unicode as she is broke</title>
		<link>http://porg.es/blog/unicode-as-she-is-broke</link>
		<comments>http://porg.es/blog/unicode-as-she-is-broke#comments</comments>
		<pubDate>Mon, 14 Mar 2011 08:34:02 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[Broken]]></category>
		<category><![CDATA[criticism]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Unicode]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=418</guid>
		<description><![CDATA[Do you think your string-handling code is robust? Are there any problems with the following snippets? // Write the first character: Console.WriteLine&#40;s&#91;0&#93;&#41;; &#160; // Reverse the string: var backwards = s.ToArray&#40;&#41;; Array.Reverse&#40;backwards&#41;; Console.WriteLine&#40;new string&#40;backwards&#41;&#41;; &#160; // List the characters, separated by commas: Console.WriteLine&#40;string.Join&#40;&#34;, &#34;, s.ToArray&#40;&#41;&#41;&#41;; All of these are potential bugs. Just set the following [...]]]></description>
			<content:encoded><![CDATA[<p>Do you think your string-handling code is robust? Are there any problems with the following snippets?</p>
<p><span id="more-418"></span></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Write the first character:</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>s<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Reverse the string:</span>
var backwards <span style="color: #008000;">=</span> s<span style="color: #008000;">.</span><span style="color: #0000FF;">ToArray</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
Array<span style="color: #008000;">.</span><span style="color: #0000FF;">Reverse</span><span style="color: #008000;">&#40;</span>backwards<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#40;</span>backwards<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// List the characters, separated by commas:</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Join</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;, &quot;</span>, s<span style="color: #008000;">.</span><span style="color: #0000FF;">ToArray</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

 <a class="simple-footnote" title="Note that even if you have a UTF-16 implementation with a &#8220;non-broken&#8221; char, the reversal given here isn&#8217;t valid. For one thing, it will put combining characters in the wrong place. The reversal of a string is also potentially locale-dependent: one can argue that the reversal of &#8220;Dijkstra&#8221; (Dutch) should be &#8220;artsijkD&#8221;." id="return-note-418-1" href="#note-418-1"><sup>1</sup></a>
<p>All of these are potential bugs.</p>
<p>Just set the following and you&#8217;ll get some nice output:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">string</span> s <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;𠂔&quot;</span><span style="color: #008000;">;</span></pre></div></div>

<pre>�
��
�,�</pre>
<p>There are other problems as well:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">string</span> A <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;𝔄&quot;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// MATHEMATICAL FRAKTUR CAPITAL A, if you can't see it</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">char</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsUpper</span><span style="color: #008000;">&#40;</span>A<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// False</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">char</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsUpper</span><span style="color: #008000;">&#40;</span>A<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// False</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">char</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsUpper</span><span style="color: #008000;">&#40;</span>A,<span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// True - this is the correct way</span></pre></div></div>

<p>Why is this?</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">char</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetUnicodeCategory</span><span style="color: #008000;">&#40;</span>A<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// Surrogate</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">char</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetUnicodeCategory</span><span style="color: #008000;">&#40;</span>A<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// Surrogate</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">char</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetUnicodeCategory</span><span style="color: #008000;">&#40;</span>A,<span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// UppercaseLetter</span></pre></div></div>

<p>Ah.</p>
<h2>The UCS-2 Hangover</h2>
<p>These are all symptoms of &#8216;migrated&#8217; libraries, originally developed for UCS-2 (in the early days of Unicode), and now touted as UTF-16.</p>
<p>Whereas UCS-2 was a fixed-width, 16-bit format, UTF-16 is a variable-width format which uses pairs of &#8216;surrogate&#8217; 16-bit units to support characters that UCS-2 does not. These are the characters outside the Basic Multilingual Plane (BMP), sometimes termed the &#8216;astral characters&#8217;.</p>
<p>Libraries with <code>char</code> as a 16-bit quantity are living a lie. They live in a delusional world of fixed-width characters, where they let you believe you can index, substring, and play around at will, whereas:</p>
<ul>
<li>You cannot select arbitrary substrings &mdash; you might get half a character included.</li>
<li>You cannot index into the string arbitrarily &mdash; you might select half a character.</li>
<li>You cannot treat a string as a bunch of isolated characters &mdash; the surrogate pairs will get broken up.</li>
</ul>
<h2>So?</h2>
<p>The general response seems to be &#8220;it doesn&#8217;t matter, most of the stuff I deal with is in the BMP, so I don&#8217;t care&#8221;.</p>
<p>A simple example of the problem with this: if any of these &#8216;orphan surrogates&#8217; gets into an XML document, you have an application-stopping bug on your hands. (Just ask <a href="http://www.linqpad.net/">LINQPad</a>, which refuses to run any of the examples in the first block of code. Luckily it has good exception handling.) <a href="http://www.google.co.nz/search?q=&quot;The+surrogate+pair+is+invalid&quot;">A quick Google search</a> shows that many developers are already encountering this problem.</p>
<p>All of the latest CJK extensions (B, C, D) also live in the <a href="http://en.wikipedia.org/wiki/Supplementary_Ideographic_Plane#Supplementary_Ideographic_Plane">Supplementary Ideographic Plane</a> (SIP, the second non-BMP plane). I haven&#8217;t really been able to find out just how many of these characters are actually used in any real sense, but you&#8217;re going to encounter them eventually (and <a href="http://www.unicode.org/charts/PDF/U1F600.pdf">recent additions to Unicode</a> might start turning up).</p>
<h3>There&#8217;s a hole in your abstraction</h3>
<p>The problem with UCS-2&ndash;flavoured UTF-16 is that it hands all the work of handling surrogate characters correctly over to the developer.</p>
<p>Underlining the third character in a string sounds like a simple task. But, if someone slips in a non-BMP character, then&#8230;</p>
 <a class="simple-footnote" title="Again, this won&#8217;t work perfectly even with a &#8220;non-broken&#8221; char type. Combining characters need to be considered. This argues for a more sophisticated library in general, one that has an idea of a visible &#8216;stack&#8217; of codepoints (or some other basic glyph construct). A discussion for another time&#8230;" id="return-note-418-2" href="#note-418-2"><sup>2</sup></a>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var s <span style="color: #0600FF; font-weight: bold;">in</span> <span style="color: #008000;">new</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;0123456789&quot;</span>, <span style="color: #666666;">&quot;〇一二三四五六七八九&quot;</span>, <span style="color: #666666;">&quot;𝟘123456789&quot;</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>s<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>,<span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;&lt;span style='border:2px #FF4500 solid'&gt;&quot;</span> <span style="color: #008000;">+</span> s<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;&lt;/span&gt;&quot;</span> <span style="color: #008000;">+</span> s<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
01<span style='border:2px #FF4500 solid'>2</span>3456789<br />
〇一<span style='border:2px #FF4500 solid'>二</span>三四五六七八九<br />
𝟘<span style='border:2px #FF4500 solid'>1</span>23456789
</p>
<p>&#8230;and now the developer has to write their own linear-time indexing functions to actually find the character they need. This is not a good abstraction.</p>
<h2>From here</h2>
<p>Once you give up the idea that you can safely treat UTF-16 as a fixed-width format, it really doesn&#8217;t have anything going for it. (Perhaps this is why the idea is so firmly embedded &mdash; elements of <a href="http://en.wikipedia.org/wiki/Cognitive_dissonance">cognitive dissonance</a>.)</p>
<p>Here&#8217;s a quick (mainly biased) diagram:</p>
<p><a href="http://porg.es/blog/wp-content/uploads/2010/11/unicode.png"><img src="http://porg.es/blog/wp-content/uploads/2010/11/unicode.png" alt="" title="Comparison of UTFs" width="546" height="300" class="aligncenter size-full wp-image-460" /></a></p>
<div class="simple-footnotes"><p class="notes">Notes:</p><ol><li id="note-418-1">Note that even if you have a UTF-16 implementation with a &#8220;non-broken&#8221; <code>char</code>, the reversal given here isn&#8217;t valid. For one thing, it will put combining characters in the wrong place. The reversal of a string is also potentially locale-dependent: one can argue that the reversal of &#8220;Dijkstra&#8221; (Dutch) should be &#8220;artsijkD&#8221;. <a href="#return-note-418-1">&#8617;</a></li><li id="note-418-2">Again, this won&#8217;t work perfectly even with a &#8220;non-broken&#8221; <code>char</code> type. Combining characters need to be considered. This argues for a more sophisticated library in general, one that has an idea of a visible &#8216;stack&#8217; of codepoints (or some other basic glyph construct). A discussion for another time&#8230; <a href="#return-note-418-2">&#8617;</a></li></ol></div>]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/unicode-as-she-is-broke/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# type inference and extension methods: an abuse</title>
		<link>http://porg.es/blog/c-type-inference-and-extension-methods-an-abuse</link>
		<comments>http://porg.es/blog/c-type-inference-and-extension-methods-an-abuse#comments</comments>
		<pubDate>Wed, 23 Feb 2011 03:11:48 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[silly]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=554</guid>
		<description><![CDATA[Here&#8217;s a little example of statically-sized stacks in C#. They&#8217;re implemented with a linked-list as the backing store: using System; &#160; namespace ConsoleApplication &#123; public static class MainClass &#123; // Example: &#160; public static void Main&#40;string&#91;&#93; args&#41; &#123; var stack = Stack.New&#60;int&#62;&#40;&#41;; // real type Stack&#60;Z,int&#62; &#160; var s1 = stack.Push&#40;1&#41;.Push&#40;2&#41;.Push&#40;3&#41;; // real type Stack&#60;S&#60;S&#60;S&#60;Z&#62;&#62;&#62;&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a little example of statically-sized stacks in C#. They&#8217;re implemented with a linked-list as the backing store:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> ConsoleApplication
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> MainClass
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Example:</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            var stack <span style="color: #008000;">=</span> Stack<span style="color: #008000;">.</span><span style="color: #008000;">New</span><span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// real type Stack&lt;Z,int&gt;</span>
&nbsp;
            var s1 <span style="color: #008000;">=</span> stack<span style="color: #008000;">.</span><span style="color: #0000FF;">Push</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Push</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Push</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// real type Stack&lt;S&lt;S&lt;S&lt;Z&gt;&gt;&gt;&gt;</span>
            var s2 <span style="color: #008000;">=</span> s1<span style="color: #008000;">.</span><span style="color: #0000FF;">Pop</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Pop</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Pop</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// real type Stack&lt;Z,int&gt;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// statically disallowed:</span>
            <span style="color: #008080; font-style: italic;">// s2.Pop();</span>
&nbsp;
            Pop2<span style="color: #008000;">&#40;</span>s1<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>s1<span style="color: #008000;">.</span><span style="color: #0000FF;">IsEmpty</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>s2<span style="color: #008000;">.</span><span style="color: #0000FF;">IsEmpty</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Example of a method requiring &quot;at least 2&quot; items on stack.</span>
        <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Pop2<span style="color: #008000;">&lt;</span>TRest,T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>Stack<span style="color: #008000;">&lt;</span>S<span style="color: #008000;">&lt;</span>S<span style="color: #008000;">&lt;</span>TRest<span style="color: #008000;">&gt;&gt;</span>,T<span style="color: #008000;">&gt;</span> stack<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            T x1<span style="color: #008000;">;</span>
            T x2<span style="color: #008000;">;</span>
            stack<span style="color: #008000;">.</span><span style="color: #0000FF;">Pop</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">out</span> x1<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Pop</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">out</span> x2<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;{0}, {1}&quot;</span>, x1, x2<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Implementation:</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// very weak type level naturals</span>
    <span style="color: #008080; font-style: italic;">// Z = zero, S&lt;x&gt; = successor of x.</span>
    <span style="color: #008080; font-style: italic;">// so, S&lt;Z&gt; = 1, S&lt;S&lt;Z&gt;&gt; = 2, etc.</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">sealed</span> <span style="color: #6666cc; font-weight: bold;">class</span> S<span style="color: #008000;">&lt;</span>TPrev<span style="color: #008000;">&gt;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">sealed</span> <span style="color: #6666cc; font-weight: bold;">class</span> Z <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// internal node type for the linked list</span>
    <span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #6666cc; font-weight: bold;">class</span> StackNode<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> StackNode<span style="color: #008000;">&#40;</span>T item, StackNode<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> next<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">item</span> <span style="color: #008000;">=</span> item<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">next</span> <span style="color: #008000;">=</span> next<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">internal</span> T item<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">internal</span> StackNode<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> next<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// the stack wrapper type (sized with types)</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Stack<span style="color: #008000;">&lt;</span>TSize,T<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">internal</span> StackNode<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> node<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Construct wrapper:</span>
        <span style="color: #0600FF; font-weight: bold;">internal</span> Stack<span style="color: #008000;">&#40;</span>StackNode<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> node<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">node</span> <span style="color: #008000;">=</span> node<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Push:</span>
        <span style="color: #0600FF; font-weight: bold;">internal</span> Stack<span style="color: #008000;">&#40;</span>T item, StackNode<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> node<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">node</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StackNode<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>item, node<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// note that the return type has the size increased by 1</span>
        <span style="color: #008080; font-style: italic;">// S&lt;TSize&gt; ~= TSize++ </span>
        <span style="color: #0600FF; font-weight: bold;">public</span> Stack<span style="color: #008000;">&lt;</span>S<span style="color: #008000;">&lt;</span>TSize<span style="color: #008000;">&gt;</span>,T<span style="color: #008000;">&gt;</span> Push<span style="color: #008000;">&#40;</span>T item<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> Stack<span style="color: #008000;">&lt;</span>S<span style="color: #008000;">&lt;</span>TSize<span style="color: #008000;">&gt;</span>,T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>item, node<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// extension methods that perform the operations:</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> Stack
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// a new stack has size zero</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> Stack<span style="color: #008000;">&lt;</span>Z,T<span style="color: #008000;">&gt;</span> <span style="color: #008000;">New</span><span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> Stack<span style="color: #008000;">&lt;</span>Z, T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// pop with ignored result</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> Stack<span style="color: #008000;">&lt;</span>TRest,T<span style="color: #008000;">&gt;</span> Pop<span style="color: #008000;">&lt;</span>TRest,T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> Stack<span style="color: #008000;">&lt;</span>S<span style="color: #008000;">&lt;</span>TRest<span style="color: #008000;">&gt;</span>,T<span style="color: #008000;">&gt;</span> stack<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            T ignored<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> stack<span style="color: #008000;">.</span><span style="color: #0000FF;">Pop</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">out</span> ignored<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// popping an item removes one S&lt;&gt; from the size (~= TSize--)</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> Stack<span style="color: #008000;">&lt;</span>TRest,T<span style="color: #008000;">&gt;</span> Pop<span style="color: #008000;">&lt;</span>TRest,T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> Stack<span style="color: #008000;">&lt;</span>S<span style="color: #008000;">&lt;</span>TRest<span style="color: #008000;">&gt;</span>,T<span style="color: #008000;">&gt;</span> stack, <span style="color: #0600FF; font-weight: bold;">out</span> T item<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            item <span style="color: #008000;">=</span> stack<span style="color: #008000;">.</span><span style="color: #0000FF;">node</span><span style="color: #008000;">.</span><span style="color: #0000FF;">item</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> Stack<span style="color: #008000;">&lt;</span>TRest, T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>stack<span style="color: #008000;">.</span><span style="color: #0000FF;">node</span><span style="color: #008000;">.</span><span style="color: #0000FF;">next</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// These are statically known, because of the types:</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> IsEmpty<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> Stack<span style="color: #008000;">&lt;</span>Z,T<span style="color: #008000;">&gt;</span> stack<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> IsEmpty<span style="color: #008000;">&lt;</span>TRest, T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> Stack<span style="color: #008000;">&lt;</span>S<span style="color: #008000;">&lt;</span>TRest<span style="color: #008000;">&gt;</span>, T<span style="color: #008000;">&gt;</span> stack<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>One fun thing about this is that we never have to check whether we&#8217;ve hit the bottom of the stack (<code>node.next == null</code>). Since the size is statically part of the type, and there&#8217;s no Pop for an empty list, we never have to check for this situation.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/c-type-inference-and-extension-methods-an-abuse/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>n-pivot quicksort</title>
		<link>http://porg.es/blog/n-pivot-quicksort</link>
		<comments>http://porg.es/blog/n-pivot-quicksort#comments</comments>
		<pubDate>Sat, 12 Sep 2009 07:56:15 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[comparison]]></category>
		<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[quicksort]]></category>
		<category><![CDATA[sorting]]></category>

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

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

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

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

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

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

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

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

<p>Sample output:</p>
<pre>$ ./sort
858124546
quicksort = 858124546
CPU time:   4.60s
quicksort2 = 858124546
CPU time:   3.66s
quicksortN 1 = 858124546
CPU time:   5.19s
quicksortN 2 = 858124546
CPU time:   3.77s
quicksortN 3 = 858124546
CPU time:   3.28s
quicksortN 4 = 858124546
CPU time:   3.27s
quicksortN 5 = 858124546
CPU time:   3.06s
quicksortN 6 = 858124546
CPU time:   3.04s
quicksortN 7 = 858124546
CPU time:   3.04s
quicksortN 8 = 858124546
CPU time:   3.07s
quicksortN 9 = 858124546
CPU time:   3.10s
quicksortN 10 = 858124546
CPU time:   3.14s
quicksortN 11 = 858124546
CPU time:   3.22s
quicksortN 12 = 858124546
CPU time:   3.31s
quicksortN 13 = 858124546
CPU time:   3.30s
quicksortN 14 = 858124546
CPU time:   3.37s
quicksortN 15 = 858124546
CPU time:   3.54s
quicksortN 16 = 858124546
CPU time:   3.41s
quicksortN 17 = 858124546
CPU time:   3.52s
quicksortN 18 = 858124546
CPU time:   3.65s
quicksortN 19 = 858124546
CPU time:   3.63s
quicksortN 20 = 858124546
CPU time:   3.77s
</pre>
<p>As expected, generalized quicksort performs slower than the equivalent hard-coded quicksorts for specific values of <i>n</i>, but higher values of <i>n</i> than 1 or 2 can perform better.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/n-pivot-quicksort/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Debugging Turing: An excursion with Scheme</title>
		<link>http://porg.es/blog/debugging-turing-an-excursion-with-scheme</link>
		<comments>http://porg.es/blog/debugging-turing-an-excursion-with-scheme#comments</comments>
		<pubDate>Sun, 09 Aug 2009 15:07:55 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[ikarus]]></category>
		<category><![CDATA[machine]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[scheme]]></category>
		<category><![CDATA[silly]]></category>
		<category><![CDATA[theory]]></category>
		<category><![CDATA[turing]]></category>
		<category><![CDATA[universal]]></category>
		<category><![CDATA[ypsilon]]></category>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

		<guid isPermaLink="false">http://porg.es/blog/?p=365</guid>
		<description><![CDATA[Argh #include &#60;stdio.h&#62; #include &#60;math.h&#62; #include &#60;fenv.h&#62; &#160; int main &#40;&#41; &#123; // don't set rounding here double ten0 = sin&#40;pow&#40;10.0,22&#41;&#41;; &#160; fesetround&#40;FE_DOWNWARD&#41;; double ten1 = sin&#40;pow&#40;10.0,22&#41;&#41;; &#160; fesetround&#40;FE_UPWARD&#41;; double ten2 = sin&#40;pow&#40;10.0,22&#41;&#41;; &#160; fesetround&#40;FE_TONEAREST&#41;; double ten3 = sin&#40;pow&#40;10.0,22&#41;&#41;; &#160; fesetround&#40;FE_TOWARDZERO&#41;; double ten4 = sin&#40;pow&#40;10.0,22&#41;&#41;; &#160; printf&#40; &#34;Default: %f\n&#34; &#34;Downward: %f\n&#34; &#34;Upward: %f\n&#34; &#34;ToNearest: %f\n&#34; [...]]]></description>
			<content:encoded><![CDATA[<p>Argh <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_sad.gif" alt="" /></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;math.h&gt;</span>
<span style="color: #339933;">#include &lt;fenv.h&gt;</span>
&nbsp;
<span style="color: #993333;">int</span> main <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// don't set rounding here</span>
	<span style="color: #993333;">double</span> ten0 <span style="color: #339933;">=</span> sin<span style="color: #009900;">&#40;</span>pow<span style="color: #009900;">&#40;</span><span style="color:#800080;">10.0</span><span style="color: #339933;">,</span><span style="color: #0000dd;">22</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	fesetround<span style="color: #009900;">&#40;</span>FE_DOWNWARD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">double</span> ten1 <span style="color: #339933;">=</span> sin<span style="color: #009900;">&#40;</span>pow<span style="color: #009900;">&#40;</span><span style="color:#800080;">10.0</span><span style="color: #339933;">,</span><span style="color: #0000dd;">22</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	fesetround<span style="color: #009900;">&#40;</span>FE_UPWARD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">double</span> ten2 <span style="color: #339933;">=</span> sin<span style="color: #009900;">&#40;</span>pow<span style="color: #009900;">&#40;</span><span style="color:#800080;">10.0</span><span style="color: #339933;">,</span><span style="color: #0000dd;">22</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	fesetround<span style="color: #009900;">&#40;</span>FE_TONEAREST<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">double</span> ten3 <span style="color: #339933;">=</span> sin<span style="color: #009900;">&#40;</span>pow<span style="color: #009900;">&#40;</span><span style="color:#800080;">10.0</span><span style="color: #339933;">,</span><span style="color: #0000dd;">22</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	fesetround<span style="color: #009900;">&#40;</span>FE_TOWARDZERO<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">double</span> ten4 <span style="color: #339933;">=</span> sin<span style="color: #009900;">&#40;</span>pow<span style="color: #009900;">&#40;</span><span style="color:#800080;">10.0</span><span style="color: #339933;">,</span><span style="color: #0000dd;">22</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span>
		<span style="color: #ff0000;">&quot;Default:    %f<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #ff0000;">&quot;Downward:   %f<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #ff0000;">&quot;Upward:     %f<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #ff0000;">&quot;ToNearest:  %f<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #ff0000;">&quot;TowardZero: %f<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>
		ten0<span style="color: #339933;">,</span> ten1<span style="color: #339933;">,</span>
		ten2<span style="color: #339933;">,</span> ten3<span style="color: #339933;">,</span> ten4<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">gcc</span> test.c <span style="color: #660033;">-lm</span> <span style="color: #660033;">-fno-builtin</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> .<span style="color: #000000; font-weight: bold;">/</span>a.out
Default:    <span style="color: #000000;">0.462613</span>
Downward:   <span style="color: #000000;">0.986580</span>
Upward:     <span style="color: #000000;">0.462613</span>
ToNearest:  <span style="color: #000000;">0.462613</span>
TowardZero: <span style="color: #000000;">0.986580</span>
~$ <span style="color: #c20cb9; font-weight: bold;">gcc</span> test.c <span style="color: #660033;">-lm</span>  <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> .<span style="color: #000000; font-weight: bold;">/</span>a.out
Default:    -<span style="color: #000000;">0.852201</span>
Downward:   -<span style="color: #000000;">0.852201</span>
Upward:     -<span style="color: #000000;">0.852201</span>
ToNearest:  -<span style="color: #000000;">0.852201</span>
TowardZero: -<span style="color: #000000;">0.852201</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/sometimes-its-all-too-much/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 &#123; print &#34;ibase=16&#34; &#125; # 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;"><span style="color: #C20CB9; font-weight: bold;">BEGIN</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> <span style="color: #0BD507; font-weight: bold;">print</span> <span style="color: #ff0000;">&quot;ibase=16&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span> <span style="color:#808080;"># set bc to hex mode</span>
&nbsp;
<span style="color:#000088;">$2</span> <span style="color:#C4C364;">~</span> <span style="color:black;">/</span>Private<span style="color:black;">/</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> <span style="color:#808080;"># skip any lines with &quot;private&quot; in the description</span>
    <span style="color: #0BD507; font-weight: bold;">getline</span>;
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
n <span style="color: #7a0874; font-weight: bold;">&#123;</span> <span style="color:#808080;"># if n is set, then print the range for bc to calculate</span>
    <span style="color: #0BD507; font-weight: bold;">printf</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;(%s-%s+1)+&quot;</span>,<span style="color:#000088;">$1</span>,n<span style="color: #7a0874; font-weight: bold;">&#41;</span>;
    n=<span style="color: #ff0000;">&quot;&quot;</span>;
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color:#000088;">$2</span> <span style="color:#C4C364;">~</span> <span style="color:black;">/</span>First<span style="color:black;">&gt;</span><span style="color:black;">/</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> <span style="color:#808080;"># set n if the start of a range</span>
    n=<span style="color:#000088;">$1</span>;
    <span style="color: #0BD507; font-weight: bold;">getline</span>;
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color:#000088;">$3</span> <span style="color:#C4C364;">!~</span> <span style="color: #ff0000;">&quot;C.&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> <span style="color:#808080;"># otherwise count anything that isn't some kind of a control character</span>
    i<span style="color:black;">++</span>;
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #C20CB9; font-weight: bold;">END</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> <span style="color:#808080;"># print out the count of everything else</span>
    <span style="color: #0BD507; font-weight: bold;">printf</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;%X<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>,i<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></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>12</slash:comments>
		</item>
		<item>
		<title>Email address validation: Simpler, Faster, More Correct</title>
		<link>http://porg.es/blog/email-address-validation-simpler-faster-more-correct</link>
		<comments>http://porg.es/blog/email-address-validation-simpler-faster-more-correct#comments</comments>
		<pubDate>Wed, 11 Mar 2009 11:10:31 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[address]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[rfc]]></category>
		<category><![CDATA[rfc5322]]></category>
		<category><![CDATA[validation]]></category>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

<p>I have not yet found a workaround for this. Help would be nice <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_smile.gif" alt="" /></p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/parametrizing-monoids-and-monads/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>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 <span style="color: #7a0874; font-weight: bold;">&#123;</span> expression <span style="color: #7a0874; font-weight: bold;">&#125;</span></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;"><span style="color: #C20CB9; font-weight: bold;">BEGIN</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> <span style="color: #4107D5; font-weight: bold;">FS</span> = <span style="color: #ff0000;">&quot;|&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span></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;"><span style="color:#000088;">$0</span> <span style="color:#C4C364;">~</span> <span style="color:black;">/</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>a<span style="color:black;">-</span>zA<span style="color:black;">-</span>Z<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color:black;">/</span></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;"><span style="color: #7a0874; font-weight: bold;">&#123;</span> tag_counts<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color:#000088;">$2</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> = <span style="color:#000088;">$1</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #C20CB9; font-weight: bold;">END</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>tag <span style="color: #000000; font-weight: bold;">in</span> tag_counts<span style="color: #7a0874; font-weight: bold;">&#41;</span>
	<span style="color: #7a0874; font-weight: bold;">&#123;</span>
		normtag=tag
		<span style="color: #07D589; font-weight: bold;">gsub</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color:black;">/-</span>|_<span style="color:black;">/</span>,<span style="color: #ff0000;">&quot;&quot;</span>,normtag<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
		count=tag_counts<span style="color: #7a0874; font-weight: bold;">&#91;</span>tag<span style="color: #7a0874; font-weight: bold;">&#93;</span>
		sum<span style="color: #7a0874; font-weight: bold;">&#91;</span>normtag<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color:black;">+</span>=count
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>count <span style="color:black;">&gt;</span> max<span style="color: #7a0874; font-weight: bold;">&#91;</span>normtag<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
		<span style="color: #7a0874; font-weight: bold;">&#123;</span>
			names<span style="color: #7a0874; font-weight: bold;">&#91;</span>normtag<span style="color: #7a0874; font-weight: bold;">&#93;</span>=tag
			max<span style="color: #7a0874; font-weight: bold;">&#91;</span>normtag<span style="color: #7a0874; font-weight: bold;">&#93;</span>=count
		<span style="color: #7a0874; font-weight: bold;">&#125;</span>
	<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>tag <span style="color: #000000; font-weight: bold;">in</span> names<span style="color: #7a0874; font-weight: bold;">&#41;</span>
	<span style="color: #7a0874; font-weight: bold;">&#123;</span>
		finaltag=names<span style="color: #7a0874; font-weight: bold;">&#91;</span>tag<span style="color: #7a0874; font-weight: bold;">&#93;</span>
		<span style="color: #07D589; font-weight: bold;">gsub</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color:black;">/</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color:black;">-</span>|_<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color:black;">+/</span>,<span style="color: #ff0000;">&quot;_&quot;</span>,finaltag<span style="color: #7a0874; font-weight: bold;">&#41;</span>
		<span style="color: #0BD507; font-weight: bold;">print</span> <span style="color: #ff0000;">&quot; &quot;</span> sum<span style="color: #7a0874; font-weight: bold;">&#91;</span>tag<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #ff0000;">&quot; &quot;</span> finaltag
	<span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></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;"><span style="color: #7a0874; font-weight: bold;">&#123;</span> tag_counts<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color:#000088;">$2</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> = <span style="color:#000088;">$1</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span></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
<span style="color: #07D589; font-weight: bold;">gsub</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color:black;">/-</span>|_<span style="color:black;">/</span>,<span style="color: #ff0000;">&quot;&quot;</span>,normtag<span style="color: #7a0874; font-weight: bold;">&#41;</span></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<span style="color: #7a0874; font-weight: bold;">&#91;</span>tag<span style="color: #7a0874; font-weight: bold;">&#93;</span>
sum<span style="color: #7a0874; font-weight: bold;">&#91;</span>normtag<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color:black;">+</span>=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;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>count <span style="color:black;">&gt;</span> max<span style="color: #7a0874; font-weight: bold;">&#91;</span>normtag<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#123;</span>
	names<span style="color: #7a0874; font-weight: bold;">&#91;</span>normtag<span style="color: #7a0874; font-weight: bold;">&#93;</span>=tag
	max<span style="color: #7a0874; font-weight: bold;">&#91;</span>normtag<span style="color: #7a0874; font-weight: bold;">&#93;</span>=count
<span style="color: #7a0874; font-weight: bold;">&#125;</span></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<span style="color: #7a0874; font-weight: bold;">&#91;</span>tag<span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #07D589; font-weight: bold;">gsub</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color:black;">/</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color:black;">-</span>|_<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color:black;">+/</span>,<span style="color: #ff0000;">&quot;_&quot;</span>,finaltag<span style="color: #7a0874; font-weight: bold;">&#41;</span></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;"><span style="color: #0BD507; font-weight: bold;">print</span> <span style="color: #ff0000;">&quot; &quot;</span> sum<span style="color: #7a0874; font-weight: bold;">&#91;</span>tag<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #ff0000;">&quot; &quot;</span> 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;"><span style="color: #C20CB9; font-weight: bold;">BEGIN</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #0BD507; font-weight: bold;">getline</span> <span style="color:black;">&lt;</span> <span style="color: #ff0000;">&quot;smart.txt&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
    <span style="color: #7a0874; font-weight: bold;">&#123;</span> stopwords<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color:#000088;">$0</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> = <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color:black;">!</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color:#000088;">$2</span> <span style="color: #000000; font-weight: bold;">in</span> stopwords<span style="color: #7a0874; font-weight: bold;">&#41;</span></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>
	</channel>
</rss>

