<?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>Code and Bugs</title>
	<atom:link href="http://pgraycode.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://pgraycode.wordpress.com</link>
	<description>Coding things</description>
	<lastBuildDate>Wed, 18 Jan 2012 06:28:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='pgraycode.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Code and Bugs</title>
		<link>http://pgraycode.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://pgraycode.wordpress.com/osd.xml" title="Code and Bugs" />
	<atom:link rel='hub' href='http://pgraycode.wordpress.com/?pushpress=hub'/>
		<item>
		<title>SOPA</title>
		<link>http://pgraycode.wordpress.com/2012/01/18/sopa/</link>
		<comments>http://pgraycode.wordpress.com/2012/01/18/sopa/#comments</comments>
		<pubDate>Wed, 18 Jan 2012 06:28:27 +0000</pubDate>
		<dc:creator>Mithrandir</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pgraycode.wordpress.com/?p=1086</guid>
		<description><![CDATA[Is this what you want? Think again please.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pgraycode.wordpress.com&amp;blog=6568508&amp;post=1086&amp;subd=pgraycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Is this what you want?</p>
<p><img class="aligncenter" title="SOPA's effects on Wikipedia" src="http://s19.postimage.org/7ru1kwzdf/405683_349024301794195_100000598835379_1282347_6.jpg" alt="" width="480" /></p>
<p>Think again please.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pgraycode.wordpress.com/1086/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pgraycode.wordpress.com/1086/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pgraycode.wordpress.com/1086/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pgraycode.wordpress.com/1086/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pgraycode.wordpress.com/1086/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pgraycode.wordpress.com/1086/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pgraycode.wordpress.com/1086/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pgraycode.wordpress.com/1086/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pgraycode.wordpress.com/1086/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pgraycode.wordpress.com/1086/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pgraycode.wordpress.com/1086/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pgraycode.wordpress.com/1086/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pgraycode.wordpress.com/1086/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pgraycode.wordpress.com/1086/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pgraycode.wordpress.com&amp;blog=6568508&amp;post=1086&amp;subd=pgraycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pgraycode.wordpress.com/2012/01/18/sopa/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9a378ed2410bdabed82883f2615f6b61?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Mithrandir</media:title>
		</media:content>

		<media:content url="http://s19.postimage.org/7ru1kwzdf/405683_349024301794195_100000598835379_1282347_6.jpg" medium="image">
			<media:title type="html">SOPA&#039;s effects on Wikipedia</media:title>
		</media:content>
	</item>
		<item>
		<title>Benchmarks</title>
		<link>http://pgraycode.wordpress.com/2011/07/29/benchmarks/</link>
		<comments>http://pgraycode.wordpress.com/2011/07/29/benchmarks/#comments</comments>
		<pubDate>Fri, 29 Jul 2011 21:23:02 +0000</pubDate>
		<dc:creator>Mithrandir</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pgraycode.wordpress.com/?p=1082</guid>
		<description><![CDATA[Recently I discovered the joy of answering questions on Stack Overflow. I had a SO account for two years but I didn&#8217;t use it until a few weeks ago. This article started after receiving a little feedback on one of my answers. As you see, I was trying to give a benchmark for those two [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pgraycode.wordpress.com&amp;blog=6568508&amp;post=1082&amp;subd=pgraycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Recently I discovered the joy of answering questions on Stack Overflow. I had a SO account for two years but I didn&#8217;t use it until a few weeks ago. This article started after receiving a little feedback on one of my <a href="http://stackoverflow.com/questions/6799102/efficient-way-to-divide-ignoring-rest/6799163#6799163">answers</a>.</p>
<p style="text-align:justify;">As you see, I was trying to give a benchmark for those two cases. Yet, it was reported as being an ugly hack and not a try at comparing two solutions. I post this short article here not because I wanted a place to cry that I was criticized but because I desired some advice on what should a good benchmark contain and how to write a proper one.</p>
<p style="text-align:justify;">Thanks :)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pgraycode.wordpress.com/1082/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pgraycode.wordpress.com/1082/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pgraycode.wordpress.com/1082/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pgraycode.wordpress.com/1082/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pgraycode.wordpress.com/1082/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pgraycode.wordpress.com/1082/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pgraycode.wordpress.com/1082/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pgraycode.wordpress.com/1082/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pgraycode.wordpress.com/1082/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pgraycode.wordpress.com/1082/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pgraycode.wordpress.com/1082/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pgraycode.wordpress.com/1082/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pgraycode.wordpress.com/1082/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pgraycode.wordpress.com/1082/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pgraycode.wordpress.com&amp;blog=6568508&amp;post=1082&amp;subd=pgraycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pgraycode.wordpress.com/2011/07/29/benchmarks/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9a378ed2410bdabed82883f2615f6b61?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Mithrandir</media:title>
		</media:content>
	</item>
		<item>
		<title>Mazes (1)</title>
		<link>http://pgraycode.wordpress.com/2011/06/06/mazes-1/</link>
		<comments>http://pgraycode.wordpress.com/2011/06/06/mazes-1/#comments</comments>
		<pubDate>Mon, 06 Jun 2011 14:09:40 +0000</pubDate>
		<dc:creator>Mithrandir</dc:creator>
				<category><![CDATA[Ideas]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Puzzles]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Maze]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://pgraycode.wordpress.com/?p=1057</guid>
		<description><![CDATA[As mentioned previously, I had an assignment which involved constructing a maze from code and letting a robot escape from it. This series won&#8217;t follow the second part, it will concentrate on maze specific topics. I will write about maze generation, maze drawing, maze counting and several other aspects. Maybe I&#8217;ll touch several properties of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pgraycode.wordpress.com&amp;blog=6568508&amp;post=1057&amp;subd=pgraycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">As mentioned <a href="http://pgraycode.wordpress.com/2011/05/19/machine-learning/">previously</a>, I had an assignment which involved constructing a maze from code and letting a robot escape from it. This series won&#8217;t follow the second part, it will concentrate on maze specific topics. I will write about maze generation, maze drawing, maze counting and several other aspects. Maybe I&#8217;ll touch several properties of the mazes, possibly I&#8217;ll write about something like the moments in statistics. Will see where this series will end and what areas will be covered by it.</p>
<p style="text-align:justify;"><span id="more-1057"></span></p>
<p style="text-align:justify;">This post will speak about maze construction. After a few searches for good and valid algorithms, I arrived at <a href="http://weblog.jamisbuck.org">Jamis</a>&#8216; blog. From there, after reading the <a href="http://weblog.jamisbuck.org/2011/2/7/maze-generation-algorithm-recap">exhaustive listing</a> of generation procedures, I selected one algorithm to implement in Haskell and to present here. I choose the <a href="http://weblog.jamisbuck.org/2011/2/3/maze-generation-sidewinder-algorithm">Sidewinder</a> both because it has a nice name and because it seemed easy to implement in Haskell (requiring little state information and only a few random numbers). It runs very quickly and can be generated on the fly if needed.</p>
<p style="text-align:justify;">It has two disadvantages though: the top row is free of walls and it can be easily solved. If starting on the lower row simple walk until you find an opening to the north and go in that direction. Repeat until you finish. If starting on the top row, you&#8217;ll have to do a little backtracking but usually not a time-consuming one. Also, it doesn&#8217;t produce all of the possible mazes. Solutions to all these problems will come in following articles, hopefully.</p>
<p style="text-align:justify;">Now, for the generation part, I tweaked the algorithm a little. Instead of deciding at each cell whether to carve east or not, I started by deciding how many cells to carve east before doing any work. This will be clearer soon, after seeing the code.</p>
<p style="text-align:justify;">First, some type definitions used to make the code look cleaner</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;">
<span style="color:#010181;">type</span> Length <span style="color:#000000;">=</span> <span style="color:#830000;">Int</span>
<span style="color:#010181;">type</span> Size <span style="color:#000000;">= (</span>Length<span style="color:#000000;">,</span> Length<span style="color:#000000;">)</span>
<span style="color:#010181;">type</span> Point <span style="color:#000000;">=</span> Size

<span style="color:#838183;font-style:italic;">{- The cardinal directions. -}</span>
<span style="color:#010181;">data</span> Cardinal <span style="color:#000000;">=</span> N <span style="color:#000000;">|</span> E <span style="color:#000000;">|</span> S <span style="color:#000000;">|</span> W <span style="color:#010181;">deriving</span> <span style="color:#000000;">(</span>Eq<span style="color:#000000;">,</span> <span style="color:#830000;">Show</span><span style="color:#000000;">,</span> <span style="color:#830000;">Read</span><span style="color:#000000;">,</span> Ord<span style="color:#000000;">,</span> Enum<span style="color:#000000;">)</span> 

<span style="color:#838183;font-style:italic;">{- A cell. The list contains the openings. -}</span>
<span style="color:#010181;">newtype</span> Cell <span style="color:#000000;">=</span> C <span style="color:#000000;">[</span>Cardinal<span style="color:#000000;">]</span> <span style="color:#010181;">deriving</span> <span style="color:#000000;">(</span><span style="color:#830000;">Show</span><span style="color:#000000;">,</span> <span style="color:#830000;">Read</span><span style="color:#000000;">)</span> 

<span style="color:#838183;font-style:italic;">{- Simple type for maze. -}</span>
<span style="color:#010181;">type</span> Maze <span style="color:#000000;">=</span> <span style="color:#830000;">Array</span> Size Cell
</pre>
</p>
<p style="text-align:justify;">We have to generate a maze, we will use a function which receives the size of the maze as input and outputs the maze. Since we need randomness, we will be working in the State monad working with a random generator as the state (that&#8217;s the <code>State StdGen</code> thing in the signature).</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;">
genMaze <span style="color:#000000;">::</span> Size <span style="color:#000000;">-&gt;</span> State <span style="color:#830000;">StdGen</span> Maze
</pre>
</p>
<p style="text-align:justify;">Basically, the generation is split into two phases: one in which we generate the important points in the maze (ends for working sets and upwards openings) and one in which the actual building is done.</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;">
genMaze s<span style="color:#000000;">&#064;(</span>sx<span style="color:#000000;">,</span> sy<span style="color:#000000;">) =</span> <span style="color:#010181;">do</span>
  <span style="color:#000000;">(</span>ews<span style="color:#000000;">,</span> ups<span style="color:#000000;">) &lt;-</span> gMP s
  <span style="color:#000000;font-weight:bold;">return</span> <span style="color:#000000;">$</span> build sx sy ews ups
</pre>
</p>
<p style="text-align:justify;">The building process is very simple, it seems that it can be generalized to any other maze generation algorithm with only a few changes. However, this is not true, to use this process you&#8217;ll have to obtain a list of important points and build the maze from it. It won&#8217;t even work for on-the-fly generation. But this wasn&#8217;t the point at that time and is not really relevant now.</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;">
build <span style="color:#000000;">::</span> Length <span style="color:#000000;">-&gt;</span> Length <span style="color:#000000;">-&gt; [</span>Point<span style="color:#000000;">] -&gt; [</span>Point<span style="color:#000000;">] -&gt;</span> Maze
</pre>
</p>
<p style="text-align:justify;">We will construct an array of cells, each of them opened to the East and West first.</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;">
build sx sy ews ups <span style="color:#000000;">=</span> runSTArray <span style="color:#000000;">$</span> <span style="color:#010181;">do</span>
  m <span style="color:#000000;">&lt;-</span> newListArray <span style="color:#000000;">((</span><span style="color:#2928ff;">1</span><span style="color:#000000;">,</span> <span style="color:#2928ff;">1</span><span style="color:#000000;">), (</span>sy<span style="color:#000000;">,</span> sx<span style="color:#000000;">)) $</span> <span style="color:#000000;font-weight:bold;">repeat</span> <span style="color:#000000;">$</span> C <span style="color:#000000;">[</span>E<span style="color:#000000;">,</span> W<span style="color:#000000;">]</span>
</pre>
</p>
<p style="text-align:center;"><a href='http://postimage.org/image/4bkitlwk/' target='_blank'><img src='http://s2.postimage.org/4bkitlwk/00_maze.jpg' border='0' alt="Only north and south walls" /></a><br />
Only north and south walls</p>
<p style="text-align:justify;">Then, using the first set of important points, the eastern openings of some cells will be closed. Using the same set, we will also close some of the western openings such that if a cell claims to be closed eastward the next cell will be closed westward.</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;">
  <span style="color:#000000;font-weight:bold;">mapM_</span> <span style="color:#000000;">(</span>blockCell m E<span style="color:#000000;">) $ (</span>sx<span style="color:#000000;">,</span> <span style="color:#2928ff;">1</span><span style="color:#000000;">) :</span> ews
  <span style="color:#000000;font-weight:bold;">mapM_</span> <span style="color:#000000;">(</span>blockCell m W <span style="color:#000000;">.</span> first <span style="color:#000000;">(+</span><span style="color:#2928ff;">1</span><span style="color:#000000;">)) $</span> <span style="color:#000000;font-weight:bold;">filter</span> <span style="color:#000000;">(</span><span style="color:#000000;font-weight:bold;">fst</span> <span style="color:#000000;">.</span> first <span style="color:#000000;">(/=</span> sx<span style="color:#000000;">))</span> ews
</pre>
</p>
<p style="text-align:center;"><a href='http://postimage.org/image/yf27tx9g/' target='_blank'><img src='http://s4.postimage.org/yf27tx9g/01_maze.jpg' border='0' alt="East and West closed" /></a><br />
East and West closed</p>
<p style="text-align:justify;">After blocking the western edges of each row we will have a set of rows, like in the following picture</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;">
  <span style="color:#000000;font-weight:bold;">mapM_</span> <span style="color:#000000;">(</span>blockCell m W <span style="color:#000000;">. (\</span>y <span style="color:#000000;">-&gt; (</span><span style="color:#2928ff;">1</span><span style="color:#000000;">,</span> y<span style="color:#000000;">))) [</span><span style="color:#2928ff;">1</span> <span style="color:#000000;">..</span> sy<span style="color:#000000;">]</span>
</pre>
</p>
<p style="text-align:center;"><a href='http://postimage.org/image/yf3vd9r8/' target='_blank'><img src='http://s4.postimage.org/yf3vd9r8/02_maze.jpg' border='0' alt="Rows fully closed" /></a><br />
Rows fully closed</p>
<p style="text-align:justify;">We&#8217;ll have to open some cells northwards and some southwards, just like we did with the East-West relationship. Then, we will have our maze.</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;">
  <span style="color:#000000;font-weight:bold;">mapM_</span> <span style="color:#000000;">(</span>openCell m N<span style="color:#000000;">)</span> ups
  <span style="color:#000000;font-weight:bold;">mapM_</span> <span style="color:#000000;">(</span>openCell m S <span style="color:#000000;">.</span> second <span style="color:#000000;">(</span><span style="color:#000000;font-weight:bold;">subtract</span> <span style="color:#2928ff;">1</span><span style="color:#000000;">))</span> ups
  <span style="color:#000000;font-weight:bold;">return</span> m
</pre>
</p>
<p style="text-align:center;"><a href='http://postimage.org/image/yf76fyqs/' target='_blank'><img src='http://s4.postimage.org/yf76fyqs/03_maze.jpg' border='0' alt="Full maze" /></a><br />
Full maze</p>
<p style="text-align:justify;">Opening and closing cells is in fact modifying the contents of the array.</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;">
<span style="color:#838183;font-style:italic;">{-</span>
<span style="color:#838183;font-style:italic;">Block one cell from the maze, represented as an array.</span>
<span style="color:#838183;font-style:italic;">-}</span>
<span style="color:#838183;font-style:italic;">-- blockCell :: Data.Array.MArray Size Cell -&gt; Cardinal -&gt; Size -&gt; m ()</span>
blockCell m d <span style="color:#000000;">(</span>x<span style="color:#000000;">,</span> y<span style="color:#000000;">) =</span> <span style="color:#010181;">do</span>
  e <span style="color:#000000;">&lt;-</span> readArray m <span style="color:#000000;">(</span>y<span style="color:#000000;">,</span> x<span style="color:#000000;">)</span>
  writeArray m <span style="color:#000000;">(</span>y<span style="color:#000000;">,</span> x<span style="color:#000000;">) $</span> block e d

<span style="color:#838183;font-style:italic;">{-</span>
<span style="color:#838183;font-style:italic;">Open one cell from the maze, represented as an array.</span>
<span style="color:#838183;font-style:italic;">-}</span>
<span style="color:#838183;font-style:italic;">-- openCell :: (MArray a Cell m) =&gt; a Size Cell -&gt; Cardinal -&gt; Size -&gt; m ()</span>
openCell m d <span style="color:#000000;">(</span>x<span style="color:#000000;">,</span> y<span style="color:#000000;">) =</span> <span style="color:#010181;">do</span>
  e <span style="color:#000000;">&lt;-</span> readArray m <span style="color:#000000;">(</span>y<span style="color:#000000;">,</span> x<span style="color:#000000;">)</span>
  writeArray m <span style="color:#000000;">(</span>y<span style="color:#000000;">,</span> x<span style="color:#000000;">) $</span> open e d

<span style="color:#838183;font-style:italic;">{- Block a cell from one direction. -}</span>
block <span style="color:#000000;">::</span> Cell <span style="color:#000000;">-&gt;</span> Cardinal <span style="color:#000000;">-&gt;</span> Cell
block <span style="color:#000000;">(</span>C l<span style="color:#000000;">)</span> x <span style="color:#000000;">=</span> C <span style="color:#000000;">$</span> <span style="color:#000000;font-weight:bold;">filter</span> <span style="color:#000000;">(/=</span> x<span style="color:#000000;">)</span> l

<span style="color:#838183;font-style:italic;">{- Open a cell to one direction. -}</span>
open <span style="color:#000000;">::</span> Cell <span style="color:#000000;">-&gt;</span> Cardinal <span style="color:#000000;">-&gt;</span> Cell
open <span style="color:#000000;">(</span>C l<span style="color:#000000;">)</span> x <span style="color:#000000;">=</span> C <span style="color:#000000;">$</span> <span style="color:#010181;">if</span> x `<span style="color:#000000;font-weight:bold;">elem</span>` l <span style="color:#010181;">then</span> l <span style="color:#010181;">else</span> x <span style="color:#000000;">:</span> l
</pre>
</p>
<p style="text-align:justify;">The only remaining part is the generation of the important points, as given by the Sidewinder&#8217;s algorithm. Since the first row is special (no walls blocking eastward) we will skip it when generating the points. Only the other rows will be used to generate the important Row Points. After this, do a simple fold to construct the Maze Points used in the above build procedure.</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;">
gMP <span style="color:#000000;">::</span> Size <span style="color:#000000;">-&gt;</span> State <span style="color:#830000;">StdGen</span> <span style="color:#000000;">([</span>Point<span style="color:#000000;">], [</span>Point<span style="color:#000000;">])</span>
gMP <span style="color:#000000;">(</span>sx<span style="color:#000000;">,</span> sy<span style="color:#000000;">) =</span> <span style="color:#010181;">do</span>
  points <span style="color:#000000;">&lt;-</span> <span style="color:#000000;font-weight:bold;">mapM</span> <span style="color:#000000;">(</span>gRP <span style="color:#2928ff;">0</span> sx<span style="color:#000000;">) [</span><span style="color:#2928ff;">2</span><span style="color:#000000;">..</span>sy<span style="color:#000000;">]</span>
  <span style="color:#000000;font-weight:bold;">return</span> <span style="color:#000000;">$</span> <span style="color:#000000;font-weight:bold;">foldl</span> <span style="color:#000000;">(\(</span>x<span style="color:#000000;">,</span> y<span style="color:#000000;">) (</span>a<span style="color:#000000;">,</span> b<span style="color:#000000;">) -&gt; (</span>x <span style="color:#000000;">++</span> a<span style="color:#000000;">,</span> y <span style="color:#000000;">++</span> b<span style="color:#000000;">)) ([], [])</span> points
</pre>
</p>
<p style="text-align:justify;">Generating the RowPoints is simple: generate two random numbers and return the points obtained from them. More exactly, construct a list of them. The numbers will represent the length of the current corridor and the offset at which the opening to the previous row will be carved.</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;">
gRP <span style="color:#000000;">::</span> Coord <span style="color:#000000;">-&gt;</span> Length <span style="color:#000000;">-&gt;</span> Coord <span style="color:#000000;">-&gt;</span> State <span style="color:#830000;">StdGen</span> <span style="color:#000000;">([</span>Point<span style="color:#000000;">], [</span>Point<span style="color:#000000;">])</span>
gRP c sx y
  <span style="color:#000000;">|</span> sx <span style="color:#000000;">&lt;=</span> <span style="color:#2928ff;">0</span> <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">return</span> <span style="color:#000000;">([], [])</span>
  <span style="color:#000000;">|</span> <span style="color:#000000;font-weight:bold;">otherwise</span> <span style="color:#000000;">=</span> <span style="color:#010181;">do</span>
    len <span style="color:#000000;">&lt;-</span> state <span style="color:#000000;">$</span> <span style="color:#000000;font-weight:bold;">randomR</span> <span style="color:#000000;">(</span><span style="color:#2928ff;">1</span><span style="color:#000000;">,</span> sx<span style="color:#000000;">)</span>
    up <span style="color:#000000;">&lt;-</span> state <span style="color:#000000;">$</span> <span style="color:#000000;font-weight:bold;">randomR</span> <span style="color:#000000;">(</span><span style="color:#2928ff;">1</span><span style="color:#000000;">,</span> len<span style="color:#000000;">)</span>
    <span style="color:#000000;">(</span>rx<span style="color:#000000;">,</span> ry<span style="color:#000000;">) &lt;-</span> gRP <span style="color:#000000;">(</span>c <span style="color:#000000;">+</span> len<span style="color:#000000;">) (</span>sx <span style="color:#000000;">-</span> len<span style="color:#000000;">)</span> y
    <span style="color:#000000;font-weight:bold;">return</span> <span style="color:#000000;">((</span>len <span style="color:#000000;">+</span> c<span style="color:#000000;">,</span> y<span style="color:#000000;">):</span>rx<span style="color:#000000;">, (</span>up <span style="color:#000000;">+</span> c<span style="color:#000000;">,</span> y<span style="color:#000000;">):</span>ry<span style="color:#000000;">)</span>
</pre>
</p>
<p style="text-align:justify;">Basically, the above function is the only one which needs random numbers. However, since we want to be able to change the generation if another, more efficient, algorithm is found, the only exported function should be <code>genMaze</code>.</p>
<p style="text-align:justify;">The entire code can be seen on <a href="https://github.com/mihaimaruseac/HsMaze/blob/master/Maze/Maze.hs">Github</a>.</p>
<p style="text-align:justify;">Next time we will look at how to do the actual rendering of this maze in Haskell (the above pictures were generated using a modified version of what will be presented then). Afterwards, we&#8217;ll touch some more interesting topics related to mazes but I don&#8217;t want to spoil anything right now.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pgraycode.wordpress.com/1057/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pgraycode.wordpress.com/1057/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pgraycode.wordpress.com/1057/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pgraycode.wordpress.com/1057/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pgraycode.wordpress.com/1057/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pgraycode.wordpress.com/1057/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pgraycode.wordpress.com/1057/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pgraycode.wordpress.com/1057/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pgraycode.wordpress.com/1057/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pgraycode.wordpress.com/1057/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pgraycode.wordpress.com/1057/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pgraycode.wordpress.com/1057/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pgraycode.wordpress.com/1057/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pgraycode.wordpress.com/1057/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pgraycode.wordpress.com&amp;blog=6568508&amp;post=1057&amp;subd=pgraycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pgraycode.wordpress.com/2011/06/06/mazes-1/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9a378ed2410bdabed82883f2615f6b61?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Mithrandir</media:title>
		</media:content>

		<media:content url="http://s2.postimage.org/4bkitlwk/00_maze.jpg" medium="image">
			<media:title type="html">Only north and south walls</media:title>
		</media:content>

		<media:content url="http://s4.postimage.org/yf27tx9g/01_maze.jpg" medium="image">
			<media:title type="html">East and West closed</media:title>
		</media:content>

		<media:content url="http://s4.postimage.org/yf3vd9r8/02_maze.jpg" medium="image">
			<media:title type="html">Rows fully closed</media:title>
		</media:content>

		<media:content url="http://s4.postimage.org/yf76fyqs/03_maze.jpg" medium="image">
			<media:title type="html">Full maze</media:title>
		</media:content>
	</item>
		<item>
		<title>GTK Custom Drawing and Saving</title>
		<link>http://pgraycode.wordpress.com/2011/05/26/gtk-custom-drawing-and-saving/</link>
		<comments>http://pgraycode.wordpress.com/2011/05/26/gtk-custom-drawing-and-saving/#comments</comments>
		<pubDate>Thu, 26 May 2011 10:55:47 +0000</pubDate>
		<dc:creator>Mithrandir</dc:creator>
				<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Questions of a Beginner]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://pgraycode.wordpress.com/?p=1049</guid>
		<description><![CDATA[In one of the previous mentioned homework, I had to draw an image of a neural network on a custom widget and later save said image to file. This post was inspired by this task. The assignment was done in Python with PyGTK. Looking at the documentation for doing custom draws, I saw that one [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pgraycode.wordpress.com&amp;blog=6568508&amp;post=1049&amp;subd=pgraycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">In one of the <a href="http://pgraycode.wordpress.com/2011/05/19/machine-learning/">previous mentioned</a> homework, I had to draw an image of a neural network on a custom widget and later save said image to file. This post was inspired by this task.</p>
<p style="text-align:justify;"><span id="more-1049"></span></p>
<p style="text-align:justify;">The <a href="https://github.com/mihaimaruseac/Backpropagation">assignment</a> was done in Python with PyGTK. Looking at the documentation for doing custom draws, I saw that one has to use a Pixmap obtained by using an Image object and then use methods associated with the drawing context obtained from it. This works, is easy to reason about but has a small problem.</p>
<p style="text-align:justify;">When you also want to save the image to a file you cannot use the same Pixmap. That is because GTK can only save Pixbuff content, not Pixmaps. And you cannot easily convert between one structure and the other. In fact, you cannot even use Image methods to obtain what you need: Pixbuff or Pixmap. That is because a Image has a backup structure and that backup structure can be a Pixbuff or a Pixmap (or several some others, as taken from PyGTK documentation).</p>
<p style="text-align:justify;">After many failed attempts to do this, I&#8217;ve come across the following convoluted solution. It was found after long search through the maze of PyGTK documentation (even dropping a little to the level of GTK documentation and trying a cross-language translation) but it worked at least.</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;">
cmap <span style="color:#000000;">=</span> drawable<span style="color:#000000;">.</span><span style="color:#010181;">get_colormap</span><span style="color:#000000;">()</span>
pbuf <span style="color:#000000;">=</span> gtk<span style="color:#000000;">.</span>gdk<span style="color:#000000;">.</span><span style="color:#010181;">Pixbuf</span><span style="color:#000000;">(</span>gtk<span style="color:#000000;">.</span>gdk<span style="color:#000000;">.</span>COLORSPACE_RGB<span style="color:#000000;">,</span> <span style="color:#000000;font-weight:bold;">True</span><span style="color:#000000;">,</span> <span style="color:#2928ff;">8</span><span style="color:#000000;">,</span> <span style="color:#000000;">*</span>drawable<span style="color:#000000;">.</span><span style="color:#010181;">get_size</span><span style="color:#000000;">())</span>
pbuf <span style="color:#000000;">=</span> pbuf<span style="color:#000000;">.</span><span style="color:#010181;">get_from_drawable</span><span style="color:#000000;">(</span>drawable<span style="color:#000000;">,</span> cmap<span style="color:#000000;">,</span> <span style="color:#2928ff;">0</span><span style="color:#000000;">,</span> <span style="color:#2928ff;">0</span><span style="color:#000000;">,</span> <span style="color:#2928ff;">0</span><span style="color:#000000;">,</span> <span style="color:#2928ff;">0</span><span style="color:#000000;">,</span> <span style="color:#000000;">*</span>drawable<span style="color:#000000;">.</span><span style="color:#010181;">get_size</span><span style="color:#000000;">())</span>
pbuf<span style="color:#000000;">.</span><span style="color:#010181;">save</span><span style="color:#000000;">(</span>fName<span style="color:#000000;">,</span> <span style="color:#ff0000;">'png'</span><span style="color:#000000;">)</span>
</pre>
</p>
<p style="text-align:justify;">After this project was finished, I&#8217;ve moved to the <a href="https://github.com/mihaimaruseac/HsMaze">next</a> which was done in Haskell. There I also had to do a custom drawing but, this time, I&#8217;ve turned to using Cairo. Much simpler and easier to reason about. And much simpler to save the images to files if needed.</p>
<p style="text-align:justify;">Unfortunately, the PyGTK documentation seems to fail to mention this or at least give a hint to use Cairo instead of native GTK methods. But this can be fixed since we know that this project is open source.</p>
<p style="text-align:justify;">I&#8217;ll come back later with more code related articles. In fact, I think I may start a series of articles soon.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pgraycode.wordpress.com/1049/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pgraycode.wordpress.com/1049/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pgraycode.wordpress.com/1049/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pgraycode.wordpress.com/1049/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pgraycode.wordpress.com/1049/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pgraycode.wordpress.com/1049/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pgraycode.wordpress.com/1049/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pgraycode.wordpress.com/1049/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pgraycode.wordpress.com/1049/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pgraycode.wordpress.com/1049/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pgraycode.wordpress.com/1049/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pgraycode.wordpress.com/1049/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pgraycode.wordpress.com/1049/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pgraycode.wordpress.com/1049/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pgraycode.wordpress.com&amp;blog=6568508&amp;post=1049&amp;subd=pgraycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pgraycode.wordpress.com/2011/05/26/gtk-custom-drawing-and-saving/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9a378ed2410bdabed82883f2615f6b61?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Mithrandir</media:title>
		</media:content>
	</item>
		<item>
		<title>Machine Learning</title>
		<link>http://pgraycode.wordpress.com/2011/05/19/machine-learning/</link>
		<comments>http://pgraycode.wordpress.com/2011/05/19/machine-learning/#comments</comments>
		<pubDate>Thu, 19 May 2011 18:08:38 +0000</pubDate>
		<dc:creator>Mithrandir</dc:creator>
				<category><![CDATA[Artificial Intelligence]]></category>
		<category><![CDATA[Questions of a Beginner]]></category>
		<category><![CDATA[AI]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://pgraycode.wordpress.com/?p=1042</guid>
		<description><![CDATA[This semester, I took an introductory course to Machine Learning. I learned about id3, version space learning, neural networks, GAs, etc. And I had to some homework using several of those techniques. The first one, a id3 classifier was done in C. At the time Haskell was not available as a potential language to write [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pgraycode.wordpress.com&amp;blog=6568508&amp;post=1042&amp;subd=pgraycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">This semester, I took an introductory course to Machine Learning. I learned about id3, version space learning, neural networks, GAs, etc. And I had to some homework using several of those techniques.</p>
<p style="text-align:justify;"><span id="more-1042"></span></p>
<p style="text-align:justify;">The first one, a id3 classifier was done in C. At the time Haskell was not available as a potential language to write the code into. However, the application offered a lot of option and it was fun and entertaining to work on it. Though not perfect, it is something I am very proud of. Code for it can be found on <a href="https://github.com/mihaimaruseac/id3">github</a> and may be forked and improved if needed.</p>
<p style="text-align:justify;">Then, we had to use Reinforcement Learning to make a robot walk in a grid along a certain path. This time, although Haskell was available, I&#8217;ve done it in Python because I wanted to have a GUI and the time was too pressing at that moment to try to do this in Haskell. Code also on <a href="https://github.com/mihaimaruseac/q-learning">github</a>.</p>
<p style="text-align:justify;">Later, we had to implement a regression/forecasting program using neural networks. I&#8217;ve done it in Python too, also because of the GUI and the available time. This homework raised a small problem, the solution to it will be posted soon. Code is on <a href="https://github.com/mihaimaruseac/Backpropagation">github</a>, as a demo for a neural network implementation.</p>
<p style="text-align:justify;">And then, there was the last homework, the freely chosen one. Just like the <a href="http://pgraycode.wordpress.com/2011/02/02/a-semester-of-coding/">previous</a> semester, I chose to do something in Haskell: to implement a robot which uses genetic algorithms to escape from a maze. I generated the maze using Sidewinder&#8217;s algorithm and several questions which will be answered in following posts have arisen. The <a href="https://github.com/mihaimaruseac/HsMaze">implementation</a> is not something that I&#8217;m extremely proud of (since the convergence is slow &#8211; the approach with genetic programming failed). Yet, it is, I believe, a code well written in Haskell, using the right tool for several functions. And I finally learned how to properly use advanced Haskell features, where they are needed, not everywhere or nowhere like in the previous attempts (at least I believe this now).</p>
<p style="text-align:justify;">I&#8217;ll come back with something related to Python and GTK soon.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pgraycode.wordpress.com/1042/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pgraycode.wordpress.com/1042/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pgraycode.wordpress.com/1042/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pgraycode.wordpress.com/1042/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pgraycode.wordpress.com/1042/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pgraycode.wordpress.com/1042/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pgraycode.wordpress.com/1042/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pgraycode.wordpress.com/1042/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pgraycode.wordpress.com/1042/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pgraycode.wordpress.com/1042/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pgraycode.wordpress.com/1042/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pgraycode.wordpress.com/1042/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pgraycode.wordpress.com/1042/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pgraycode.wordpress.com/1042/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pgraycode.wordpress.com&amp;blog=6568508&amp;post=1042&amp;subd=pgraycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pgraycode.wordpress.com/2011/05/19/machine-learning/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9a378ed2410bdabed82883f2615f6b61?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Mithrandir</media:title>
		</media:content>
	</item>
		<item>
		<title>Java Type Signatures</title>
		<link>http://pgraycode.wordpress.com/2011/04/28/java-type-signatures/</link>
		<comments>http://pgraycode.wordpress.com/2011/04/28/java-type-signatures/#comments</comments>
		<pubDate>Thu, 28 Apr 2011 09:12:56 +0000</pubDate>
		<dc:creator>Mithrandir</dc:creator>
				<category><![CDATA[Ideas]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://pgraycode.wordpress.com/?p=1035</guid>
		<description><![CDATA[This year, I had to do several assignments in Java. Some of them didn&#8217;t require me to use a specific IDE but there was one arguing that we should use Eclipse because it helps us in development better than any other tool. And it does so, with several exceptions. My main disappointment came from the way it handles function [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pgraycode.wordpress.com&amp;blog=6568508&amp;post=1035&amp;subd=pgraycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">This year, I had to do several assignments in Java. Some of them didn&#8217;t require me to use a specific IDE but there was one arguing that we should use Eclipse because it helps us in development better than any other tool.</p>
<p style="text-align:justify;"><span id="more-1035"></span></p>
<p style="text-align:justify;">And it does so, with several exceptions. My main disappointment came from the way it handles function signatures. You have the <code>@Override</code> annotation which may help you spot mistakes in overwriting functions from base classes. It helps you doing this but it also fails at some point.</p>
<p style="text-align:justify;">Consider that you have the following interface</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;"><span style="color:#000000;font-weight:bold;">public interface</span> ITest <span style="color:#000000;">{</span>
    <span style="color:#000000;font-weight:bold;">public</span> <span style="color:#830000;">int</span> <span style="color:#010181;">function1</span><span style="color:#000000;">(</span><span style="color:#830000;">int</span> x<span style="color:#000000;">);</span>
    <span style="color:#000000;font-weight:bold;">public</span> <span style="color:#830000;">int</span> <span style="color:#010181;">function2</span><span style="color:#000000;">(</span><span style="color:#830000;">int</span> x<span style="color:#000000;">);</span>
    <span style="color:#000000;font-weight:bold;">public</span> <span style="color:#830000;">int</span> <span style="color:#010181;">function4</span><span style="color:#000000;">(</span><span style="color:#830000;">int</span> x<span style="color:#000000;">);</span>
    <span style="color:#000000;font-weight:bold;">public</span> <span style="color:#830000;">int</span> <span style="color:#010181;">function5</span><span style="color:#000000;">(</span><span style="color:#830000;">int</span> x<span style="color:#000000;">)</span> <span style="color:#000000;font-weight:bold;">throws</span> CustomException<span style="color:#000000;">;</span>
<span style="color:#000000;">}</span></pre>
</p>
<p style="text-align:justify;">with the following CustomException definition.</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;"><span style="color:#000000;font-weight:bold;">public class</span> CustomException <span style="color:#000000;font-weight:bold;">extends</span> <span style="color:#000000;font-weight:bold;">Exception</span> <span style="color:#000000;">{</span> 

    <span style="color:#000000;font-weight:bold;">String</span> msg<span style="color:#000000;">;</span> 

    <span style="color:#000000;font-weight:bold;">public</span> <span style="color:#010181;">CustomException</span><span style="color:#000000;">(</span><span style="color:#000000;font-weight:bold;">String</span> msg<span style="color:#000000;">){</span>
        <span style="color:#000000;font-weight:bold;">this</span><span style="color:#000000;">.</span>msg <span style="color:#000000;">=</span> msg<span style="color:#000000;">;</span>
    <span style="color:#000000;">}</span> 

    &#064;Override
    <span style="color:#000000;font-weight:bold;">public</span> <span style="color:#000000;font-weight:bold;">String</span> <span style="color:#010181;">toString</span><span style="color:#000000;">(){</span>
        <span style="color:#000000;font-weight:bold;">return</span> msg<span style="color:#000000;">;</span>
    <span style="color:#000000;">}</span>
<span style="color:#000000;">}</span> </pre>
</p>
<p style="text-align:justify;">From this, you would understand that the only exception which could be raised is <code>CustomException</code> from <code>function5</code>. Is this really true?</p>
<p style="text-align:justify;">Let&#8217;s see an abstrac class using this interface</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;"><span style="color:#000000;font-weight:bold;">public abstract class</span> ATest1 <span style="color:#000000;font-weight:bold;">implements</span> ITest <span style="color:#000000;">{</span> 

    &#064;Override
    <span style="color:#000000;font-weight:bold;">public</span> <span style="color:#830000;">int</span> <span style="color:#010181;">function1</span><span style="color:#000000;">(</span><span style="color:#830000;">int</span> x<span style="color:#000000;">) {</span>
        <span style="color:#000000;font-weight:bold;">if</span> <span style="color:#000000;">(</span>x <span style="color:#000000;">==</span> <span style="color:#2928ff;">0</span><span style="color:#000000;">)</span>
            <span style="color:#000000;font-weight:bold;">throw new</span> <span style="color:#000000;font-weight:bold;">InvalidParameterException</span><span style="color:#000000;">(</span><span style="color:#ff0000;">&quot;X must not be 0&quot;</span><span style="color:#000000;">);</span>
        <span style="color:#000000;font-weight:bold;">return</span> x<span style="color:#000000;">;</span>
    <span style="color:#000000;">}</span> 

    &#064;Override
    <span style="color:#000000;font-weight:bold;">public abstract</span> <span style="color:#830000;">int</span> <span style="color:#010181;">function2</span><span style="color:#000000;">(</span><span style="color:#830000;">int</span> x<span style="color:#000000;">);</span> 

    &#064;Override
    <span style="color:#000000;font-weight:bold;">public</span> <span style="color:#830000;">int</span> <span style="color:#010181;">function4</span><span style="color:#000000;">(</span><span style="color:#830000;">int</span> x<span style="color:#000000;">) {</span>
        <span style="color:#000000;font-weight:bold;">return</span> x<span style="color:#000000;">;</span>
    <span style="color:#000000;">}</span> 

    &#064;Override
    <span style="color:#000000;font-weight:bold;">public</span> <span style="color:#830000;">int</span> <span style="color:#010181;">function5</span><span style="color:#000000;">(</span><span style="color:#830000;">int</span> x<span style="color:#000000;">)</span> <span style="color:#000000;font-weight:bold;">throws</span> CustomException <span style="color:#000000;">{</span>
        <span style="color:#000000;font-weight:bold;">if</span> <span style="color:#000000;">(</span>x <span style="color:#000000;">==</span> <span style="color:#2928ff;">0</span><span style="color:#000000;">)</span>
            <span style="color:#000000;font-weight:bold;">throw new</span> <span style="color:#010181;">CustomException</span><span style="color:#000000;">(</span><span style="color:#ff0000;">&quot;X must not be 0&quot;</span><span style="color:#000000;">);</span>
        <span style="color:#000000;font-weight:bold;">return</span> <span style="color:#000000;">-</span>x<span style="color:#000000;">;</span>
    <span style="color:#000000;">}</span> 

<span style="color:#000000;">}</span> </pre>
</p>
<p style="text-align:justify;">Now, we have one more exception which can be thrown but Eclipse didn&#8217;t suggest to add a <code>throws</code> declaration to that method.</p>
<p style="text-align:justify;">Now, consider the following two classes, one of them defining the single left out method</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;"><span style="color:#000000;font-weight:bold;">public class</span> Test2 <span style="color:#000000;font-weight:bold;">extends</span> ATest1 <span style="color:#000000;">{</span> 

    &#064;Override
    <span style="color:#000000;font-weight:bold;">public</span> <span style="color:#830000;">int</span> <span style="color:#010181;">function2</span><span style="color:#000000;">(</span><span style="color:#830000;">int</span> x<span style="color:#000000;">) {</span>
        <span style="color:#000000;font-weight:bold;">return</span> x <span style="color:#000000;">+</span> <span style="color:#2928ff;">10</span><span style="color:#000000;">;</span>
    <span style="color:#000000;">}</span>
<span style="color:#000000;">}</span> </pre>
</p>
<p style="text-align:justify;">while the other overwrites a few other methods and doesn&#8217;t throw any exception in them.</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;"><span style="color:#000000;font-weight:bold;">public class</span> Test1 <span style="color:#000000;font-weight:bold;">extends</span> ATest1 <span style="color:#000000;">{</span> 

    &#064;Override
    <span style="color:#000000;font-weight:bold;">public</span> <span style="color:#830000;">int</span> <span style="color:#010181;">function2</span><span style="color:#000000;">(</span><span style="color:#830000;">int</span> x<span style="color:#000000;">) {</span>
        <span style="color:#000000;font-weight:bold;">return</span> x <span style="color:#000000;">+</span> <span style="color:#2928ff;">1</span><span style="color:#000000;">;</span>
    <span style="color:#000000;">}</span> 

    &#064;Override
    <span style="color:#000000;font-weight:bold;">public</span> <span style="color:#830000;">int</span> <span style="color:#010181;">function1</span><span style="color:#000000;">(</span><span style="color:#830000;">int</span> x<span style="color:#000000;">){</span>
        <span style="color:#000000;font-weight:bold;">return</span> x <span style="color:#000000;">+</span> <span style="color:#2928ff;">1</span><span style="color:#000000;">;</span>
    <span style="color:#000000;">}</span> 

    &#064;Override
    <span style="color:#000000;font-weight:bold;">public</span> <span style="color:#830000;">int</span> <span style="color:#010181;">function5</span><span style="color:#000000;">(</span><span style="color:#830000;">int</span> x<span style="color:#000000;">){</span>
        <span style="color:#000000;font-weight:bold;">return</span> x <span style="color:#000000;">+</span> <span style="color:#2928ff;">100</span><span style="color:#000000;">;</span>
    <span style="color:#000000;">}</span> 

<span style="color:#000000;">}</span> </pre>
</p>
<p style="text-align:justify;">Suppose now, that the user has access only to the interface, he knows only the signatures of functions. In this case, what will the following program do?</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;">    <span style="color:#000000;font-weight:bold;">public static</span> <span style="color:#830000;">void</span> <span style="color:#010181;">main</span><span style="color:#000000;">(</span><span style="color:#000000;font-weight:bold;">String</span> args<span style="color:#000000;">[]){</span>
        ITest t1 <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">new</span> <span style="color:#010181;">Test1</span><span style="color:#000000;">();</span>
        ITest t2 <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">new</span> <span style="color:#010181;">Test2</span><span style="color:#000000;">();</span>
        Test2 t3 <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">new</span> <span style="color:#010181;">Test2</span><span style="color:#000000;">();</span>
        Test1 t4 <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">new</span> <span style="color:#010181;">Test1</span><span style="color:#000000;">();</span> 

        <span style="color:#000000;font-weight:bold;">System</span><span style="color:#000000;">.</span>out<span style="color:#000000;">.</span><span style="color:#010181;">println</span><span style="color:#000000;">(</span>t1<span style="color:#000000;">.</span><span style="color:#010181;">function1</span><span style="color:#000000;">(</span><span style="color:#2928ff;">0</span><span style="color:#000000;">));</span>
        <span style="color:#000000;font-weight:bold;">System</span><span style="color:#000000;">.</span>out<span style="color:#000000;">.</span><span style="color:#010181;">println</span><span style="color:#000000;">(</span>t1<span style="color:#000000;">.</span><span style="color:#010181;">function2</span><span style="color:#000000;">(</span><span style="color:#2928ff;">0</span><span style="color:#000000;">));</span>
        <span style="color:#000000;font-weight:bold;">System</span><span style="color:#000000;">.</span>out<span style="color:#000000;">.</span><span style="color:#010181;">println</span><span style="color:#000000;">(</span>t1<span style="color:#000000;">.</span><span style="color:#010181;">function4</span><span style="color:#000000;">(</span><span style="color:#2928ff;">0</span><span style="color:#000000;">));</span>
        <span style="color:#000000;font-weight:bold;">try</span><span style="color:#000000;">{</span>
            <span style="color:#000000;font-weight:bold;">System</span><span style="color:#000000;">.</span>out<span style="color:#000000;">.</span><span style="color:#010181;">println</span><span style="color:#000000;">(</span>t1<span style="color:#000000;">.</span><span style="color:#010181;">function5</span><span style="color:#000000;">(</span><span style="color:#2928ff;">0</span><span style="color:#000000;">));</span>
        <span style="color:#000000;">}</span> <span style="color:#000000;font-weight:bold;">catch</span> <span style="color:#000000;">(</span>CustomException e<span style="color:#000000;">) {</span>
            e<span style="color:#000000;">.</span><span style="color:#010181;">printStackTrace</span><span style="color:#000000;">();</span>
        <span style="color:#000000;">}</span> 

        <span style="color:#000000;font-weight:bold;">System</span><span style="color:#000000;">.</span>out<span style="color:#000000;">.</span><span style="color:#010181;">println</span><span style="color:#000000;">(</span>t2<span style="color:#000000;">.</span><span style="color:#010181;">function1</span><span style="color:#000000;">(</span><span style="color:#2928ff;">0</span><span style="color:#000000;">));</span>
        <span style="color:#000000;font-weight:bold;">System</span><span style="color:#000000;">.</span>out<span style="color:#000000;">.</span><span style="color:#010181;">println</span><span style="color:#000000;">(</span>t2<span style="color:#000000;">.</span><span style="color:#010181;">function2</span><span style="color:#000000;">(</span><span style="color:#2928ff;">0</span><span style="color:#000000;">));</span>
        <span style="color:#000000;font-weight:bold;">System</span><span style="color:#000000;">.</span>out<span style="color:#000000;">.</span><span style="color:#010181;">println</span><span style="color:#000000;">(</span>t2<span style="color:#000000;">.</span><span style="color:#010181;">function4</span><span style="color:#000000;">(</span><span style="color:#2928ff;">0</span><span style="color:#000000;">));</span>
        <span style="color:#000000;font-weight:bold;">try</span> <span style="color:#000000;">{</span>
            <span style="color:#000000;font-weight:bold;">System</span><span style="color:#000000;">.</span>out<span style="color:#000000;">.</span><span style="color:#010181;">println</span><span style="color:#000000;">(</span>t2<span style="color:#000000;">.</span><span style="color:#010181;">function5</span><span style="color:#000000;">(</span><span style="color:#2928ff;">0</span><span style="color:#000000;">));</span>
        <span style="color:#000000;">}</span> <span style="color:#000000;font-weight:bold;">catch</span> <span style="color:#000000;">(</span>CustomException e<span style="color:#000000;">) {</span>
            e<span style="color:#000000;">.</span><span style="color:#010181;">printStackTrace</span><span style="color:#000000;">();</span>
        <span style="color:#000000;">}</span> 

        <span style="color:#000000;font-weight:bold;">System</span><span style="color:#000000;">.</span>out<span style="color:#000000;">.</span><span style="color:#010181;">println</span><span style="color:#000000;">(</span>t3<span style="color:#000000;">.</span><span style="color:#010181;">function1</span><span style="color:#000000;">(</span><span style="color:#2928ff;">0</span><span style="color:#000000;">));</span>
        <span style="color:#000000;font-weight:bold;">System</span><span style="color:#000000;">.</span>out<span style="color:#000000;">.</span><span style="color:#010181;">println</span><span style="color:#000000;">(</span>t3<span style="color:#000000;">.</span><span style="color:#010181;">function2</span><span style="color:#000000;">(</span><span style="color:#2928ff;">0</span><span style="color:#000000;">));</span>
        <span style="color:#000000;font-weight:bold;">System</span><span style="color:#000000;">.</span>out<span style="color:#000000;">.</span><span style="color:#010181;">println</span><span style="color:#000000;">(</span>t3<span style="color:#000000;">.</span><span style="color:#010181;">function4</span><span style="color:#000000;">(</span><span style="color:#2928ff;">0</span><span style="color:#000000;">));</span>
        <span style="color:#000000;font-weight:bold;">try</span> <span style="color:#000000;">{</span>
            <span style="color:#000000;font-weight:bold;">System</span><span style="color:#000000;">.</span>out<span style="color:#000000;">.</span><span style="color:#010181;">println</span><span style="color:#000000;">(</span>t3<span style="color:#000000;">.</span><span style="color:#010181;">function5</span><span style="color:#000000;">(</span><span style="color:#2928ff;">0</span><span style="color:#000000;">));</span>
        <span style="color:#000000;">}</span> <span style="color:#000000;font-weight:bold;">catch</span> <span style="color:#000000;">(</span>CustomException e<span style="color:#000000;">) {</span>
            e<span style="color:#000000;">.</span><span style="color:#010181;">printStackTrace</span><span style="color:#000000;">();</span>
        <span style="color:#000000;">}</span> 

        <span style="color:#000000;font-weight:bold;">System</span><span style="color:#000000;">.</span>out<span style="color:#000000;">.</span><span style="color:#010181;">println</span><span style="color:#000000;">(</span>t4<span style="color:#000000;">.</span><span style="color:#010181;">function1</span><span style="color:#000000;">(</span><span style="color:#2928ff;">0</span><span style="color:#000000;">));</span>
        <span style="color:#000000;font-weight:bold;">System</span><span style="color:#000000;">.</span>out<span style="color:#000000;">.</span><span style="color:#010181;">println</span><span style="color:#000000;">(</span>t4<span style="color:#000000;">.</span><span style="color:#010181;">function2</span><span style="color:#000000;">(</span><span style="color:#2928ff;">0</span><span style="color:#000000;">));</span>
        <span style="color:#000000;font-weight:bold;">System</span><span style="color:#000000;">.</span>out<span style="color:#000000;">.</span><span style="color:#010181;">println</span><span style="color:#000000;">(</span>t4<span style="color:#000000;">.</span><span style="color:#010181;">function4</span><span style="color:#000000;">(</span><span style="color:#2928ff;">0</span><span style="color:#000000;">));</span>
        <span style="color:#000000;font-weight:bold;">System</span><span style="color:#000000;">.</span>out<span style="color:#000000;">.</span><span style="color:#010181;">println</span><span style="color:#000000;">(</span>t4<span style="color:#000000;">.</span><span style="color:#010181;">function5</span><span style="color:#000000;">(</span><span style="color:#2928ff;">0</span><span style="color:#000000;">));</span>
    <span style="color:#000000;">}</span> </pre>
</p>
<p style="text-align:justify;">Each try-catch block above was suggested by Eclipse. As you see in <code>function5</code> calls, this is not a symmetrical way, giving rise to some errors while refactoring. Hopefully, Eclipse is there and suggests you that there are some exceptions.</p>
<p style="text-align:justify;">Yet, should you make <code>main</code> throw exceptions instead of reporting them, there would be no way to reason why that exception appeared there after changing <code>t4</code> with <code>t3</code> in the last line.</p>
<p style="text-align:justify;">However, the above <code>main</code> doesn&#8217;t throw <code>CustomException</code>. It throws <code>InvalidParameterException</code> from <code>t2.function1(0)</code>, something which is inexplicable from the function&#8217;s signature.</p>
<p style="text-align:justify;">This is similar to getting a <code>NullPointerException</code> somewhere where you expected only valid references. Or a segfault in a library function which didn&#8217;t present itself as being able to segfault. Something which you cannot fix and the documentation doesn&#8217;t explain how it was produced. A bug and a very nasty one.</p>
<p style="text-align:justify;">You may say that this is an unlikely situation, that since you knew what exceptions are thrown, you can add the <code>throws</code> annotation to the interface and then you can document it. However, there are cases of big projects with multiple inheritance paths and lengthy development history with multiple teams of developers. In these instances, it is very common to throw some exception in a method from a class deep down in the hierarchy without needing to report it upwards and document it.</p>
<p style="text-align:justify;">In Haskell, it is simpler. When you change a function&#8217;s type to include the possibility of failure, the <code>Maybe</code> you might add must be put in every signature for each function calling you function, you&#8217;ll have to change them all. Thus, you&#8217;ll surely be forced to document the possibility of failure.</p>
<p style="text-align:justify;">Only <code>head: empty list</code> and <code>Prelude: undefined</code> or exceptions regarding missing patterns can show up (maybe several others but I guess that they can be sorted into one of these three). The last two cases indicate a serious error in the library design and can be fixed, someway or the other. And the first case is the reason why a stack-trace printing debugger for Haskell is so much desired.</p>
<p style="text-align:justify;">But this was a rant about Java, not about Haskell.</p>
<p style="text-align:justify;">EDIT: I hit the post button to early and it is possible that a version with TODOs has appeared in your feed, I&#8217;m sorry for that.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pgraycode.wordpress.com/1035/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pgraycode.wordpress.com/1035/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pgraycode.wordpress.com/1035/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pgraycode.wordpress.com/1035/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pgraycode.wordpress.com/1035/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pgraycode.wordpress.com/1035/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pgraycode.wordpress.com/1035/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pgraycode.wordpress.com/1035/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pgraycode.wordpress.com/1035/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pgraycode.wordpress.com/1035/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pgraycode.wordpress.com/1035/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pgraycode.wordpress.com/1035/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pgraycode.wordpress.com/1035/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pgraycode.wordpress.com/1035/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pgraycode.wordpress.com&amp;blog=6568508&amp;post=1035&amp;subd=pgraycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pgraycode.wordpress.com/2011/04/28/java-type-signatures/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9a378ed2410bdabed82883f2615f6b61?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Mithrandir</media:title>
		</media:content>
	</item>
		<item>
		<title>Solving XKCD&#8217;s Nerd Sniping problem</title>
		<link>http://pgraycode.wordpress.com/2011/02/23/solving-xkcds-nerd-snipping-problem/</link>
		<comments>http://pgraycode.wordpress.com/2011/02/23/solving-xkcds-nerd-snipping-problem/#comments</comments>
		<pubDate>Wed, 23 Feb 2011 17:39:02 +0000</pubDate>
		<dc:creator>Mithrandir</dc:creator>
				<category><![CDATA[Puzzles]]></category>
		<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://pgraycode.wordpress.com/?p=1004</guid>
		<description><![CDATA[A while ago, during a period of free time, I implemented a solution in Haskell for the XKCD&#8217;s raptor problem. Now, I&#8217;ll try to solve another problem presented there, the one found in the Nerd Sniping comic. Of course, the implementation will still be in Haskell. First, we have to define a data structure for [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pgraycode.wordpress.com&amp;blog=6568508&amp;post=1004&amp;subd=pgraycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">A while ago, during a period of free time, I implemented <a href="http://pgraycode.wordpress.com/2010/05/29/raptors/">a solution in Haskell</a> for the <a href="http://xkcd.com/135/">XKCD&#8217;s raptor problem</a>. Now, I&#8217;ll try to solve another problem presented there, the one found in the <a href="http://xkcd.com/356/">Nerd Sniping comic</a>. Of course, the implementation will still be in Haskell.</p>
<p style="text-align:justify;"><span id="more-1004"></span>First, we have to define a data structure for keeping the network of resistors. I&#8217;ll use a structure used a while ago in a C algorithm for solving the <a href="http://en.wikipedia.org/wiki/Network_analysis_%28electrical_circuits%29">network analysis problem</a> but more simplified than what I used at the time. We only need to be interested in networks formed with resistors only, thus we can use only <a href="http://en.wikipedia.org/wiki/Network_analysis_%28electrical_circuits%29#General_form_of_network_node_elimination">node elimination</a> to solve this problem. Because of this, the data structures used are very simple:</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;"><span style="color:#555555;">    2 </span>
<span style="color:#555555;">    3 </span><span style="color:#010181;">type</span> Resistance <span style="color:#000000;">=</span> <span style="color:#830000;">Double</span>
<span style="color:#555555;">    4 </span><span style="color:#010181;">type</span> Conductance <span style="color:#000000;">=</span> <span style="color:#830000;">Double</span>
<span style="color:#555555;">    5 </span>
<span style="color:#555555;">    6 </span><span style="color:#010181;">type</span> Network a <span style="color:#000000;">= [(</span>a<span style="color:#000000;">,</span> Node a<span style="color:#000000;">)]</span>
<span style="color:#555555;">    7 </span><span style="color:#010181;">type</span> Node a <span style="color:#000000;">= [</span>WireTo a<span style="color:#000000;">]</span>
<span style="color:#555555;">    8 </span><span style="color:#010181;">type</span> WireTo a <span style="color:#000000;">= (</span>a<span style="color:#000000;">,</span> Resistance<span style="color:#000000;">)</span>
<span style="color:#555555;">    9 </span> </pre>
</p>
<p style="text-align:justify;">As you see, we&#8217;ll use two association lists: one to keep the mapping between node ids (those can be almost anything) and one to keep the mapping between the node id and the resistance of the wire connected between that node and the actual node. While this structure prevents the <a href="http://www.haskell.org/haskellwiki/Tying_the_Knot">know tying problem</a> (<strong>homework</strong>: try solving this problem using techniques presented in the linked article), it adds duplicated information: for each wire between nodes a and b we store it in two places: once in node a and once in node b.</p>
<p style="text-align:justify;">The previous paragraph hinted that there are two problems which need to be solved. Firstly, when comparing two wires and two nodes only the ids need to be taken care of, not everything in the pair. Thus, we need to implement our own equality functions.</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;"><span style="color:#555555;">   10 </span><span style="color:#000000;">(===) :: (</span>Eq a<span style="color:#000000;">) =&gt; (</span>a<span style="color:#000000;">,</span> b<span style="color:#000000;">) -&gt; (</span>a<span style="color:#000000;">,</span> b<span style="color:#000000;">) -&gt;</span> <span style="color:#830000;">Bool</span>
<span style="color:#555555;">   11 </span><span style="color:#000000;">(</span>a<span style="color:#000000;">,</span> _<span style="color:#000000;">) === (</span>b<span style="color:#000000;">,</span> _<span style="color:#000000;">) =</span> a <span style="color:#000000;">==</span> b
<span style="color:#555555;">   12 </span>
<span style="color:#555555;">   13 </span><span style="color:#000000;">(=/=) :: (</span>Eq a<span style="color:#000000;">) =&gt; (</span>a<span style="color:#000000;">,</span> b<span style="color:#000000;">) -&gt; (</span>a<span style="color:#000000;">,</span> b<span style="color:#000000;">) -&gt;</span> <span style="color:#830000;">Bool</span>
<span style="color:#555555;">   14 </span><span style="color:#000000;">(=/=)</span> a <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">not</span> <span style="color:#000000;">. (</span>a <span style="color:#000000;">===)</span> </pre>
</p>
<p style="text-align:justify;">To solve the second problem, I wrote two functions to the user and will demand (expect) that the user will use them to construct any network. Thus, the first function will construct a part of the network: a simple wire (similar to the <code>return</code> function from monads).</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;"><span style="color:#555555;">   66 </span><span style="color:#838183;font-style:italic;">-- builds a simple network: a simple wire</span>
<span style="color:#555555;">   67 </span>buildPart <span style="color:#000000;">:: (</span>Ord a<span style="color:#000000;">) =&gt;</span> a <span style="color:#000000;">-&gt;</span> a <span style="color:#000000;">-&gt;</span> Resistance <span style="color:#000000;">-&gt;</span> Network a
<span style="color:#555555;">   68 </span>buildPart a b r
<span style="color:#555555;">   69 </span>  <span style="color:#000000;">|</span> r <span style="color:#000000;">&lt;</span> <span style="color:#2928ff;">0</span> <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">error</span> <span style="color:#ff0000;">&quot;Negative resistance is not allowed&quot;</span>
<span style="color:#555555;">   70 </span>  <span style="color:#000000;">|</span> a <span style="color:#000000;">==</span> b <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">error</span> <span style="color:#ff0000;">&quot;No wire can have the same ends&quot;</span>
<span style="color:#555555;">   71 </span>  <span style="color:#000000;">|</span> a <span style="color:#000000;">&gt;</span> b <span style="color:#000000;">=</span> buildPart b a r
<span style="color:#555555;">   72 </span>  <span style="color:#000000;">|</span> <span style="color:#000000;font-weight:bold;">otherwise</span> <span style="color:#000000;">= [(</span>a<span style="color:#000000;">, [(</span>b<span style="color:#000000;">,</span> r<span style="color:#000000;">)]), (</span>b<span style="color:#000000;">, [(</span>a<span style="color:#000000;">,</span> r<span style="color:#000000;">)])]</span> </pre>
</p>
<p style="text-align:justify;">Using this function we can already construct the first test network, as seen in the following picture:</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;"><span style="color:#555555;">  117 </span><span style="color:#838183;font-style:italic;">{-</span>
<span style="color:#555555;">  118 </span><span style="color:#838183;font-style:italic;">Simple test: a network with only a wire.</span>
<span style="color:#555555;">  119 </span><span style="color:#838183;font-style:italic;">-}</span>
<span style="color:#555555;">  120 </span>testSimple <span style="color:#000000;">::</span> Network <span style="color:#830000;">Int</span>
<span style="color:#555555;">  121 </span>testSimple <span style="color:#000000;">=</span> buildPart <span style="color:#2928ff;">0 1 5</span> </pre>
</p>
<p style="text-align:center;"><a href="http://twitpic.com/42ykth" title="Share photos on twitter with Twitpic"><img src="http://twitpic.com/show/thumb/42ykth.png" width="150" height="150" alt="Share photos on twitter with Twitpic" /></a><br />
Simple network with only a resistor</p>
<p style="text-align:justify;">The second function will take two networks and construct a new one by joining them (similar to the <code>mplus</code> function from <code>MonadPlus</code> or <code>mappend</code> from <code>Monoid</code>).</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;"><span style="color:#555555;">   74 </span><span style="color:#838183;font-style:italic;">-- joins two networks</span>
<span style="color:#555555;">   75 </span>joinParts <span style="color:#000000;">:: (</span>Eq a<span style="color:#000000;">) =&gt;</span> Network a <span style="color:#000000;">-&gt;</span> Network a <span style="color:#000000;">-&gt;</span> Network a
<span style="color:#555555;">   76 </span>joinParts <span style="color:#000000;">[]</span> ns <span style="color:#000000;">=</span> ns
<span style="color:#555555;">   77 </span>joinParts <span style="color:#000000;">(</span>n<span style="color:#000000;">:</span>ns<span style="color:#000000;">)</span> nodes
<span style="color:#555555;">   78 </span>  <span style="color:#000000;">|</span> other <span style="color:#000000;">==</span> Nothing <span style="color:#000000;">=</span> n <span style="color:#000000;">:</span> joinParts ns nodes
<span style="color:#555555;">   79 </span>  <span style="color:#000000;">|</span> <span style="color:#000000;font-weight:bold;">otherwise</span> <span style="color:#000000;">= (</span><span style="color:#000000;font-weight:bold;">fst</span> n<span style="color:#000000;">,</span> <span style="color:#000000;font-weight:bold;">snd</span> n `combineNodes` m<span style="color:#000000;">) :</span> joinParts ns nodes<span style="color:#000000;">'</span>
<span style="color:#555555;">   80 </span>  <span style="color:#010181;">where</span>
<span style="color:#555555;">   81 </span>    other <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">lookup</span> <span style="color:#000000;">(</span><span style="color:#000000;font-weight:bold;">fst</span> n<span style="color:#000000;">)</span> nodes
<span style="color:#555555;">   82 </span>    m <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">fromJust</span> other
<span style="color:#555555;">   83 </span>    nodes<span style="color:#000000;">' =</span> <span style="color:#000000;font-weight:bold;">filter</span> <span style="color:#000000;">(=/=</span> n<span style="color:#000000;">)</span> nodes</pre>
</p>
<p style="text-align:justify;">Care must be taken when joining two networks containing the same nodes. To combine them correctly, the following two functions are used:</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;"><span style="color:#555555;">   85 </span><span style="color:#838183;font-style:italic;">{-</span>
<span style="color:#555555;">   86 </span><span style="color:#838183;font-style:italic;">Combines two nodes (wires leaving the same node, declared in two parts of</span>
<span style="color:#555555;">   87 </span><span style="color:#838183;font-style:italic;">network.</span>
<span style="color:#555555;">   88 </span><span style="color:#838183;font-style:italic;">-}</span>
<span style="color:#555555;">   89 </span>combineNodes <span style="color:#000000;">:: (</span>Eq a<span style="color:#000000;">) =&gt;</span> Node a <span style="color:#000000;">-&gt;</span> Node a <span style="color:#000000;">-&gt;</span> Node a
<span style="color:#555555;">   90 </span>combineNodes <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">foldr</span> <span style="color:#000000;">(\(</span>i<span style="color:#000000;">,</span>r<span style="color:#000000;">)</span> n <span style="color:#000000;">-&gt;</span> addWireTo n i r<span style="color:#000000;">)</span>
<span style="color:#555555;">   91 </span>
<span style="color:#555555;">   92 </span><span style="color:#838183;font-style:italic;">{-</span>
<span style="color:#555555;">   93 </span><span style="color:#838183;font-style:italic;">Adds a wire to a node, doing the right thing if there is a wire there already.</span>
<span style="color:#555555;">   94 </span><span style="color:#838183;font-style:italic;">-}</span>
<span style="color:#555555;">   95 </span>addWireTo <span style="color:#000000;">:: (</span>Eq a<span style="color:#000000;">) =&gt;</span> Node a <span style="color:#000000;">-&gt;</span> a <span style="color:#000000;">-&gt;</span> Resistance <span style="color:#000000;">-&gt;</span> Node a
<span style="color:#555555;">   96 </span>addWireTo w a r <span style="color:#000000;">=</span> parallel <span style="color:#000000;">$ (</span>a<span style="color:#000000;">,</span>r<span style="color:#000000;">) :</span> w</pre>
</p>
<p style="text-align:justify;">When we encounter two parallel resistances, we will transform them immediately.</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;"><span style="color:#555555;">   98 </span><span style="color:#838183;font-style:italic;">{-</span>
<span style="color:#555555;">   99 </span><span style="color:#838183;font-style:italic;">Reduce a list of wires by reducing parallel resistors to a single one.</span>
<span style="color:#555555;">  100 </span><span style="color:#838183;font-style:italic;">-}</span>
<span style="color:#555555;">  101 </span>parallel <span style="color:#000000;">:: (</span>Eq a<span style="color:#000000;">) =&gt; [</span>WireTo a<span style="color:#000000;">] -&gt; [</span>WireTo a<span style="color:#000000;">]</span>
<span style="color:#555555;">  102 </span>parallel <span style="color:#000000;">[] = []</span>
<span style="color:#555555;">  103 </span>parallel <span style="color:#000000;">(</span>w<span style="color:#000000;">:</span>ws<span style="color:#000000;">) =</span> w<span style="color:#000000;">' :</span> parallel ws<span style="color:#000000;">'</span>
<span style="color:#555555;">  104 </span>  <span style="color:#010181;">where</span>
<span style="color:#555555;">  105 </span>    ws<span style="color:#000000;">' =</span> <span style="color:#000000;font-weight:bold;">filter</span> <span style="color:#000000;">(=/=</span> w<span style="color:#000000;">)</span> ws
<span style="color:#555555;">  106 </span>    ws<span style="color:#000000;">'' =</span> w <span style="color:#000000;">:</span> <span style="color:#000000;font-weight:bold;">filter</span> <span style="color:#000000;">(===</span> w<span style="color:#000000;">)</span> ws
<span style="color:#555555;">  107 </span>    w<span style="color:#000000;">' =</span> <span style="color:#010181;">if</span> ws<span style="color:#000000;">'' == []</span> <span style="color:#010181;">then</span> w <span style="color:#010181;">else</span> <span style="color:#000000;font-weight:bold;">foldl1</span> parallel<span style="color:#000000;">'</span> ws<span style="color:#000000;">''</span>
<span style="color:#555555;">  108 </span>
<span style="color:#555555;">  109 </span><span style="color:#838183;font-style:italic;">{-</span>
<span style="color:#555555;">  110 </span><span style="color:#838183;font-style:italic;">Reduce two wires to a single one, if they form parallel resistors.</span>
<span style="color:#555555;">  111 </span><span style="color:#838183;font-style:italic;">-}</span>
<span style="color:#555555;">  112 </span>parallel<span style="color:#000000;">' :: (</span>Eq a<span style="color:#000000;">) =&gt;</span> WireTo a <span style="color:#000000;">-&gt;</span> WireTo a <span style="color:#000000;">-&gt;</span> WireTo a
<span style="color:#555555;">  113 </span>parallel<span style="color:#000000;">' (</span>a<span style="color:#000000;">,</span> r1<span style="color:#000000;">) (</span>b<span style="color:#000000;">,</span> r2<span style="color:#000000;">)</span>
<span style="color:#555555;">  114 </span>  <span style="color:#000000;">|</span> a <span style="color:#000000;">==</span> b <span style="color:#000000;">= (</span>a<span style="color:#000000;">,</span> r1 <span style="color:#000000;">*</span> r2 <span style="color:#000000;">/ (</span>r1 <span style="color:#000000;">+</span> r2<span style="color:#000000;">))</span>
<span style="color:#555555;">  115 </span>  <span style="color:#000000;">|</span> <span style="color:#000000;font-weight:bold;">otherwise</span> <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">error</span> <span style="color:#ff0000;">&quot;Cannot reduce: not parallel resistors&quot;</span> </pre>
</p>
<p style="text-align:justify;">Right now, we can construct several more tests, presented in the following pictures</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;"><span style="color:#555555;">  123 </span><span style="color:#838183;font-style:italic;">{-</span>
<span style="color:#555555;">  124 </span><span style="color:#838183;font-style:italic;">Second test: a network with two parallel wires.</span>
<span style="color:#555555;">  125 </span><span style="color:#838183;font-style:italic;">-}</span>
<span style="color:#555555;">  126 </span>testSimple<span style="color:#000000;">' ::</span> Network <span style="color:#830000;">Int</span>
<span style="color:#555555;">  127 </span>testSimple<span style="color:#000000;">' =</span> buildPart <span style="color:#2928ff;">0 1 10</span> `joinParts` buildPart <span style="color:#2928ff;">0 1 6</span>
<span style="color:#555555;">  128 </span>
<span style="color:#555555;">  129 </span><span style="color:#838183;font-style:italic;">{-</span>
<span style="color:#555555;">  130 </span><span style="color:#838183;font-style:italic;">Third test: a network with two series resistors.</span>
<span style="color:#555555;">  131 </span><span style="color:#838183;font-style:italic;">-}</span>
<span style="color:#555555;">  132 </span>testSimple<span style="color:#000000;">'' =</span> buildPart <span style="color:#2928ff;">0 1 40</span> `joinParts` buildPart <span style="color:#2928ff;">1 2 2</span>
<span style="color:#555555;">  133 </span>
<span style="color:#555555;">  134 </span><span style="color:#838183;font-style:italic;">{-</span>
<span style="color:#555555;">  135 </span><span style="color:#838183;font-style:italic;">Fourth test: tetrahedron</span>
<span style="color:#555555;">  136 </span><span style="color:#838183;font-style:italic;">-}</span>
<span style="color:#555555;">  137 </span>testTetra <span style="color:#000000;">::</span> Network <span style="color:#830000;">Int</span>
<span style="color:#555555;">  138 </span>testTetra
<span style="color:#555555;">  139 </span>  <span style="color:#000000;">=</span> buildPart <span style="color:#2928ff;">0 1 1</span> `joinParts`
<span style="color:#555555;">  140 </span>    buildPart <span style="color:#2928ff;">0 2 1</span> `joinParts`
<span style="color:#555555;">  141 </span>    buildPart <span style="color:#2928ff;">1 2 1</span> `joinParts`
<span style="color:#555555;">  142 </span>    buildPart <span style="color:#2928ff;">0 3 1</span> `joinParts`
<span style="color:#555555;">  143 </span>    buildPart <span style="color:#2928ff;">1 3 1</span> `joinParts`
<span style="color:#555555;">  144 </span>    buildPart <span style="color:#2928ff;">2 3 1</span> </pre>
</p>
<p style="text-align:center;"><a href="http://twitpic.com/42yn64" title="Share photos on twitter with Twitpic"><img src="http://twitpic.com/show/thumb/42yn64.png" width="150" height="150" alt="Share photos on twitter with Twitpic" /></a><a href="http://twitpic.com/42ynbb" title="Share photos on twitter with Twitpic"><img src="http://twitpic.com/show/thumb/42ynbb.png" width="150" height="150" alt="Share photos on twitter with Twitpic" /></a><a href="http://twitpic.com/42yngo" title="Share photos on twitter with Twitpic"><img src="http://twitpic.com/show/thumb/42yngo.png" width="150" height="150" alt="Share photos on twitter with Twitpic" /></a><br />
A collection of networks</p>
<p style="text-align:justify;">Now, we can even build 2D networks by giving Cartesian id&#8217;s to nodes. For example, the following is the simplest instance of the XKCD problem (reduced to a minimum configuration).</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;"><span style="color:#555555;">  146 </span><span style="color:#838183;font-style:italic;">{-</span>
<span style="color:#555555;">  147 </span><span style="color:#838183;font-style:italic;">Fifth test: 2 squares</span>
<span style="color:#555555;">  148 </span><span style="color:#838183;font-style:italic;">-}</span>
<span style="color:#555555;">  149 </span>testSquares <span style="color:#000000;">::</span> Network <span style="color:#000000;">(</span><span style="color:#830000;">Int</span><span style="color:#000000;">,</span> <span style="color:#830000;">Int</span><span style="color:#000000;">)</span>
<span style="color:#555555;">  150 </span>testSquares <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">foldl</span> joinParts <span style="color:#000000;">[] .</span> <span style="color:#000000;font-weight:bold;">map</span> <span style="color:#000000;">(\(</span>x<span style="color:#000000;">,</span> y<span style="color:#000000;">) -&gt;</span> buildPart x y <span style="color:#2928ff;">1</span><span style="color:#000000;">) $</span> <span style="color:#000000;font-weight:bold;">list</span>
<span style="color:#555555;">  151 </span>  <span style="color:#010181;">where</span>
<span style="color:#555555;">  152 </span>    <span style="color:#000000;font-weight:bold;">list</span> <span style="color:#000000;">= [((</span><span style="color:#2928ff;">0</span><span style="color:#000000;">,</span> <span style="color:#2928ff;">0</span><span style="color:#000000;">), (</span><span style="color:#2928ff;">0</span><span style="color:#000000;">,</span> <span style="color:#2928ff;">1</span><span style="color:#000000;">)), ((</span><span style="color:#2928ff;">0</span><span style="color:#000000;">,</span> <span style="color:#2928ff;">1</span><span style="color:#000000;">), (</span><span style="color:#2928ff;">0</span><span style="color:#000000;">,</span> <span style="color:#2928ff;">2</span><span style="color:#000000;">)), ((</span><span style="color:#2928ff;">0</span><span style="color:#000000;">,</span> <span style="color:#2928ff;">2</span><span style="color:#000000;">), (</span><span style="color:#2928ff;">1</span><span style="color:#000000;">,</span> <span style="color:#2928ff;">2</span><span style="color:#000000;">)),</span>
<span style="color:#555555;">  153 </span>      <span style="color:#000000;">((</span><span style="color:#2928ff;">1</span><span style="color:#000000;">,</span> <span style="color:#2928ff;">2</span><span style="color:#000000;">), (</span><span style="color:#2928ff;">1</span><span style="color:#000000;">,</span> <span style="color:#2928ff;">1</span><span style="color:#000000;">)), ((</span><span style="color:#2928ff;">1</span><span style="color:#000000;">,</span> <span style="color:#2928ff;">1</span><span style="color:#000000;">), (</span><span style="color:#2928ff;">0</span><span style="color:#000000;">,</span> <span style="color:#2928ff;">1</span><span style="color:#000000;">)), ((</span><span style="color:#2928ff;">1</span><span style="color:#000000;">,</span> <span style="color:#2928ff;">1</span><span style="color:#000000;">), (</span><span style="color:#2928ff;">1</span><span style="color:#000000;">,</span> <span style="color:#2928ff;">0</span><span style="color:#000000;">)), ((</span><span style="color:#2928ff;">0</span><span style="color:#000000;">,</span> <span style="color:#2928ff;">0</span><span style="color:#000000;">), (</span><span style="color:#2928ff;">1</span><span style="color:#000000;">,</span> <span style="color:#2928ff;">0</span><span style="color:#000000;">))]</span> </pre>
</p>
<p style="text-align:center;"><a href="http://twitpic.com/42yo5w" title="Share photos on twitter with Twitpic"><img src="http://twitpic.com/show/thumb/42yo5w.png" width="150" height="150" alt="Share photos on twitter with Twitpic" /></a><br />
Small 2D network</p>
<p style="text-align:justify;">Before going into defining the XCKD problem and solving it, we need to implement the algorithm for solving any network of resistors. <i>The following code is not optimized and I am really sure that it can be tweaked a little to allow for infinite structures due to the laziness of Haskell</i> (to solve this is left as a <b>homework</b>). First, the code tests whether we compute a valid answer or not.</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;"><span style="color:#555555;">   16 </span><span style="color:#838183;font-style:italic;">{-</span>
<span style="color:#555555;">   17 </span><span style="color:#838183;font-style:italic;">Starts the solving phase testing if each node is defined.</span>
<span style="color:#555555;">   18 </span><span style="color:#838183;font-style:italic;">-}</span>
<span style="color:#555555;">   19 </span>solve <span style="color:#000000;">:: (</span>Ord a<span style="color:#000000;">) =&gt;</span> Network a <span style="color:#000000;">-&gt;</span> a <span style="color:#000000;">-&gt;</span> a <span style="color:#000000;">-&gt;</span> Resistance
<span style="color:#555555;">   20 </span>solve n st en
<span style="color:#555555;">   21 </span>  <span style="color:#000000;">|</span> stn <span style="color:#000000;">==</span> Nothing <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">error</span> <span style="color:#ff0000;">&quot;Wrong start node&quot;</span>
<span style="color:#555555;">   22 </span>  <span style="color:#000000;">|</span> enn <span style="color:#000000;">==</span> Nothing <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">error</span> <span style="color:#ff0000;">&quot;Wrong end node&quot;</span>
<span style="color:#555555;">   23 </span>  <span style="color:#000000;">|</span> <span style="color:#000000;font-weight:bold;">otherwise</span> <span style="color:#000000;">=</span> solve<span style="color:#000000;">'</span> n st en
<span style="color:#555555;">   24 </span>  <span style="color:#010181;">where</span>
<span style="color:#555555;">   25 </span>    stn <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">lookup</span> st n
<span style="color:#555555;">   26 </span>    stnd <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">fromJust</span> stn
<span style="color:#555555;">   27 </span>    enn <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">lookup</span> en n</pre>
</p>
<p style="text-align:justify;">After we are sure that the nodes in question exist in the network, we start the solving process</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;"><span style="color:#555555;">   29 </span>solve<span style="color:#000000;">' :: (</span>Ord a<span style="color:#000000;">) =&gt;</span> Network a <span style="color:#000000;">-&gt;</span> a <span style="color:#000000;">-&gt;</span> a <span style="color:#000000;">-&gt;</span> Resistance
<span style="color:#555555;">   30 </span>solve<span style="color:#000000;">'</span> n st en
<span style="color:#555555;">   31 </span>  <span style="color:#000000;">|</span> <span style="color:#000000;font-weight:bold;">null</span> candidates <span style="color:#000000;">=</span> getSolution n st en
<span style="color:#555555;">   32 </span>  <span style="color:#000000;">|</span> <span style="color:#000000;font-weight:bold;">otherwise</span> <span style="color:#000000;">=</span> solve<span style="color:#000000;">' (</span>removeNode n <span style="color:#000000;">(</span><span style="color:#000000;font-weight:bold;">head</span> candidates<span style="color:#000000;">))</span> st en
<span style="color:#555555;">   33 </span>  <span style="color:#010181;">where</span>
<span style="color:#555555;">   34 </span>    candidates <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">filter</span> <span style="color:#000000;">(\(</span>x<span style="color:#000000;">,</span>_<span style="color:#000000;">) -&gt;</span> x <span style="color:#000000;">/=</span> st <span style="color:#000000;">&amp;&amp;</span> x <span style="color:#000000;">/=</span> en<span style="color:#000000;">)</span> n</pre>
</p>
<p style="text-align:justify;">When we have only the start and end nodes we return the solution</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;"><span style="color:#555555;">   63 </span>getSolution <span style="color:#000000;">:: (</span>Eq a<span style="color:#000000;">) =&gt;</span> Network a <span style="color:#000000;">-&gt;</span> a <span style="color:#000000;">-&gt;</span> a <span style="color:#000000;">-&gt;</span> Resistance
<span style="color:#555555;">   64 </span>getSolution n st en <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">fromJust</span> <span style="color:#000000;">.</span> <span style="color:#000000;font-weight:bold;">lookup</span> en <span style="color:#000000;">.</span> <span style="color:#000000;font-weight:bold;">fromJust</span> <span style="color:#000000;">.</span> <span style="color:#000000;font-weight:bold;">lookup</span> st <span style="color:#000000;">$</span> n</pre>
</p>
<p style="text-align:justify;">Otherwise, when we have a candidate node to be removed, we remove it:</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;"><span style="color:#555555;">   36 </span>removeNode <span style="color:#000000;">:: (</span>Ord a<span style="color:#000000;">) =&gt;</span> Network a <span style="color:#000000;">-&gt; (</span>a<span style="color:#000000;">,</span> Node a<span style="color:#000000;">) -&gt;</span> Network a
<span style="color:#555555;">   37 </span>removeNode net w<span style="color:#000000;">&#064;(</span>tag<span style="color:#000000;">,</span> nod<span style="color:#000000;">)</span>
<span style="color:#555555;">   38 </span>  <span style="color:#000000;">|</span> <span style="color:#000000;font-weight:bold;">length</span> nod <span style="color:#000000;">==</span> <span style="color:#2928ff;">1</span> <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">filter</span> <span style="color:#000000;">(=/=</span> w<span style="color:#000000;">)</span> net
<span style="color:#555555;">   39 </span>  <span style="color:#000000;">|</span> <span style="color:#000000;font-weight:bold;">otherwise</span> <span style="color:#000000;">=</span> filtered `joinParts` keep `joinParts` new
<span style="color:#555555;">   40 </span>  <span style="color:#010181;">where</span>
<span style="color:#555555;">   41 </span>    affectedTags <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">map fst</span> nod
<span style="color:#555555;">   42 </span>    <span style="color:#838183;font-style:italic;">-- construct the unaffected nodes list</span>
<span style="color:#555555;">   43 </span>    unaffectedNodes <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">filter</span> <span style="color:#000000;">(\(</span>a<span style="color:#000000;">,</span>_<span style="color:#000000;">) -&gt;</span> a `<span style="color:#000000;font-weight:bold;">notElem</span>` affectedTags<span style="color:#000000;">)</span> net
<span style="color:#555555;">   44 </span>    keep <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">filter</span> <span style="color:#000000;">(=/=</span> w<span style="color:#000000;">)</span> unaffectedNodes
<span style="color:#555555;">   45 </span>    <span style="color:#838183;font-style:italic;">-- and the affected ones</span>
<span style="color:#555555;">   46 </span>    change <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">filter</span> <span style="color:#000000;">(\(</span>a<span style="color:#000000;">,</span>_<span style="color:#000000;">)-&gt;</span> a `<span style="color:#000000;font-weight:bold;">elem</span>` affectedTags<span style="color:#000000;">)</span> net
<span style="color:#555555;">   47 </span>    <span style="color:#838183;font-style:italic;">-- remove the node from all the maps</span>
<span style="color:#555555;">   48 </span>    filtered <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">map</span> <span style="color:#000000;">(</span>purify tag<span style="color:#000000;">)</span> change
<span style="color:#555555;">   49 </span>    <span style="color:#838183;font-style:italic;">-- compute the sum of inverses</span>
<span style="color:#555555;">   50 </span>    sumR <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">sum</span> <span style="color:#000000;">.</span> <span style="color:#000000;font-weight:bold;">map</span> <span style="color:#000000;">((</span><span style="color:#2928ff;">1</span><span style="color:#000000;">/) .</span> <span style="color:#000000;font-weight:bold;">snd</span><span style="color:#000000;">) $</span> nod
<span style="color:#555555;">   51 </span>    pairs <span style="color:#000000;">= [(</span>x<span style="color:#000000;">,</span> y<span style="color:#000000;">) |</span> x <span style="color:#000000;">&lt;-</span> affectedTags<span style="color:#000000;">,</span> y <span style="color:#000000;">&lt;-</span> affectedTags<span style="color:#000000;">,</span> x <span style="color:#000000;">&lt;</span> y<span style="color:#000000;">]</span>
<span style="color:#555555;">   52 </span>    new <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">foldl</span> joinParts <span style="color:#000000;">[] .</span> <span style="color:#000000;font-weight:bold;">map</span> <span style="color:#000000;">(</span>buildFromTags nod sumR<span style="color:#000000;">) $</span> pairs
<span style="color:#555555;">   53 </span>
<span style="color:#555555;">   54 </span>purify <span style="color:#000000;">:: (</span>Eq a<span style="color:#000000;">) =&gt;</span> a <span style="color:#000000;">-&gt; (</span>a<span style="color:#000000;">,</span> Node a<span style="color:#000000;">) -&gt; (</span>a<span style="color:#000000;">,</span> Node a<span style="color:#000000;">)</span>
<span style="color:#555555;">   55 </span>purify t <span style="color:#000000;">(</span>tag<span style="color:#000000;">,</span> wires<span style="color:#000000;">) = (</span>tag<span style="color:#000000;">,</span> <span style="color:#000000;font-weight:bold;">filter</span> <span style="color:#000000;">(\(</span>a<span style="color:#000000;">,</span>_<span style="color:#000000;">)-&gt;</span>a <span style="color:#000000;">/=</span> t<span style="color:#000000;">)</span> wires<span style="color:#000000;">)</span>
<span style="color:#555555;">   56 </span>
<span style="color:#555555;">   57 </span>buildFromTags <span style="color:#000000;">:: (</span>Ord a<span style="color:#000000;">) =&gt;</span> Node a <span style="color:#000000;">-&gt;</span> Conductance <span style="color:#000000;">-&gt; (</span>a<span style="color:#000000;">,</span> a<span style="color:#000000;">) -&gt;</span> Network a
<span style="color:#555555;">   58 </span>buildFromTags n s <span style="color:#000000;">(</span>x<span style="color:#000000;">,</span> y<span style="color:#000000;">) =</span> buildPart x y <span style="color:#000000;">(</span>s <span style="color:#000000;">*</span> xx <span style="color:#000000;">*</span> yy<span style="color:#000000;">)</span>
<span style="color:#555555;">   59 </span>  <span style="color:#010181;">where</span>
<span style="color:#555555;">   60 </span>    xx <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">fromJust</span> <span style="color:#000000;">.</span> <span style="color:#000000;font-weight:bold;">lookup</span> x <span style="color:#000000;">$</span> n
<span style="color:#555555;">   61 </span>    yy <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">fromJust</span> <span style="color:#000000;">.</span> <span style="color:#000000;font-weight:bold;">lookup</span> y <span style="color:#000000;">$</span> n</pre>
</p>
<p style="text-align:justify;">We can test each of the previously defined networks to see if the algorithm works.</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;"><span style="color:#000000;">*</span>Main<span style="color:#000000;">&gt;</span> solve testSimple <span style="color:#2928ff;">0 1</span>
<span style="color:#2928ff;">5.0</span>
<span style="color:#000000;">*</span>Main<span style="color:#000000;">&gt;</span> solve testSimple<span style="color:#000000;">'</span> <span style="color:#2928ff;">0 1</span>
<span style="color:#2928ff;">3.75</span>
<span style="color:#000000;">*</span>Main<span style="color:#000000;">&gt;</span> solve testSimple<span style="color:#000000;">''</span> <span style="color:#2928ff;">0 1</span>
<span style="color:#2928ff;">40.0</span>
<span style="color:#000000;">*</span>Main<span style="color:#000000;">&gt;</span> solve testSimple<span style="color:#000000;">''</span> <span style="color:#2928ff;">0 2</span>
<span style="color:#2928ff;">42.0</span>
<span style="color:#000000;">*</span>Main<span style="color:#000000;">&gt;</span> solve testTetra <span style="color:#2928ff;">0 3</span>
<span style="color:#2928ff;">0.5</span>
<span style="color:#000000;">*</span>Main<span style="color:#000000;">&gt;</span> solve testSquares <span style="color:#000000;">(</span><span style="color:#2928ff;">0</span><span style="color:#000000;">,</span> <span style="color:#2928ff;">0</span><span style="color:#000000;">) (</span><span style="color:#2928ff;">1</span><span style="color:#000000;">,</span> <span style="color:#2928ff;">2</span><span style="color:#000000;">)</span>
<span style="color:#2928ff;">1.4000000000000001</span></pre>
</p>
<p style="text-align:justify;">Now, the solution to the XKCD&#8217;s problem. First, we define the network: we will take a finite case, between <img src='http://s0.wp.com/latex.php?latex=%28-n%2C-n%29&amp;bg=fff&amp;fg=444444&amp;s=0' alt='(-n,-n)' title='(-n,-n)' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%28n%2Cn%29&amp;bg=fff&amp;fg=444444&amp;s=0' alt='(n,n)' title='(n,n)' class='latex' />. By increasing n we will get closer and closer to the actual result (as you will see later, the convergence is pretty good).</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;"><span style="color:#555555;">  155 </span>build <span style="color:#000000;">::</span> <span style="color:#830000;">Int</span> <span style="color:#000000;">-&gt;</span> Network <span style="color:#000000;">(</span><span style="color:#830000;">Int</span><span style="color:#000000;">,</span> <span style="color:#830000;">Int</span><span style="color:#000000;">)</span>
<span style="color:#555555;">  156 </span>build n <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">foldl</span> joinParts <span style="color:#000000;">[] .</span> <span style="color:#000000;font-weight:bold;">map</span> <span style="color:#000000;">(\(</span>x<span style="color:#000000;">,</span> y<span style="color:#000000;">) -&gt;</span> buildPart x y <span style="color:#2928ff;">1</span><span style="color:#000000;">) $</span> <span style="color:#000000;font-weight:bold;">list</span>
<span style="color:#555555;">  157 </span>  <span style="color:#010181;">where</span>
<span style="color:#555555;">  158 </span>    <span style="color:#000000;font-weight:bold;">list</span> <span style="color:#000000;">= [(</span>x<span style="color:#000000;">,</span> y<span style="color:#000000;">) |</span> x <span style="color:#000000;">&lt;-</span> points<span style="color:#000000;">,</span> y <span style="color:#000000;">&lt;-</span> points<span style="color:#000000;">,</span> x `neigh` y<span style="color:#000000;">,</span> x <span style="color:#000000;">&lt;</span> y<span style="color:#000000;">]</span>
<span style="color:#555555;">  159 </span>    points <span style="color:#000000;">=</span> fillBelow n
<span style="color:#555555;">  160 </span>
<span style="color:#555555;">  161 </span>fillBelow <span style="color:#000000;">::</span> <span style="color:#830000;">Int</span> <span style="color:#000000;">-&gt; [(</span><span style="color:#830000;">Int</span><span style="color:#000000;">,</span> <span style="color:#830000;">Int</span><span style="color:#000000;">)]</span>
<span style="color:#555555;">  162 </span>fillBelow n <span style="color:#000000;">= [(</span>x<span style="color:#000000;">,</span> y<span style="color:#000000;">) |</span> x <span style="color:#000000;">&lt;- [-</span>n <span style="color:#000000;">..</span> n<span style="color:#000000;">],</span> y <span style="color:#000000;">&lt;- [-</span>n <span style="color:#000000;">..</span>n<span style="color:#000000;">]]</span>
<span style="color:#555555;">  163 </span>
<span style="color:#555555;">  164 </span>neigh <span style="color:#000000;">:: (</span><span style="color:#830000;">Int</span><span style="color:#000000;">,</span> <span style="color:#830000;">Int</span><span style="color:#000000;">) -&gt; (</span><span style="color:#830000;">Int</span><span style="color:#000000;">,</span> <span style="color:#830000;">Int</span><span style="color:#000000;">) -&gt;</span> <span style="color:#830000;">Bool</span>
<span style="color:#555555;">  165 </span>neigh <span style="color:#000000;">(</span>a<span style="color:#000000;">,</span> b<span style="color:#000000;">) (</span>c<span style="color:#000000;">,</span> d<span style="color:#000000;">) =</span> <span style="color:#000000;font-weight:bold;">abs</span> <span style="color:#000000;">(</span>a <span style="color:#000000;">-</span> c<span style="color:#000000;">) +</span> <span style="color:#000000;font-weight:bold;">abs</span> <span style="color:#000000;">(</span>b <span style="color:#000000;">-</span> d<span style="color:#000000;">) ==</span> <span style="color:#2928ff;">1</span> </pre>
</p>
<p style="text-align:center;"><a href="http://twitpic.com/42yp0i" title="Share photos on twitter with Twitpic"><img src="http://twitpic.com/show/thumb/42yp0i.png" width="150" height="150" alt="Share photos on twitter with Twitpic" /></a><br />
An instance of the XKCD problem</p>
<p style="text-align:justify;">Thus, to solve the problem for one iteration we have</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;"><span style="color:#555555;">  167 </span>solveXKCD <span style="color:#000000;">::</span> <span style="color:#830000;">Int</span> <span style="color:#000000;">-&gt;</span> Resistance
<span style="color:#555555;">  168 </span>solveXKCD n <span style="color:#000000;">=</span> solve <span style="color:#000000;">(</span>build n<span style="color:#000000;">) (</span><span style="color:#2928ff;">0</span><span style="color:#000000;">,</span> <span style="color:#2928ff;">0</span><span style="color:#000000;">) (</span><span style="color:#2928ff;">1</span><span style="color:#000000;">,</span> <span style="color:#2928ff;">2</span><span style="color:#000000;">)</span> </pre>
</p>
<p style="text-align:justify;">And the <code>main</code>, used for printing the results</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:auto;"><span style="color:#555555;">  170 </span>main <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">mapM</span> <span style="color:#000000;">(</span><span style="color:#000000;font-weight:bold;">print</span> <span style="color:#000000;">. \</span>x <span style="color:#000000;">-&gt; (</span>x<span style="color:#000000;">,</span> solveXKCD x<span style="color:#000000;">)) [</span><span style="color:#2928ff;">3</span><span style="color:#000000;">..]</span> </pre>
</p>
<p style="text-align:justify;">And now the results. I let the program run until it reached a network of size 25 then I stopped it and plotted the results.</p>
<p style="text-align:center;"><a href="http://twitpic.com/42yp8z" title="Share photos on twitter with Twitpic"><img src="http://twitpic.com/show/thumb/42yp8z.png" width="150" height="150" alt="Share photos on twitter with Twitpic" /></a><br />
Resistance vs grid size, see the convergence speed</p>
<p style="text-align:justify;">From the plot, it is easy to see that the valid result lies somewhere between <img src='http://s0.wp.com/latex.php?latex=.774&amp;bg=fff&amp;fg=444444&amp;s=0' alt='.774' title='.774' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=.772&amp;bg=fff&amp;fg=444444&amp;s=0' alt='.772' title='.772' class='latex' /> range, just like <a href="http://forums.xkcd.com/viewtopic.php?f=7&amp;t=16104&amp;p=433453&amp;hilit=0356#p433538">the valid answer is</a>.</p>
<p style="text-align:justify;">In the end, I&#8217;d like to relate to <a href="http://forums.xkcd.com/viewtopic.php?f=7&amp;t=16104&amp;p=433453&amp;hilit=0356#p433587">another post</a> from that topic: please add more puzzles like this so that I can do something when I have free time instead of slacking off.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pgraycode.wordpress.com/1004/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pgraycode.wordpress.com/1004/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pgraycode.wordpress.com/1004/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pgraycode.wordpress.com/1004/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pgraycode.wordpress.com/1004/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pgraycode.wordpress.com/1004/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pgraycode.wordpress.com/1004/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pgraycode.wordpress.com/1004/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pgraycode.wordpress.com/1004/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pgraycode.wordpress.com/1004/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pgraycode.wordpress.com/1004/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pgraycode.wordpress.com/1004/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pgraycode.wordpress.com/1004/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pgraycode.wordpress.com/1004/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pgraycode.wordpress.com&amp;blog=6568508&amp;post=1004&amp;subd=pgraycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pgraycode.wordpress.com/2011/02/23/solving-xkcds-nerd-snipping-problem/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9a378ed2410bdabed82883f2615f6b61?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Mithrandir</media:title>
		</media:content>

		<media:content url="http://twitpic.com/show/thumb/42ykth.png" medium="image">
			<media:title type="html">Share photos on twitter with Twitpic</media:title>
		</media:content>

		<media:content url="http://twitpic.com/show/thumb/42yn64.png" medium="image">
			<media:title type="html">Share photos on twitter with Twitpic</media:title>
		</media:content>

		<media:content url="http://twitpic.com/show/thumb/42ynbb.png" medium="image">
			<media:title type="html">Share photos on twitter with Twitpic</media:title>
		</media:content>

		<media:content url="http://twitpic.com/show/thumb/42yngo.png" medium="image">
			<media:title type="html">Share photos on twitter with Twitpic</media:title>
		</media:content>

		<media:content url="http://twitpic.com/show/thumb/42yo5w.png" medium="image">
			<media:title type="html">Share photos on twitter with Twitpic</media:title>
		</media:content>

		<media:content url="http://twitpic.com/show/thumb/42yp0i.png" medium="image">
			<media:title type="html">Share photos on twitter with Twitpic</media:title>
		</media:content>

		<media:content url="http://twitpic.com/show/thumb/42yp8z.png" medium="image">
			<media:title type="html">Share photos on twitter with Twitpic</media:title>
		</media:content>
	</item>
		<item>
		<title>Parsing text in Haskell with Alex</title>
		<link>http://pgraycode.wordpress.com/2011/02/18/parsing-text-in-haskell-with-alex/</link>
		<comments>http://pgraycode.wordpress.com/2011/02/18/parsing-text-in-haskell-with-alex/#comments</comments>
		<pubDate>Fri, 18 Feb 2011 18:50:58 +0000</pubDate>
		<dc:creator>Mithrandir</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[HaCoTeB]]></category>
		<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://pgraycode.wordpress.com/?p=975</guid>
		<description><![CDATA[Recently I remembered that I&#8217;ve started HaCoTeB as a project in which to test text lexing and parsing with Alex and Happy. Things didn&#8217;t work so well and HaCoTeB died without a single Alex specification file written. However, during this little free time I had this week, I tried again to parse a simple text [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pgraycode.wordpress.com&amp;blog=6568508&amp;post=975&amp;subd=pgraycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Recently I remembered that I&#8217;ve started <a href="http://pgraycode.wordpress.com/2010/07/28/enter-hacoteb/">HaCoTeB</a> as a project in which to test text lexing and parsing with <a href="http://www.haskell.org/alex/">Alex</a> and <a href="http://www.haskell.org/happy/">Happy</a>. Things didn&#8217;t work so well and HaCoTeB <a href="http://pgraycode.wordpress.com/2011/01/29/hacoteb-is-dead/">died</a> without a single Alex specification file written.</p>
<p style="text-align:justify;">However, during this little free time I had this week, I tried again to parse a simple text file with Alex.</p>
<p style="text-align:justify;"><span id="more-975"></span></p>
<p style="text-align:justify;">We are given an input file consisting of text and properly enclosed mark-ups like ** for bold text, // for italic text, &#8212; for stroked-out text and __ for underline text (each of them enclosing the text enriched with the format). For example, <code>//this should be in italic//</code> should be an italic text.</p>
<p style="text-align:justify;">In order to do this, we begin be specifying the regular expressions used to split the text into tokens (also known as lexing or scanning) and the actions associated with each expression. First, we define the special symbols, used as a mark-up.</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';"><a name="alexl_21"></a><span style="color:#555555;">00021 </span>
<a name="alexl_22"></a><span style="color:#555555;">00022 </span><span style="color:#838183;font-style:italic;">-- put here all characters used to select format</span>
<a name="alexl_23"></a><span style="color:#555555;">00023 </span><span style="color:#000000;">$</span>formatspec <span style="color:#000000;">= [\*\/</span>_<span style="color:#000000;">\-]</span>
<a name="alexl_24"></a><span style="color:#555555;">00024 </span> </pre>
</p>
<p style="text-align:justify;">Then we specify the expressions and their actions.</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';"><a name="alexl_27"></a><span style="color:#555555;">00027 </span>
<a name="alexl_28"></a><span style="color:#555555;">00028 </span><span style="color:#000000;">[</span>^<span style="color:#000000;">$</span>formatspec<span style="color:#000000;">]+ {</span> takeText <span style="color:#000000;">}</span>
<a name="alexl_29"></a><span style="color:#555555;">00029 </span><span style="color:#000000;">$</span>formatspec <span style="color:#000000;">{</span> takeText <span style="color:#000000;">}</span> <span style="color:#838183;font-style:italic;">-- for single chars (think a * b)</span>
<a name="alexl_30"></a><span style="color:#555555;">00030 </span><span style="color:#000000;">\/{</span><span style="color:#2928ff;">2</span><span style="color:#000000;">} {</span> flipFormat Italic <span style="color:#000000;">}</span>
<a name="alexl_31"></a><span style="color:#555555;">00031 </span><span style="color:#000000;">\*{</span><span style="color:#2928ff;">2</span><span style="color:#000000;">} {</span> flipFormat Bold <span style="color:#000000;">}</span>
<a name="alexl_32"></a><span style="color:#555555;">00032 </span>_<span style="color:#000000;">{</span><span style="color:#2928ff;">2</span><span style="color:#000000;">} {</span> flipFormat Underline <span style="color:#000000;">}</span>
<a name="alexl_33"></a><span style="color:#555555;">00033 </span><span style="color:#000000;">\-{</span><span style="color:#2928ff;">2</span><span style="color:#000000;">} {</span> flipFormat Strikeout <span style="color:#000000;">}</span>
<a name="alexl_34"></a><span style="color:#555555;">00034 </span></pre>
</p>
<p style="text-align:justify;">As you see in <a href="#alexl_29">line 29</a>, we need to handle single appearances of mark-up characters in a separate regular expression. Otherwise, text like &#8220;mark-up&#8221; or &#8220;a * b&#8221; will result in a scan error.</p>
<p style="text-align:justify;">Before defining the action, let&#8217;s look at how the task at hand was solved. Basically, we will gather as much text as possible without including any format specifier into a single value, constructed using takeText. When we encounter a format specifier we change a global state to inform the subsequent takeText call that the gathered string is enriched with a new format. Because that data is not defined anywhere in the Alex templates, we will have to implement it ourselves. To do that, we tell Alex that we want a monadic wrapper with user data</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';"><a name="alexl_18"></a><span style="color:#555555;">00018 </span>
<a name="alexl_19"></a><span style="color:#555555;">00019 </span><span style="color:#838183;font-style:italic;">-- the monadUserState allows for a monadic parser with user data</span>
<a name="alexl_20"></a><span style="color:#555555;">00020 </span><span style="color:#000000;">%</span>wrapper <span style="color:#ff0000;">&quot;monadUserState&quot;</span>
<a name="alexl_21"></a><span style="color:#555555;">00021 </span> </pre>
</p>
<p style="text-align:justify;">Then, we define the user data and the initial value for it (don&#8217;t change the name of the following functions, they are called this way from the template)</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';"><a name="alexl_71"></a><span style="color:#555555;">00071 </span><span style="color:#010181;">type</span> AlexUserState <span style="color:#000000;">= [</span>Format<span style="color:#000000;">]</span>
<a name="alexl_72"></a><span style="color:#555555;">00072 </span>
<a name="alexl_73"></a><span style="color:#555555;">00073 </span>alexInitUserState <span style="color:#000000;">= []</span>
<a name="alexl_74"></a><span style="color:#555555;">00074 </span> </pre>
</p>
<p style="text-align:justify;">The Format data is another defined type, its definition may be left as an exercise since it is not relevant here.</p>
<p style="text-align:justify;">Also, although the wrapper is called <em>monadUserState</em> there are no functions defined there to do that. You&#8217;ll have to implement them by hand for now, a mail was sent describing the problem on haskell-cafe, maybe it will be solved soon. (Also, this <a href="http://www.haskell.org/alex/doc/html/wrappers.html">wrapper</a> is not presented in the <a href="http://www.haskell.org/alex/doc/html/index.html">official documentation</a> for Alex, I have found it while trying to solve other problems by looking at the templates).</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';"><a name="alexl_74"></a><span style="color:#555555;">00074 </span>
<a name="alexl_75"></a><span style="color:#555555;">00075 </span>getUserData <span style="color:#000000;">::</span> Alex AlexUserState
<a name="alexl_76"></a><span style="color:#555555;">00076 </span>getUserData <span style="color:#000000;">=</span> Alex <span style="color:#000000;">$ \</span>s<span style="color:#000000;">&#064;</span>AlexState<span style="color:#000000;">{</span>alex_ust<span style="color:#000000;">=</span>udata<span style="color:#000000;">} -&gt;</span> Right <span style="color:#000000;">(</span>s<span style="color:#000000;">,</span> udata<span style="color:#000000;">)</span>
<a name="alexl_77"></a><span style="color:#555555;">00077 </span>
<a name="alexl_78"></a><span style="color:#555555;">00078 </span>setUserData <span style="color:#000000;">::</span> AlexUserState <span style="color:#000000;">-&gt;</span> Alex <span style="color:#000000;">()</span>
<a name="alexl_79"></a><span style="color:#555555;">00079 </span>setUserData udata <span style="color:#000000;">=</span> Alex <span style="color:#000000;">$ \</span>s <span style="color:#000000;">-&gt;</span> Right <span style="color:#000000;">(</span>s<span style="color:#000000;">{</span>alex_ust<span style="color:#000000;">=</span>udata<span style="color:#000000;">}, ())</span>
<a name="alexl_80"></a><span style="color:#555555;">00080 </span> </pre>
</p>
<p style="text-align:justify;">The last thing that you need to define is what should be done when the end of the file  (EOF) is encountered. We will return Nothing, making all actions have return type of <code>Alex (Maybe Token)</code> instead of simply <code>Alex Token</code>.</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';"><a name="alexl_97"></a><span style="color:#555555;">00097 </span>
<a name="alexl_98"></a><span style="color:#555555;">00098 </span><span style="color:#838183;font-style:italic;">{-</span>
<a name="alexl_99"></a><span style="color:#555555;">00099 </span><span style="color:#838183;font-style:italic;">Remember that this function must also return a token!! We have made it to</span>
<a name="alexl_100"></a><span style="color:#555555;">00100 </span><span style="color:#838183;font-style:italic;">return Nothing to enable you to forget about it.</span>
<a name="alexl_101"></a><span style="color:#555555;">00101 </span><span style="color:#838183;font-style:italic;">-}</span>
<a name="alexl_102"></a><span style="color:#555555;">00102 </span>alexEOF <span style="color:#000000;">::</span> Alex <span style="color:#000000;">(</span><span style="color:#830000;">Maybe</span> Token<span style="color:#000000;">)</span>
<a name="alexl_103"></a><span style="color:#555555;">00103 </span>alexEOF <span style="color:#000000;">=</span> <span style="color:#000000;font-weight:bold;">return</span> Nothing
<a name="alexl_104"></a><span style="color:#555555;">00104 </span><span style="color:#000000;">}</span>
<a name="alexl_105"></a><span style="color:#555555;">00105 </span> </pre>
</p>
<p style="text-align:justify;">While this has the advantage that you can use only the good tokens in the Token data definition, it has the disadvantage that two layers of monads are present. Of course, this function can be implemented by each user.</p>
<p style="text-align:justify;">In the end, we need something to start the whole parsing process.  We do this via two functions: one to loop over the text returning it token by token and collecting the results into a list and another to start the whole computation.</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';"><a name="alexl_55"></a><span style="color:#555555;">00055 </span>
<a name="alexl_56"></a><span style="color:#555555;">00056 </span>run <span style="color:#000000;">::</span> <span style="color:#830000;">String</span> <span style="color:#000000;">-&gt;</span> <span style="color:#830000;">Either String</span> <span style="color:#000000;">[</span>Token<span style="color:#000000;">]</span>
<a name="alexl_57"></a><span style="color:#555555;">00057 </span>run content <span style="color:#000000;">=</span> runAlex content <span style="color:#000000;">$</span> loop <span style="color:#000000;">[]</span>
<a name="alexl_58"></a><span style="color:#555555;">00058 </span>
<a name="alexl_59"></a><span style="color:#555555;">00059 </span>loop end <span style="color:#000000;">=</span> <span style="color:#010181;">do</span> <span style="color:#838183;font-style:italic;">--alexMonadScan &gt;&gt;= \t -&gt; loop end &gt;&gt;= \e -&gt; return $ t : e</span>
<a name="alexl_60"></a><span style="color:#555555;">00060 </span>  tok <span style="color:#000000;">&lt;-</span> alexMonadScan<span style="color:#000000;">;</span>
<a name="alexl_61"></a><span style="color:#555555;">00061 </span>  <span style="color:#010181;">case</span> tok <span style="color:#010181;">of</span>
<a name="alexl_62"></a><span style="color:#555555;">00062 </span>    Nothing <span style="color:#000000;">-&gt;</span> <span style="color:#000000;font-weight:bold;">return</span> end
<a name="alexl_63"></a><span style="color:#555555;">00063 </span>    Just t <span style="color:#000000;">-&gt;</span> loop end <span style="color:#000000;">&gt;&gt;= \</span>e <span style="color:#000000;">-&gt;</span> <span style="color:#000000;font-weight:bold;">return</span> <span style="color:#000000;">$</span> t <span style="color:#000000;">:</span> e
<a name="alexl_64"></a><span style="color:#555555;">00064 </span> </pre>
</p>
<p style="text-align:justify;">The end result looks like this:</p>
<p style="text-align:justify;">
<pre style="color:#000000;background-color:#ffffff;font-size:10pt;font-family:'Courier New';overflow:scroll;">[Text [Bold] "This is a bolded paragraph, every word here should be bold ",Text [Strikeout,Bold] "and this part should be stroked out",Text [Bold] " while ",Text [Italic,Bold] "this should be in italic",Text [Bold] " and ",Text [Underline,Bold] "this underlined",Text [Bold] ". ",Text [Italic,Strikeout,Underline,Bold] "This part should have all of the attributes",Text [Bold] "."]</pre>
</p>
<p style="text-align:justify;">However, this is not yet complete, I guess that no user would want to look at something like that. A conversion function is needed but that is not in the scope of this article. I&#8217;ll return on this topic later.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pgraycode.wordpress.com/975/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pgraycode.wordpress.com/975/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pgraycode.wordpress.com/975/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pgraycode.wordpress.com/975/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pgraycode.wordpress.com/975/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pgraycode.wordpress.com/975/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pgraycode.wordpress.com/975/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pgraycode.wordpress.com/975/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pgraycode.wordpress.com/975/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pgraycode.wordpress.com/975/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pgraycode.wordpress.com/975/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pgraycode.wordpress.com/975/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pgraycode.wordpress.com/975/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pgraycode.wordpress.com/975/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pgraycode.wordpress.com&amp;blog=6568508&amp;post=975&amp;subd=pgraycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pgraycode.wordpress.com/2011/02/18/parsing-text-in-haskell-with-alex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9a378ed2410bdabed82883f2615f6b61?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Mithrandir</media:title>
		</media:content>
	</item>
		<item>
		<title>A semester of coding</title>
		<link>http://pgraycode.wordpress.com/2011/02/02/a-semester-of-coding/</link>
		<comments>http://pgraycode.wordpress.com/2011/02/02/a-semester-of-coding/#comments</comments>
		<pubDate>Wed, 02 Feb 2011 19:28:05 +0000</pubDate>
		<dc:creator>Mithrandir</dc:creator>
				<category><![CDATA[Artificial Intelligence]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Reviews]]></category>
		<category><![CDATA[AI]]></category>
		<category><![CDATA[compilers]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://pgraycode.wordpress.com/?p=969</guid>
		<description><![CDATA[The last semester was a very busy one at my faculty. This is the main reason why I failed to update this blog. However, now that I have free time again, I&#8217;ll present some of the coding assignments that I&#8217;ve done during the last semester. I had four homeworks at Compilers Design. All in Java. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pgraycode.wordpress.com&amp;blog=6568508&amp;post=969&amp;subd=pgraycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">The last semester was a very busy one at my faculty. This is the main reason why I failed to update this blog. However, now that I have free time again, I&#8217;ll present some of the coding assignments that I&#8217;ve done during the last semester.</p>
<p style="text-align:justify;"><span id="more-969"></span></p>
<p style="text-align:justify;">I had four homeworks at Compilers Design. All in Java. In the end, we obtained a compiler for <a href="http://en.wikipedia.org/wiki/Cool_(programming_language)">Cool</a>. However, I&#8217;ll not insist on this. It was fun on some parts, and bad on others. Maybe I&#8217;ll try to change something in the next year.</p>
<p style="text-align:justify;">Then, I had 3 assignments at Artificial Intelligence. One was a simple game in Prolog and is not relevant to this post. The second one was to write a simple liniar planner for a general class of problems in Scheme. As the code on <a href="https://github.com/mihaimaruseac/SchemePlanner">github</a> shows, I ended up writing some Scheme functions to turn it into a more Haskel-ish language. It was a fun project where I have written many folds and maps.</p>
<p style="text-align:justify;">Lastly, the third assignment was more liberal: we had to choose from a series of titles and languages. I decided to implement a multi-agent Minesweeper game in Haskell. The code is now public on <a href="https://github.com/mihaimaruseac/HsMinesweeper">github</a> (though it&#8217;s yet to be completed on the GUI, it has a few bugs and has some design choices which I&#8217;ll want to rewrite). However, this was the most exciting project I&#8217;ve been working on this year (until now, I will have to do a Valgrind port in the next semester as part of the Graduation Assignment &#8211; don&#8217;t know if this is the exact name).</p>
<p style="text-align:justify;">Recently, I&#8217;ve tried to implement the KamikazeRace game on Facebook for an Android device. The code is also on <a href="https://github.com/mihaimaruseac/KamikazeRace">github</a> but it was only tested locally (though some of my colleagues are testing it on real devices and bugs are filled in back to me).</p>
<p style="text-align:justify;">Speaking of bugs, recently I&#8217;ve moved several of ROSEdu&#8217;s repositories from Trac instances to Redmine ones. When this move will be completed and the articles describing the ROSEdu&#8217;s projects will be available I&#8217;ll come back on this subject.</p>
<p style="text-align:justify;">Right now, I have one more exam to take and I&#8217;m going to learn for it in a few moments. Or sleep :)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pgraycode.wordpress.com/969/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pgraycode.wordpress.com/969/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pgraycode.wordpress.com/969/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pgraycode.wordpress.com/969/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pgraycode.wordpress.com/969/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pgraycode.wordpress.com/969/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pgraycode.wordpress.com/969/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pgraycode.wordpress.com/969/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pgraycode.wordpress.com/969/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pgraycode.wordpress.com/969/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pgraycode.wordpress.com/969/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pgraycode.wordpress.com/969/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pgraycode.wordpress.com/969/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pgraycode.wordpress.com/969/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pgraycode.wordpress.com&amp;blog=6568508&amp;post=969&amp;subd=pgraycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pgraycode.wordpress.com/2011/02/02/a-semester-of-coding/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9a378ed2410bdabed82883f2615f6b61?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Mithrandir</media:title>
		</media:content>
	</item>
		<item>
		<title>HaCoTeB is dead</title>
		<link>http://pgraycode.wordpress.com/2011/01/29/hacoteb-is-dead/</link>
		<comments>http://pgraycode.wordpress.com/2011/01/29/hacoteb-is-dead/#comments</comments>
		<pubDate>Sat, 29 Jan 2011 18:04:54 +0000</pubDate>
		<dc:creator>Mithrandir</dc:creator>
				<category><![CDATA[Ideas]]></category>
		<category><![CDATA[HaCoTeB]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://pgraycode.wordpress.com/?p=967</guid>
		<description><![CDATA[A while ago I started a project which promised to make posting code snippets on wordpress.com blogs easy as pie. However, things didn&#8217;t worked well in the end. What happened was that I was dissatisfied with the first version and dreamed a new version which was a total remade of the project. However, I haven&#8217;t [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pgraycode.wordpress.com&amp;blog=6568508&amp;post=967&amp;subd=pgraycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">A while ago I started a <a href="http://pgraycode.wordpress.com/2010/07/28/enter-hacoteb/">project</a> which promised to make posting code snippets on wordpress.com blogs easy as pie. However, things didn&#8217;t worked well in the end.</p>
<p style="text-align:justify;"><span id="more-967"></span>What happened was that I was dissatisfied with the first version and dreamed a new version which was a total remade of the project. However, I haven&#8217;t had the needed time to finish it before school homeworks began so it entered into a complete sleep, just like the blogs and the mmorpg that I was playing.</p>
<p style="text-align:justify;">Now, when I see that I have a little more free time before I&#8217;ll have to really start my Graduation Thesis, I find myself thinking about what I wanted this project to be. I find it difficult to restart working on it right now while I am constantly flooded into a stream of ideas. Thus, I have two choices: either I&#8217;ll choose to stay and remember what I planned for that project and I&#8217;ll start working on it or I&#8217;ll start a new project from the most promising idea while trying to note the others somewhere where I know I could find them later (and in an understandable format, I&#8217;ve lost too many ideas because I didn&#8217;t remember what was I thinking when they poped up into my head).</p>
<p style="text-align:justify;">I&#8217;ll chose the second option because it still leaves open the first alternative. Maybe, someday, I&#8217;ll restart work on this project or a new idea will make it obsolete (like that one thing which is bugging me for a few days, more on this later).</p>
<p style="text-align:justify;">I&#8217;ll be back soon with a more optimistic post, when I&#8217;ll have time again to post.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pgraycode.wordpress.com/967/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pgraycode.wordpress.com/967/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pgraycode.wordpress.com/967/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pgraycode.wordpress.com/967/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pgraycode.wordpress.com/967/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pgraycode.wordpress.com/967/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pgraycode.wordpress.com/967/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pgraycode.wordpress.com/967/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pgraycode.wordpress.com/967/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pgraycode.wordpress.com/967/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pgraycode.wordpress.com/967/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pgraycode.wordpress.com/967/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pgraycode.wordpress.com/967/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pgraycode.wordpress.com/967/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pgraycode.wordpress.com&amp;blog=6568508&amp;post=967&amp;subd=pgraycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pgraycode.wordpress.com/2011/01/29/hacoteb-is-dead/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9a378ed2410bdabed82883f2615f6b61?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Mithrandir</media:title>
		</media:content>
	</item>
	</channel>
</rss>
