<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Reading TAoCP</title>
	<atom:link href="http://readingtaocp.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://readingtaocp.wordpress.com</link>
	<description>Playing with The Art</description>
	<lastBuildDate>Wed, 25 Feb 2009 17:34:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='readingtaocp.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Reading TAoCP</title>
		<link>http://readingtaocp.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://readingtaocp.wordpress.com/osd.xml" title="Reading TAoCP" />
	<atom:link rel='hub' href='http://readingtaocp.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Sec. 1.2.1: Mathematical Induction</title>
		<link>http://readingtaocp.wordpress.com/2008/09/17/sec-121-mathematical-induction/</link>
		<comments>http://readingtaocp.wordpress.com/2008/09/17/sec-121-mathematical-induction/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 23:35:13 +0000</pubDate>
		<dc:creator>Andrei Formiga</dc:creator>
				<category><![CDATA[vol1]]></category>
		<category><![CDATA[induction]]></category>
		<category><![CDATA[recursion]]></category>

		<guid isPermaLink="false">http://readingtaocp.wordpress.com/?p=24</guid>
		<description><![CDATA[This section presents the induction principle for proofs, with good examples. I actually read this section carefully. There&#8217;s even a quick discussion about using induction for proving correctness of programs, using some kind of Floyd-Hoare logic. Exercises are quite good, as usual. This being a book about algorithms, there&#8217;s no mention to the recursion theorem [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=readingtaocp.wordpress.com&amp;blog=4612758&amp;post=24&amp;subd=readingtaocp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This section presents the induction principle for proofs, with good examples. I actually read this section carefully. There&#8217;s even a quick discussion about using induction for proving correctness of programs, using some kind of <a href="http://en.wikipedia.org/wiki/Hoare_logic">Floyd-Hoare logic</a>. Exercises are quite good, as usual. This being a book about algorithms, there&#8217;s no mention to the <a href="http://www.mathreference.com/set-zf,rect.html">recursion theorem</a> in the context of set theory, which is commonly studied along with induction principles in courses or books about sets. However, the last exercise here is about generalizing induction for well-ordered sets. Nothing about <a href="http://en.wikipedia.org/wiki/Corecursion">corecursion</a>, though.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/readingtaocp.wordpress.com/24/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/readingtaocp.wordpress.com/24/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/readingtaocp.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/readingtaocp.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/readingtaocp.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/readingtaocp.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/readingtaocp.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/readingtaocp.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/readingtaocp.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/readingtaocp.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/readingtaocp.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/readingtaocp.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/readingtaocp.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/readingtaocp.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/readingtaocp.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/readingtaocp.wordpress.com/24/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=readingtaocp.wordpress.com&amp;blog=4612758&amp;post=24&amp;subd=readingtaocp&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://readingtaocp.wordpress.com/2008/09/17/sec-121-mathematical-induction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/91fa687725c763561519614037bb31a9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tautologico</media:title>
		</media:content>
	</item>
		<item>
		<title>Sec. 1.2: Mathematical Preliminaries</title>
		<link>http://readingtaocp.wordpress.com/2008/09/17/sec-12-mathematical-preliminaries/</link>
		<comments>http://readingtaocp.wordpress.com/2008/09/17/sec-12-mathematical-preliminaries/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 23:23:12 +0000</pubDate>
		<dc:creator>Andrei Formiga</dc:creator>
				<category><![CDATA[vol1]]></category>
		<category><![CDATA[cmath]]></category>
		<category><![CDATA[j]]></category>

		<guid isPermaLink="false">http://readingtaocp.wordpress.com/?p=22</guid>
		<description><![CDATA[This is the big section with the mathematical background required for algorithm analysis. The first time I started reading vol. 1, I spent a long time in this section, eventually losing interest. Not that I don&#8217;t like math, quite the contrary; but I started reading the book expecting algorithms, and it took too damn long [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=readingtaocp.wordpress.com&amp;blog=4612758&amp;post=22&amp;subd=readingtaocp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is the big section with the mathematical background required for algorithm analysis. The first time I started reading vol. 1, I spent a long time in this section, eventually losing interest. Not that I don&#8217;t like math, quite the contrary; but I started reading the book expecting algorithms, and it took too damn long to get to them. So here is what Knuth says in the beginning of this section:</p>
<blockquote><p>The reader may choose to read the following subsections carefully, with implicit faith in the author&#8217;s assertion that the topics treated here are indeed very relevant; but it is probably preferable, for motivation, to skim over this section lightly at first, and (after seeing numerous applications of the techniques in future chapters) return to it later for more intensive study. If too much time is spent studying this material when first reading the book, a person might never get on to the computer programming topics!</p></blockquote>
<p>And so it is that I am skimming this section. Later I can get back to it for some fun and games. I might also get back to read the whole <a href="http://www-cs-faculty.stanford.edu/~uno/gkp.html">Concrete Mathematics</a> book, another one I started and never finished. Additional motivation to read the CMath book is that there is a free ebook called <a href="http://www.jsoftware.com/jwiki/Books#head-7f1226badddd8670bb01cc46fc4fc24a48ddd797">Concrete Math Companion</a>, which presents <a href="http://www.jsoftware.com/">J</a> programs and algorithms related to subjects treated in CMath. Just imagine the geekiness factor of this whole endeavor. It boggles the mind.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/readingtaocp.wordpress.com/22/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/readingtaocp.wordpress.com/22/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/readingtaocp.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/readingtaocp.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/readingtaocp.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/readingtaocp.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/readingtaocp.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/readingtaocp.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/readingtaocp.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/readingtaocp.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/readingtaocp.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/readingtaocp.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/readingtaocp.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/readingtaocp.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/readingtaocp.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/readingtaocp.wordpress.com/22/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=readingtaocp.wordpress.com&amp;blog=4612758&amp;post=22&amp;subd=readingtaocp&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://readingtaocp.wordpress.com/2008/09/17/sec-12-mathematical-preliminaries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/91fa687725c763561519614037bb31a9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tautologico</media:title>
		</media:content>
	</item>
		<item>
		<title>Markov algorithms in Scala</title>
		<link>http://readingtaocp.wordpress.com/2008/09/13/markov-algorithms-in-scala/</link>
		<comments>http://readingtaocp.wordpress.com/2008/09/13/markov-algorithms-in-scala/#comments</comments>
		<pubDate>Sat, 13 Sep 2008 04:03:46 +0000</pubDate>
		<dc:creator>Andrei Formiga</dc:creator>
				<category><![CDATA[vol1]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://readingtaocp.wordpress.com/?p=18</guid>
		<description><![CDATA[Now here&#8217;s the Scala code for Markov algorithms. I mostly followed the form of the Haskell code, except for string processing, which is done with StringBuilders to improve performance. Definitely a premature optimization. case class StepParams(theta: String, phi: String, b: Int, a: Int) class Algorithm(params: Array[StepParams]) { private def replaceIfPossible(p: StepParams, s: StringBuilder) = { [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=readingtaocp.wordpress.com&amp;blog=4612758&amp;post=18&amp;subd=readingtaocp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Now here&#8217;s the Scala code for Markov algorithms. I mostly followed the form of the <a href="http://readingtaocp.wordpress.com/2008/09/13/markov-algorithms-in-haskell/">Haskell code</a>, except for string processing, which is done with StringBuilders to improve performance. Definitely a premature optimization.</p>
<pre><span style="color:#a020f0;">case</span> <span style="color:#a020f0;">class</span> <span style="color:#228b22;">StepParams</span>(<span style="color:#b8860b;">theta</span>: <span style="color:#228b22;">String</span>, <span style="color:#b8860b;">phi</span>: <span style="color:#228b22;">String</span>, <span style="color:#b8860b;">b</span>: <span style="color:#228b22;">Int</span>, <span style="color:#b8860b;">a</span>: <span style="color:#228b22;">Int</span>)

<span style="color:#a020f0;">class</span> <span style="color:#228b22;">Algorithm</span>(<span style="color:#b8860b;">params</span>: <span style="color:#228b22;">Array[StepParams]</span>) {
  <span style="color:#a020f0;">private</span> <span style="color:#a020f0;">def</span> <span style="color:#0000ff;">replaceIfPossible</span>(<span style="color:#b8860b;">p</span>: <span style="color:#228b22;">StepParams</span>, <span style="color:#b8860b;">s</span>: <span style="color:#228b22;">StringBuilder</span>) =
  {
    <span style="color:#a020f0;">val</span> <span style="color:#b8860b;">i</span> = s.indexOf(p.theta)
    <span style="color:#a020f0;">if</span> (i != -1)
    {
      s.replace(i, i + p.theta.length, p.phi)
      <span style="color:#5f9ea0;">true</span>
    }
    <span style="color:#a020f0;">else</span> <span style="color:#5f9ea0;">false</span>
  }

  <span style="color:#a020f0;">private</span> <span style="color:#a020f0;">def</span> <span style="color:#0000ff;">execLoop</span>(<span style="color:#b8860b;">word</span>: <span style="color:#228b22;">StringBuilder</span>, <span style="color:#b8860b;">step</span>: <span style="color:#228b22;">Int</span>): <span style="color:#228b22;">String </span>=
  {
    <span style="color:#a020f0;">if</span> (step == params.length)
      word.toString
    <span style="color:#a020f0;">else</span> <span style="color:#a020f0;">if</span> (replaceIfPossible(params(step), word))
      execLoop(word, params(step).b)
    <span style="color:#a020f0;">else</span>
      execLoop(word, params(step).a)
  }

  <span style="color:#a020f0;">def</span> <span style="color:#0000ff;">exec</span>(<span style="color:#b8860b;">in</span>: <span style="color:#228b22;">String</span>) = execLoop(<span style="color:#a020f0;">new</span> StringBuilder(in), 0)
}

<span style="color:#a020f0;">object</span> <span style="color:#b8860b;">Markov</span> {
  <span style="color:#a020f0;">val</span> <span style="color:#b8860b;">absDiff</span> = <span style="color:#a020f0;">new</span> Algorithm(Array(StepParams(<span style="color:#bc8f8f;">"ab"</span>, <span style="color:#bc8f8f;">""</span> , 0, 1),
                                    StepParams(<span style="color:#bc8f8f;">"a"</span> , <span style="color:#bc8f8f;">"c"</span>, 1, 2),
                                    StepParams(<span style="color:#bc8f8f;">"b"</span> , <span style="color:#bc8f8f;">"c"</span>, 2, 3)))

  <span style="color:#a020f0;">val</span> <span style="color:#b8860b;">min</span> = <span style="color:#a020f0;">new</span> Algorithm(Array(StepParams(<span style="color:#bc8f8f;">"ab"</span>, <span style="color:#bc8f8f;">"d"</span> , 1, 3),
                                StepParams(<span style="color:#bc8f8f;">"ad"</span>, <span style="color:#bc8f8f;">"d"</span> , 2, 3),
                                StepParams(<span style="color:#bc8f8f;">"db"</span>, <span style="color:#bc8f8f;">"dd"</span>, 1, 3),
                                StepParams(<span style="color:#bc8f8f;">"a"</span> , <span style="color:#bc8f8f;">""</span>  , 3, 4),
                                StepParams(<span style="color:#bc8f8f;">"b"</span> , <span style="color:#bc8f8f;">""</span>  , 4, 5)))

  <span style="color:#b22222;">// </span><span style="color:#b22222;">The gcd algorithm, Exercise 8, Section 1.1
</span>  <span style="color:#a020f0;">val</span> <span style="color:#b8860b;">gcd</span> = <span style="color:#a020f0;">new</span> Algorithm(Array(StepParams(<span style="color:#bc8f8f;">"ab"</span>, <span style="color:#bc8f8f;">""</span> , 1, 2),
                                StepParams(<span style="color:#bc8f8f;">""</span>  , <span style="color:#bc8f8f;">"c"</span>, 0, 2),
                                StepParams(<span style="color:#bc8f8f;">"a"</span> , <span style="color:#bc8f8f;">"b"</span>, 2, 3),
                                StepParams(<span style="color:#bc8f8f;">"c"</span> , <span style="color:#bc8f8f;">"a"</span>, 3, 4),
                                StepParams(<span style="color:#bc8f8f;">"b"</span> , <span style="color:#bc8f8f;">"b"</span>, 0, 5)))

  <span style="color:#a020f0;">def</span> <span style="color:#0000ff;">replicate</span>(<span style="color:#b8860b;">n</span>: <span style="color:#228b22;">Int</span>, <span style="color:#b8860b;">c</span>: <span style="color:#228b22;">Char</span>) =
  {
    <span style="color:#a020f0;">val</span> <span style="color:#b8860b;">sb</span> = <span style="color:#a020f0;">new</span> StringBuffer(n)
    <span style="color:#a020f0;">for</span> (i &lt;- 1 to n)
      sb.append(c)
    sb.toString
  }

  <span style="color:#a020f0;">def</span> <span style="color:#0000ff;">buildGcdString</span>(<span style="color:#b8860b;">m</span>: <span style="color:#228b22;">Int</span>, <span style="color:#b8860b;">n</span>: <span style="color:#228b22;">Int</span>) = replicate(m, <span style="color:#bc8f8f;">'a'</span>) + replicate(n, <span style="color:#bc8f8f;">'b'</span>)

  <span style="color:#a020f0;">def</span> <span style="color:#0000ff;">main</span>(<span style="color:#b8860b;">args</span>: <span style="color:#228b22;">Array[String]</span>)
  {
    <span style="color:#a020f0;">val</span> <span style="color:#b8860b;">in</span> = <span style="color:#a020f0;">if</span> (args.length &gt; 0) args(0) <span style="color:#a020f0;">else</span> <span style="color:#bc8f8f;">"aaabb"</span>
    println(<span style="color:#bc8f8f;">"absDiff: "</span> + absDiff.exec(in))
    println(<span style="color:#bc8f8f;">"min    : "</span> + min.exec(in))
    println(<span style="color:#bc8f8f;">"gcd    : "</span> + gcd.exec(in))
  }
}</pre>
<p>After loading this in the interpreter we can try it:</p>
<pre style="font-size:medium;">scala&gt; Markov.gcd.exec(Markov.buildGcdString(24, 16))
res1: String = aaaaaaaa</pre>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/readingtaocp.wordpress.com/18/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/readingtaocp.wordpress.com/18/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/readingtaocp.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/readingtaocp.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/readingtaocp.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/readingtaocp.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/readingtaocp.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/readingtaocp.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/readingtaocp.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/readingtaocp.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/readingtaocp.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/readingtaocp.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/readingtaocp.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/readingtaocp.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/readingtaocp.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/readingtaocp.wordpress.com/18/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=readingtaocp.wordpress.com&amp;blog=4612758&amp;post=18&amp;subd=readingtaocp&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://readingtaocp.wordpress.com/2008/09/13/markov-algorithms-in-scala/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/91fa687725c763561519614037bb31a9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tautologico</media:title>
		</media:content>
	</item>
		<item>
		<title>Markov algorithms in Haskell</title>
		<link>http://readingtaocp.wordpress.com/2008/09/13/markov-algorithms-in-haskell/</link>
		<comments>http://readingtaocp.wordpress.com/2008/09/13/markov-algorithms-in-haskell/#comments</comments>
		<pubDate>Sat, 13 Sep 2008 03:50:37 +0000</pubDate>
		<dc:creator>Andrei Formiga</dc:creator>
				<category><![CDATA[vol1]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://readingtaocp.wordpress.com/?p=16</guid>
		<description><![CDATA[What follows are the basic definitions and the execution engine for Markov algorithms, written in Haskell. After the code I&#8217;ll show how to use it with some sample algorithms. import Data.List -- The parameters for a computational step -- Each step is (theta, phi, b, a), following Knuth's notation type StepParams = (String, String, Int, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=readingtaocp.wordpress.com&amp;blog=4612758&amp;post=16&amp;subd=readingtaocp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>What follows are the basic definitions and the execution engine for Markov algorithms, written in Haskell. After the code I&#8217;ll show how to use it with some sample algorithms.</p>
<pre style="font-size:medium;"><span style="color:#a020f0;">import</span> <span style="color:#228b22;">Data.List</span>

<span style="color:#b22222;">-- </span><span style="color:#b22222;">The parameters for a computational step
</span><span style="color:#b22222;">-- </span><span style="color:#b22222;">Each step is (theta, phi, b, a), following Knuth's notation
</span><span style="color:#a020f0;">type</span> <span style="color:#228b22;">StepParams</span> <span style="color:#b8860b;">=</span> (<span style="color:#228b22;">String</span>, <span style="color:#228b22;">String</span>, <span style="color:#228b22;">Int</span>, <span style="color:#228b22;">Int</span>)

<span style="color:#b22222;">-- </span><span style="color:#b22222;">An algorithm is a sequence of steps
</span><span style="color:#a020f0;">type</span> <span style="color:#228b22;">Algorithm</span> <span style="color:#b8860b;">=</span> [<span style="color:#228b22;">StepParams</span>]

<span style="color:#b22222;">-- </span><span style="color:#b22222;">replace the first (leftmost) occurrence of string from with string to
</span><span style="color:#0000ff;">replace</span> <span style="color:#b8860b;">::</span> <span style="color:#228b22;">Eq</span> a <span style="color:#b8860b;">=&gt;</span> [a] <span style="color:#b8860b;">-&gt;</span> [a] <span style="color:#b8860b;">-&gt;</span> [a] <span style="color:#b8860b;">-&gt;</span> [a]
<span style="color:#0000ff;">replace</span> <span style="color:#228b22;">[]</span>   to <span style="color:#228b22;">[]</span> <span style="color:#b8860b;">=</span> to
<span style="color:#0000ff;">replace</span> <span style="color:#a020f0;">_</span>    <span style="color:#a020f0;">_</span>  <span style="color:#228b22;">[]</span> <span style="color:#b8860b;">=</span> <span style="color:#228b22;">[]</span>
<span style="color:#0000ff;">replace</span> from to s<span style="color:#b8860b;">@</span>(c<span style="color:#228b22;">:</span>cs)
    <span style="color:#b8860b;">|</span> isPrefixOf from s <span style="color:#b8860b;">=</span> to <span style="color:#b8860b;">++</span> (drop (length from) s)
    <span style="color:#b8860b;">|</span> otherwise <span style="color:#b8860b;">=</span> c <span style="color:#228b22;">:</span> (replace from to cs)

<span style="color:#b22222;">-- </span><span style="color:#b22222;">The execution function
</span><span style="color:#0000ff;">exec</span> <span style="color:#b8860b;">::</span> [<span style="color:#228b22;">StepParams</span>] <span style="color:#b8860b;">-&gt;</span> <span style="color:#228b22;">String</span> <span style="color:#b8860b;">-&gt;</span> <span style="color:#228b22;">String</span>
<span style="color:#0000ff;">exec</span> ps input <span style="color:#b8860b;">=</span> execLoop (input, 0)
    <span style="color:#a020f0;">where</span> execLoop (word, step)
              <span style="color:#b8860b;">|</span> step <span style="color:#b8860b;">==</span> length ps <span style="color:#b8860b;">=</span> word
              <span style="color:#b8860b;">|</span> otherwise         <span style="color:#b8860b;">=</span> execLoop <span style="color:#b8860b;">$</span> doStep (ps<span style="color:#b8860b;">!!</span>step) word
          doStep (theta, phi, b, a) word
              <span style="color:#b8860b;">|</span> isInfixOf theta word <span style="color:#b8860b;">=</span> (replace theta phi word, b)
              <span style="color:#b8860b;">|</span> otherwise            <span style="color:#b8860b;">=</span> (word, a)
</pre>
<p>Here are some algorithms. This calculates the absolute difference between numbers m and n, represented as strings with m &#8216;a&#8217;s and n &#8216;b&#8217;s, as suggested in the text. The output is formed with &#8216;c&#8217;s, the number of which will be the absolute value of m &#8211; n. </p>
<pre style="font-size:medium;">
<font color="#0000ff">absDiff</font> <font color="#b8860b">::</font> <font color="#228b22">Algorithm</font>
<font color="#0000ff">absDiff</font> <font color="#b8860b">=</font> [(<font color="#bc8f8f">"ab"</font>, <font color="#bc8f8f">""</font> , 0, 1),
           (<font color="#bc8f8f">"a"</font> , <font color="#bc8f8f">"c"</font>, 1, 2),
           (<font color="#bc8f8f">"b"</font> , <font color="#bc8f8f">"c"</font>, 2, 3)]
</pre>
<p>We can execute this algorithm for some sample input with</p>
<pre style="font-size:medium;">
exec absDiff "aaaabb"
</pre>
<p>Which should print &#8220;cc&#8221;.</p>
<p>And here is the algorithm for calculating the greatest common divisor of m and n, using the same convention for the input. The output will consist of &#8216;a&#8217;s. This is the answer to exercise 8 of Section 1.1.</p>
<pre style="font-size:medium;">
<font color="#0000ff">mGcd</font> <font color="#b8860b">::</font> <font color="#228b22">Algorithm</font>
<font color="#0000ff">mGcd</font> <font color="#b8860b">=</font> [(<font color="#bc8f8f">"ab"</font>, <font color="#bc8f8f">""</font> , 1, 2),      <font color="#b22222">-- </font><font color="#b22222">0
</font>        (<font color="#bc8f8f">""</font>  , <font color="#bc8f8f">"c"</font>, 0, 2),      <font color="#b22222">-- </font><font color="#b22222">1
</font>        (<font color="#bc8f8f">"a"</font> , <font color="#bc8f8f">"b"</font>, 2, 3),      <font color="#b22222">-- </font><font color="#b22222">2
</font>        (<font color="#bc8f8f">"c"</font> , <font color="#bc8f8f">"a"</font>, 3, 4),      <font color="#b22222">-- </font><font color="#b22222">3
</font>        (<font color="#bc8f8f">"b"</font> , <font color="#bc8f8f">"b"</font>, 0, 5)]      <font color="#b22222">-- </font><font color="#b22222">4
</font></pre>
<p>The name is mGcd (for Markov gcd) to avoid clashes with the Prelude&#8217;s gcd. Note also that in step 1 the string theta (which is empty) should always match, so the value of a is irrelevant. I put 2 there, Knuth used 0 in the answer. Now let&#8217;s define some auxiliary functions to help us actually use this with some significant numbers: </p>
<pre style="font-size:medium;">
<font color="#0000ff">buildStr</font> m n <font color="#b8860b">=</font> (replicate m <font color="#bc8f8f">'a'</font>) <font color="#b8860b">++</font> (replicate n <font color="#bc8f8f">'b'</font>)

<font color="#0000ff">interpretResult</font> <font color="#b8860b">=</font> length

<font color="#0000ff">markovGCD</font> m n <font color="#b8860b">=</font> interpretResult <font color="#b8860b">$</font> exec mGcd (buildString m n)
</pre>
<p>And now we can use it:</p>
<pre style="font-size:medium;">
*Main&gt; markovGCD 2166 6099
57
</pre>
<p>It takes quite a few seconds, but outputs the correct answer to exercise 4 of Section 1.1. </p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/readingtaocp.wordpress.com/16/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/readingtaocp.wordpress.com/16/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/readingtaocp.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/readingtaocp.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/readingtaocp.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/readingtaocp.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/readingtaocp.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/readingtaocp.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/readingtaocp.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/readingtaocp.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/readingtaocp.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/readingtaocp.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/readingtaocp.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/readingtaocp.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/readingtaocp.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/readingtaocp.wordpress.com/16/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=readingtaocp.wordpress.com&amp;blog=4612758&amp;post=16&amp;subd=readingtaocp&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://readingtaocp.wordpress.com/2008/09/13/markov-algorithms-in-haskell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/91fa687725c763561519614037bb31a9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tautologico</media:title>
		</media:content>
	</item>
		<item>
		<title>Sec. 1.1: Algorithms</title>
		<link>http://readingtaocp.wordpress.com/2008/09/11/sec-11-algorithms/</link>
		<comments>http://readingtaocp.wordpress.com/2008/09/11/sec-11-algorithms/#comments</comments>
		<pubDate>Fri, 12 Sep 2008 01:48:24 +0000</pubDate>
		<dc:creator>Andrei Formiga</dc:creator>
				<category><![CDATA[vol1]]></category>

		<guid isPermaLink="false">http://readingtaocp.wordpress.com/?p=7</guid>
		<description><![CDATA[Section 1.1, Algorithms, is an introduction to the ideas of the book. It&#8217;s interesting to see how Knuth wrote the book in a time when computers were not widespread, and he thought about a target audience of people with little programming experience. He even explains what a variable assignment is. Nowadays no one would recomend [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=readingtaocp.wordpress.com&amp;blog=4612758&amp;post=7&amp;subd=readingtaocp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Section 1.1, Algorithms, is an introduction to the ideas of the book. It&#8217;s interesting to see how Knuth wrote the book in a time when computers were not widespread, and he thought about a target audience of people with little programming experience. He even explains what a variable assignment is. Nowadays no one would recomend TAoCP for beginners, so very few people will read this section without knowing how assignment works, or how to read an algorithm. </p>
<p>By the end of the section Knuth shows briefly how to formalize the notion of algorithm mathematically, employing Markov algorithms. This was interesting, because although I had seen Markov algorithms before (in <a title="Haskell Curry" href="http://en.wikipedia.org/wiki/Haskell_Curry">Curry</a>&#8216;s Fountations of Mathematical Logic), I didn&#8217;t really play with them. I ended up writing programs in <a title="Haskell" href="http://haskell.org/">Haskell</a> and <a title="Scala" href="http://www.scala-lang.org/">Scala</a> to simulate an algorithm expressed with the notation as presented by Knuth, so I could check if I was getting it right. Later I&#8217;ll be posting the code here; first let&#8217;s see if I can get syntax highlighting working for these two languages.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/readingtaocp.wordpress.com/7/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/readingtaocp.wordpress.com/7/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/readingtaocp.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/readingtaocp.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/readingtaocp.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/readingtaocp.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/readingtaocp.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/readingtaocp.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/readingtaocp.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/readingtaocp.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/readingtaocp.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/readingtaocp.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/readingtaocp.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/readingtaocp.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/readingtaocp.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/readingtaocp.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=readingtaocp.wordpress.com&amp;blog=4612758&amp;post=7&amp;subd=readingtaocp&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://readingtaocp.wordpress.com/2008/09/11/sec-11-algorithms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/91fa687725c763561519614037bb31a9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tautologico</media:title>
		</media:content>
	</item>
		<item>
		<title>The Art of Computer Programming</title>
		<link>http://readingtaocp.wordpress.com/2008/08/25/the-art-of-computer-programming/</link>
		<comments>http://readingtaocp.wordpress.com/2008/08/25/the-art-of-computer-programming/#comments</comments>
		<pubDate>Mon, 25 Aug 2008 16:47:18 +0000</pubDate>
		<dc:creator>Andrei Formiga</dc:creator>
				<category><![CDATA[meta]]></category>
		<category><![CDATA[knuth]]></category>

		<guid isPermaLink="false">http://readingtaocp.wordpress.com/?p=3</guid>
		<description><![CDATA[I&#8217;m reading Knuth&#8216;s masterpiece The Art of Computer Programming for real, this time. I&#8217;ll log some notes about my efforts here. Due to Real Life obligations, it will have to proceed in a quiet fashion, and many moons shall pass between posts. Such is life. Also, although Knuth recommends reading the books in order, I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=readingtaocp.wordpress.com&amp;blog=4612758&amp;post=3&amp;subd=readingtaocp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m reading <a title="Don Knuth's Home Page" href="http://www-cs-faculty.stanford.edu/~knuth/">Knuth</a>&#8216;s masterpiece <a title="The Art of Computer Programming" href="http://www-cs-faculty.stanford.edu/~knuth/taocp.html">The Art of Computer Programming</a> for real, this time. I&#8217;ll log some notes about my efforts here. Due to Real Life obligations, it will have to proceed in a quiet fashion, and many moons shall pass between posts. Such is life.</p>
<p>Also, although Knuth recommends reading the books in order, I will probably read some parts concurrently. For example, right now I will skim through Chapter 1 (which I read many years ago, even doing many exercises) and begin studying Chapter 3 seriously, because random number generation interests me. So be it.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/readingtaocp.wordpress.com/3/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/readingtaocp.wordpress.com/3/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/readingtaocp.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/readingtaocp.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/readingtaocp.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/readingtaocp.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/readingtaocp.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/readingtaocp.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/readingtaocp.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/readingtaocp.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/readingtaocp.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/readingtaocp.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/readingtaocp.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/readingtaocp.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/readingtaocp.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/readingtaocp.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=readingtaocp.wordpress.com&amp;blog=4612758&amp;post=3&amp;subd=readingtaocp&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://readingtaocp.wordpress.com/2008/08/25/the-art-of-computer-programming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/91fa687725c763561519614037bb31a9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tautologico</media:title>
		</media:content>
	</item>
	</channel>
</rss>
