<?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:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>It Seemed Like A Good Idea, At The Time</title>
	<atom:link href="http://blog.willdonnelly.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.willdonnelly.net</link>
	<description>Coding, Mostly</description>
	<lastBuildDate>Fri, 06 Nov 2009 20:12:44 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='blog.willdonnelly.net' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/37c251d80b031b3ae5db9ced8980cb96?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>It Seemed Like A Good Idea, At The Time</title>
		<link>http://blog.willdonnelly.net</link>
	</image>
			<item>
		<title>Solving Logic Grid Puzzles in Haskell</title>
		<link>http://blog.willdonnelly.net/2009/11/06/solving-logic-grid-puzzles-in-haskell/</link>
		<comments>http://blog.willdonnelly.net/2009/11/06/solving-logic-grid-puzzles-in-haskell/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 19:48:05 +0000</pubDate>
		<dc:creator>Will Donnelly</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://willdonnelly.wordpress.com/?p=314</guid>
		<description><![CDATA[The Zebra Puzzle is a decades-old exercise in deductive logic. Unfortunately, I lack the patience to sit down and solve this kind of puzzle. So in this post, we&#8217;re going to cheat by teaching Haskell to solve it for us.
&#62; import Data.Maybe
&#62; import Control.Monad

We will take our cue from this solution written in SWI Prolog, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.willdonnelly.net&blog=4676114&post=314&subd=willdonnelly&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>The <a href="http://en.wikipedia.org/wiki/Zebra_Puzzle">Zebra Puzzle</a> is a decades-old exercise in deductive logic. Unfortunately, I lack the patience to sit down and solve this kind of puzzle. So in this post, we&#8217;re going to cheat by teaching Haskell to solve it for us.</p>
<pre style="font-size:9px;"><code>&gt; <span style="color:blue;font-weight:bold;">import</span> Data.Maybe
&gt; <span style="color:blue;font-weight:bold;">import</span> Control.Monad
</code></pre>
<p>We will take our cue from <a href="http://sandbox.rulemaker.net/ngps/119">this solution</a> written in SWI Prolog, and encode our puzzle as a set of constraints that our solving code must satisfy.</p>
<p>An &#8216;entry&#8217; is a single entity in the solution. In the Zebra puzzle, these are going to be the individual houses. For maximum generality, we&#8217;re just going to represent all the properties of the entries with strings.</p>
<p>An answer to the puzzle is a collection of several entities which together satisfy all of the rules set forth in the puzzle description.</p>
<pre style="font-size:9px;"><code>&gt; <span style="color:blue;font-weight:bold;">type</span> Entry  <span style="color:red;">=</span> <span style="color:red;">[</span>String<span style="color:red;">]</span>
&gt; <span style="color:blue;font-weight:bold;">type</span> Answer <span style="color:red;">=</span> <span style="color:red;">[</span>Entry<span style="color:red;">]</span>
</code></pre>
<p>I was going to explain the rule types at this point, but the explanation ended up being a lot harder to understand than just looking at the rules for this puzzle, so we&#8217;ll do that instead.</p>
<p>The first four rules simply teach our solver about numbers. After these rules are satisfied, the entries in the solution will be numbered one through five.</p>
<pre style="font-size:9px;"><code>&gt; rules <span style="color:red;">=</span>
&gt;   <span style="color:red;">[</span> Follows  <span style="color:red;">[</span> <span style="color:teal;">"1"</span><span style="color:red;">,</span> <span style="color:teal;">""</span><span style="color:red;">,</span>        <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span>              <span style="color:red;">]</span>
&gt;              <span style="color:red;">[</span> <span style="color:teal;">"2"</span><span style="color:red;">,</span> <span style="color:teal;">""</span><span style="color:red;">,</span>        <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span>              <span style="color:red;">]</span>
&gt;   <span style="color:red;">,</span> Follows  <span style="color:red;">[</span> <span style="color:teal;">"2"</span><span style="color:red;">,</span> <span style="color:teal;">""</span><span style="color:red;">,</span>        <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span>              <span style="color:red;">]</span>
&gt;              <span style="color:red;">[</span> <span style="color:teal;">"3"</span><span style="color:red;">,</span> <span style="color:teal;">""</span><span style="color:red;">,</span>        <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span>              <span style="color:red;">]</span>
&gt;   <span style="color:red;">,</span> Follows  <span style="color:red;">[</span> <span style="color:teal;">"3"</span><span style="color:red;">,</span> <span style="color:teal;">""</span><span style="color:red;">,</span>        <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span>              <span style="color:red;">]</span>
&gt;              <span style="color:red;">[</span> <span style="color:teal;">"4"</span><span style="color:red;">,</span> <span style="color:teal;">""</span><span style="color:red;">,</span>        <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span>              <span style="color:red;">]</span>
&gt;   <span style="color:red;">,</span> Follows  <span style="color:red;">[</span> <span style="color:teal;">"4"</span><span style="color:red;">,</span> <span style="color:teal;">""</span><span style="color:red;">,</span>        <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span>              <span style="color:red;">]</span>
&gt;              <span style="color:red;">[</span> <span style="color:teal;">"5"</span><span style="color:red;">,</span> <span style="color:teal;">""</span><span style="color:red;">,</span>        <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span>              <span style="color:red;">]</span>
</code></pre>
<p>Then we specify all of the clues that make up the puzzle itself. These are given in order, so it should be easy to see the correspondence with the clues listed in the wikipedia article</p>
<pre style="font-size:9px;"><code>&gt;   <span style="color:red;">,</span> Literal  <span style="color:red;">[</span> <span style="color:teal;">""</span><span style="color:red;">,</span>  <span style="color:teal;">"England"</span><span style="color:red;">,</span> <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">"Red"</span><span style="color:red;">,</span>    <span style="color:teal;">""</span>              <span style="color:red;">]</span>
&gt;   <span style="color:red;">,</span> Literal  <span style="color:red;">[</span> <span style="color:teal;">""</span><span style="color:red;">,</span>  <span style="color:teal;">"Spain"</span><span style="color:red;">,</span>   <span style="color:teal;">"Dog"</span><span style="color:red;">,</span>    <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span>              <span style="color:red;">]</span>
&gt;   <span style="color:red;">,</span> Literal  <span style="color:red;">[</span> <span style="color:teal;">""</span><span style="color:red;">,</span>  <span style="color:teal;">""</span><span style="color:red;">,</span>        <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">"Coffee"</span><span style="color:red;">,</span> <span style="color:teal;">"Green"</span><span style="color:red;">,</span>  <span style="color:teal;">""</span>              <span style="color:red;">]</span>
&gt;   <span style="color:red;">,</span> Literal  <span style="color:red;">[</span> <span style="color:teal;">""</span><span style="color:red;">,</span>  <span style="color:teal;">"Ukraine"</span><span style="color:red;">,</span> <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">"Tea"</span><span style="color:red;">,</span>    <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span>              <span style="color:red;">]</span>
&gt;   <span style="color:red;">,</span> Follows  <span style="color:red;">[</span> <span style="color:teal;">""</span><span style="color:red;">,</span>  <span style="color:teal;">""</span><span style="color:red;">,</span>        <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">"Green"</span><span style="color:red;">,</span>  <span style="color:teal;">""</span>              <span style="color:red;">]</span>
&gt;              <span style="color:red;">[</span> <span style="color:teal;">""</span><span style="color:red;">,</span>  <span style="color:teal;">""</span><span style="color:red;">,</span>        <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">"Ivory"</span><span style="color:red;">,</span>  <span style="color:teal;">""</span>              <span style="color:red;">]</span>
&gt;   <span style="color:red;">,</span> Literal  <span style="color:red;">[</span> <span style="color:teal;">""</span><span style="color:red;">,</span>  <span style="color:teal;">""</span><span style="color:red;">,</span>        <span style="color:teal;">"Snails"</span><span style="color:red;">,</span> <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">"Old Gold"</span>      <span style="color:red;">]</span>
&gt;   <span style="color:red;">,</span> Literal  <span style="color:red;">[</span> <span style="color:teal;">""</span><span style="color:red;">,</span>  <span style="color:teal;">""</span><span style="color:red;">,</span>        <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">"Yellow"</span><span style="color:red;">,</span> <span style="color:teal;">"Kools"</span>         <span style="color:red;">]</span>
&gt;   <span style="color:red;">,</span> Literal  <span style="color:red;">[</span> <span style="color:teal;">"3"</span><span style="color:red;">,</span> <span style="color:teal;">""</span><span style="color:red;">,</span>        <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">"Milk"</span><span style="color:red;">,</span>   <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span>              <span style="color:red;">]</span>
&gt;   <span style="color:red;">,</span> Literal  <span style="color:red;">[</span> <span style="color:teal;">"1"</span><span style="color:red;">,</span> <span style="color:teal;">"Norway"</span><span style="color:red;">,</span>  <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span>              <span style="color:red;">]</span>
&gt;   <span style="color:red;">,</span> Adjacent <span style="color:red;">[</span> <span style="color:teal;">""</span><span style="color:red;">,</span>  <span style="color:teal;">""</span><span style="color:red;">,</span>        <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">"Chesterfields"</span> <span style="color:red;">]</span>
&gt;              <span style="color:red;">[</span> <span style="color:teal;">""</span><span style="color:red;">,</span>  <span style="color:teal;">""</span><span style="color:red;">,</span>        <span style="color:teal;">"Fox"</span><span style="color:red;">,</span>    <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span>              <span style="color:red;">]</span>
&gt;   <span style="color:red;">,</span> Adjacent <span style="color:red;">[</span> <span style="color:teal;">""</span><span style="color:red;">,</span>  <span style="color:teal;">""</span><span style="color:red;">,</span>        <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">"Kools"</span>         <span style="color:red;">]</span>
&gt;              <span style="color:red;">[</span> <span style="color:teal;">""</span><span style="color:red;">,</span>  <span style="color:teal;">""</span><span style="color:red;">,</span>        <span style="color:teal;">"Horse"</span><span style="color:red;">,</span>  <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span>              <span style="color:red;">]</span>
&gt;   <span style="color:red;">,</span> Literal  <span style="color:red;">[</span> <span style="color:teal;">""</span><span style="color:red;">,</span>  <span style="color:teal;">""</span><span style="color:red;">,</span>        <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">"Juice"</span><span style="color:red;">,</span>  <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">"Lucky Strike"</span>  <span style="color:red;">]</span>
&gt;   <span style="color:red;">,</span> Literal  <span style="color:red;">[</span> <span style="color:teal;">""</span><span style="color:red;">,</span>  <span style="color:teal;">"Japan"</span><span style="color:red;">,</span>   <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">"Parliaments"</span>   <span style="color:red;">]</span>
&gt;   <span style="color:red;">,</span> Adjacent <span style="color:red;">[</span> <span style="color:teal;">""</span><span style="color:red;">,</span>  <span style="color:teal;">"Norway"</span><span style="color:red;">,</span>  <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span>              <span style="color:red;">]</span>
&gt;              <span style="color:red;">[</span> <span style="color:teal;">""</span><span style="color:red;">,</span>  <span style="color:teal;">""</span><span style="color:red;">,</span>        <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">"Blue"</span><span style="color:red;">,</span>   <span style="color:teal;">""</span>              <span style="color:red;">]</span>
</code></pre>
<p>Unfortunately, one drink and one animal are missing from the rules as stated, so here we just inform the solver &#8220;someone drinks water&#8221; and &#8220;someone owns a zebra&#8221;</p>
<pre style="font-size:9px;"><code>&gt;   <span style="color:red;">,</span> Literal  <span style="color:red;">[</span> <span style="color:teal;">""</span><span style="color:red;">,</span>  <span style="color:teal;">""</span><span style="color:red;">,</span>        <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">"Water"</span><span style="color:red;">,</span>  <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span>              <span style="color:red;">]</span>
&gt;   <span style="color:red;">,</span> Literal  <span style="color:red;">[</span> <span style="color:teal;">""</span><span style="color:red;">,</span>  <span style="color:teal;">""</span><span style="color:red;">,</span>        <span style="color:teal;">"Zebra"</span><span style="color:red;">,</span>  <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span><span style="color:red;">,</span>       <span style="color:teal;">""</span>              <span style="color:red;">]</span>
&gt;   <span style="color:red;">]</span>
</code></pre>
<p>So we have three kinds of rules, for which we&#8217;ll need a data definition. By now it should be self-evident how each of these work</p>
<pre style="font-size:9px;"><code>&gt; <span style="color:blue;font-weight:bold;">data</span> Rule <span style="color:red;">=</span> Literal  Entry
&gt;           <span style="color:red;">|</span> Adjacent Entry Entry
&gt;           <span style="color:red;">|</span> Follows  Entry Entry
&gt;           <span style="color:blue;font-weight:bold;">deriving</span> <span style="color:red;">(</span>Show<span style="color:red;">)</span>
</code></pre>
<p>At this point, we have a nice, declarative specification of what a solution will look like, and we need to write the code to solve for it. The key to solving the puzzle efficiently is to realize that each rule is effectively describing some small portion of a possible answer, with empty strings representing unknown values. What we need next is a way to expand a rule into a list of all those answers that it represents</p>
<pre style="font-size:9px;"><code>&gt; expandRule <span style="color:red;">::</span> Int <span style="color:red;">-&gt;</span> Rule <span style="color:red;">-&gt;</span> <span style="color:red;">[</span>Answer<span style="color:red;">]</span>
&gt; expandRule n <span style="color:red;">(</span>Literal   a <span style="color:red;">)</span> <span style="color:red;">=</span> <span style="color:red;">[</span> expand n <span style="color:red;">[</span> a <span style="color:red;">]</span> x <span style="color:red;">|</span> x <span style="color:red;">&lt;-</span> <span style="color:red;">[</span><span class="hs-num">0</span> <span style="color:red;">..</span> n <span style="color:green;">-</span> <span class="hs-num">1</span><span style="color:red;">]</span> <span style="color:red;">]</span>
&gt; expandRule n <span style="color:red;">(</span>Follows  a b<span style="color:red;">)</span> <span style="color:red;">=</span> <span style="color:red;">[</span> expand n <span style="color:red;">[</span>a<span style="color:red;">,</span>b<span style="color:red;">]</span> x <span style="color:red;">|</span> x <span style="color:red;">&lt;-</span> <span style="color:red;">[</span><span class="hs-num">0</span> <span style="color:red;">..</span> n <span style="color:green;">-</span> <span class="hs-num">2</span><span style="color:red;">]</span> <span style="color:red;">]</span>
&gt; expandRule n <span style="color:red;">(</span>Adjacent a b<span style="color:red;">)</span> <span style="color:red;">=</span> concat <span style="color:red;">[</span>e $ Follows a b<span style="color:red;">,</span> e $ Follows b a<span style="color:red;">]</span>
&gt;   <span style="color:blue;font-weight:bold;">where</span> e <span style="color:red;">=</span> expandRule n
&gt;
&gt; expand <span style="color:red;">::</span> Int <span style="color:red;">-&gt;</span> <span style="color:red;">[</span>Entry<span style="color:red;">]</span> <span style="color:red;">-&gt;</span> Int <span style="color:red;">-&gt;</span> <span style="color:red;">[</span>Entry<span style="color:red;">]</span>
&gt; expand n rs<span style="color:red;">@</span><span style="color:red;">(</span>r:<span style="color:blue;font-weight:bold;">_</span><span style="color:red;">)</span> x <span style="color:red;">=</span> replicate x blank ++ rs ++ replicate <span style="color:red;">(</span>n <span style="color:green;">-</span> x <span style="color:green;">-</span> <span class="hs-num">1</span><span style="color:red;">)</span> blank
&gt;   <span style="color:blue;font-weight:bold;">where</span> blank <span style="color:red;">=</span> replicate <span style="color:red;">(</span>length r<span style="color:red;">)</span> <span style="color:teal;">""</span>
</code></pre>
<p>As we go about solving the puzzle, we will at all times have a collection of answers the solver currently knows to be possible, and a set of answer fragments resulting from expanding the rule we&#8217;re currently trying to satisfy. What we need is a way to test every possible combination of the old answers with the new answer fragments.</p>
<p>Our solution will make use of the nondeterminism monad, called the &#8220;list&#8221; monad by the unenlightened. As we iterate over the rules, we will expand each one in turn, test every possible combination with the old answers, and then filter out the impossible ones.</p>
<p>At first, this will cause a combinatorial explosion of possible answers, but as new rules are added, we will eventually reach a point where each additional rule manages only to decrease the number of possible solutions, until there is only one remaining.</p>
<pre style="font-size:9px;"><code>&gt; applyRules <span style="color:red;">::</span> Answer <span style="color:red;">-&gt;</span> <span style="color:red;">[</span>Rule<span style="color:red;">]</span> <span style="color:red;">-&gt;</span> <span style="color:red;">[</span>Answer<span style="color:red;">]</span>
&gt; applyRules answer rules <span style="color:red;">=</span> foldM applyRule answer rules
&gt;   <span style="color:blue;font-weight:bold;">where</span> applyRule a r <span style="color:red;">=</span> catMaybes <span style="color:red;">[</span>overlay a x <span style="color:red;">|</span> x <span style="color:red;">&lt;-</span> expandRule <span style="color:red;">(</span>length a<span style="color:red;">)</span> r<span style="color:red;">]</span>
</code></pre>
<p>From the definition of <code style="font-size:9px;">applyRules</code>, it is clear that our overlay operation needs to have type <code style="font-size:9px;">Answer -&gt; Answer -&gt; Maybe Answer</code>. If any two answers are both defined and different from each other, we return <code style="font-size:9px;">Nothing</code>, and otherwise we return the most defined of the two fields.</p>
<pre style="font-size:9px;"><code>&gt; overlay <span style="color:red;">::</span> Answer <span style="color:red;">-&gt;</span> Answer <span style="color:red;">-&gt;</span> Maybe Answer
&gt; overlay old new <span style="color:red;">=</span> sequence $ zipWith overlay' old new
&gt;   <span style="color:blue;font-weight:bold;">where</span> overlay' old new <span style="color:red;">=</span> sequence $ zipWith overlay'' old new
&gt;         overlay'' <span style="color:teal;">""</span> <span style="color:teal;">""</span>  <span style="color:red;">=</span> Just <span style="color:teal;">""</span>
&gt;         overlay'' <span style="color:teal;">""</span> n   <span style="color:red;">=</span> Just n
&gt;         overlay'' o  <span style="color:teal;">""</span>  <span style="color:red;">=</span> Just o
&gt;         overlay'' o  n
&gt;           <span style="color:red;">|</span> o == n       <span style="color:red;">=</span> Just o
&gt;           <span style="color:red;">|</span> otherwise    <span style="color:red;">=</span> Nothing
</code></pre>
<p>Before any constraints are applied, the answer is entirely undefined, so the process of solving consists simply of applying all the rules to an initial empty answer, and seeing what results. In this case, there is only one answer, but removing one or more rules from the list can make other solutions equally valid.</p>
<pre style="font-size:9px;"><code>&gt; main <span style="color:red;">=</span> showAnswers . applyRules emptyAnswer $ rules
&gt;   <span style="color:blue;font-weight:bold;">where</span> emptyAnswer <span style="color:red;">=</span> replicate <span class="hs-num">5</span> . replicate <span class="hs-num">6</span> $ <span style="color:teal;">""</span>
&gt;         showAnswers <span style="color:red;">=</span> mapM_ $ mapM_ print
</code></pre>
<p>To see how each additional rule changes the set of possible answers, you can try something like &#8220;<code style="font-size:9px;">mapM_ print . applyRules emptyAnswer . take ## $ rules</code>&#8221; in GHCi, for all the numbers between 1 and 20.</p>
<p>(This post is Literate Haskell, meaning that it can be copied and pasted in its entirety into a <code style="font-size:9px;">*.lhs</code> file, and then run with <code style="font-size:9px;">runhaskell</code>)</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/willdonnelly.wordpress.com/314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/willdonnelly.wordpress.com/314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/willdonnelly.wordpress.com/314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/willdonnelly.wordpress.com/314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/willdonnelly.wordpress.com/314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/willdonnelly.wordpress.com/314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/willdonnelly.wordpress.com/314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/willdonnelly.wordpress.com/314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/willdonnelly.wordpress.com/314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/willdonnelly.wordpress.com/314/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.willdonnelly.net&blog=4676114&post=314&subd=willdonnelly&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.willdonnelly.net/2009/11/06/solving-logic-grid-puzzles-in-haskell/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7a273a4628e287a480462eb09844efeb?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Will Donnelly</media:title>
		</media:content>
	</item>
		<item>
		<title>Brian&#8217;s (Purely) Functional Brain</title>
		<link>http://blog.willdonnelly.net/2009/10/14/brians-purely-functional-brain/</link>
		<comments>http://blog.willdonnelly.net/2009/10/14/brians-purely-functional-brain/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 02:16:39 +0000</pubDate>
		<dc:creator>Will Donnelly</dc:creator>
				<category><![CDATA[haskell]]></category>
		<category><![CDATA[anythingyoucandohaskellcandobetter]]></category>
		<category><![CDATA[brians brain]]></category>

		<guid isPermaLink="false">http://blog.willdonnelly.net/?p=293</guid>
		<description><![CDATA[So about a week ago I came across an interesting post in which the author implemented the Brian&#8217;s Brain cellular automaton in 67 lines of Clojure. Not about to let my favorite language be outdone, I thought I&#8217;d see how well Haskell would do with the same task.
Then I was kept horribly busy for a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.willdonnelly.net&blog=4676114&post=293&subd=willdonnelly&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>So about a week ago I came across <a href="http://blog.bestinclass.dk/index.php/2009/10/brians-functional-brain/">an interesting post</a> in which the author implemented the <a href="http://en.wikipedia.org/wiki/Brian's_Brain">Brian&#8217;s Brain</a> cellular automaton in 67 lines of Clojure. Not about to let <a title="Haskell: My Language Can Beat Up Your Language" href="http://haskell.org/">my favorite language</a> be outdone, I thought I&#8217;d see how well Haskell would do with the same task.</p>
<p>Then I was kept horribly busy for a week by schoolwork, so a couple of days ago I started playing around with the problem. The results? Not too shabby!</p>
<p>So first of all, we&#8217;ll be needing some imports. Since (for some odd reason) Haskell requires all imports up front, and since this blog post is supposed to be <a href="http://www.haskell.org/haskellwiki/Literate_programming">Literate Haskell</a>, you&#8217;ll have to just trust me that we&#8217;ll need these for now:</p>
<pre style="font-family:Consolas, Monaco, Monospace;font-size:10px;"><span style="color:#00ffff;">&gt;</span> <span style="text-decoration:underline;">import</span> Data<span style="color:#00ffff;">.</span>Array             <span style="color:#0000ff;font-style:italic;">-- Used to store the world state for processing</span>
<span style="color:#00ffff;">&gt;</span> <span style="text-decoration:underline;">import</span> System<span style="color:#00ffff;">.</span>Random          <span style="color:#0000ff;font-style:italic;">-- Used to generate the initial random world</span>
<span style="color:#00ffff;">&gt;</span> <span style="text-decoration:underline;">import</span> Control<span style="color:#00ffff;">.</span>Monad          <span style="color:#0000ff;font-style:italic;">-- Used for some fancy looping constructs</span>
<span style="color:#00ffff;">&gt;</span> <span style="text-decoration:underline;">import</span> Control<span style="color:#00ffff;">.</span>Concurrent     <span style="color:#0000ff;font-style:italic;">-- Used to fork the quit event handler</span>
<span style="color:#00ffff;">&gt;</span> <span style="text-decoration:underline;">import</span> Graphics<span style="color:#00ffff;">.</span>UI<span style="color:#00ffff;">.</span>SDL <span style="text-decoration:underline;">as</span> SDL <span style="color:#0000ff;font-style:italic;">-- Used to draw the pretty pictures</span>
<span style="color:#00ffff;">&gt;</span> <span style="text-decoration:underline;">import</span> Control<span style="color:#00ffff;">.</span>Parallel<span style="color:#00ffff;">.</span>Strategies</pre>
<p><span style="background-color:#ffffff;">Cells can be either On, Dying, or Off:</span></p>
<pre style="font-family:Consolas, Monaco, Monospace;font-size:10px;"><span style="color:#00ffff;">&gt;</span> <span style="text-decoration:underline;">data</span> Cell  <span style="color:#ff0000;">=</span> Off <span style="color:#ff0000;">|</span> Dying <span style="color:#ff0000;">|</span> On <span style="text-decoration:underline;">deriving</span> <span style="color:#00ffff;">(</span>Eq<span style="color:#00ffff;">,</span> Enum<span style="color:#00ffff;">)
</span></pre>
<p>For convenience, let&#8217;s define some constants:</p>
<pre style="font-family:Consolas, Monaco, Monospace;font-size:10px;"><span style="color:#00ffff;">&gt;</span> worldX   <span style="color:#ff0000;">=</span> <span style="color:#ff00ff;">90</span>                <span style="color:#0000ff;font-style:italic;">-- The horizontal size of the world</span>
<span style="color:#00ffff;">&gt;</span> worldY   <span style="color:#ff0000;">=</span> <span style="color:#ff00ff;">90</span>                <span style="color:#0000ff;font-style:italic;">-- The vertical size of the world</span>
<span style="color:#00ffff;">&gt;</span> cellSize <span style="color:#ff0000;">=</span> <span style="color:#ff00ff;">8</span>                 <span style="color:#0000ff;font-style:italic;">-- The overall size of a cell</span>
<span style="color:#00ffff;">&gt;</span> border   <span style="color:#ff0000;">=</span> <span style="color:#ff00ff;">1</span>                 <span style="color:#0000ff;font-style:italic;">-- The border width between cells</span>
<span style="color:#00ffff;">&gt;</span> screenX  <span style="color:#ff0000;">=</span> worldX <span style="color:#00ffff;">*</span> cellSize <span style="color:#0000ff;font-style:italic;">-- The horizontal size of the world, in pixels</span>
<span style="color:#00ffff;">&gt;</span> screenY  <span style="color:#ff0000;">=</span> worldY <span style="color:#00ffff;">*</span> cellSize <span style="color:#0000ff;font-style:italic;">-- The vertical size of the world, in pixels</span>
<span style="color:#00ffff;">&gt;</span> fillSize <span style="color:#ff0000;">=</span> cellSize <span style="color:#0000ff;font-style:italic;">-</span> border <span style="color:#0000ff;font-style:italic;">-- The size of the filled area in each cell
</span></pre>
<p>Cells progress from On to Dying to Off, and they turn on only when they have exactly two live neighbors.</p>
<pre style="font-family:Consolas, Monaco, Monospace;font-size:10px;"><span style="color:#00ffff;">&gt;</span> stepCell <span style="color:#00ffff;">(</span>On<span style="color:#00ffff;">,</span>    <span style="text-decoration:underline;">_</span><span style="color:#00ffff;">)</span> <span style="color:#ff0000;">=</span> Dying  <span style="color:#0000ff;font-style:italic;">-- Live cells always start to die</span>
<span style="color:#00ffff;">&gt;</span> stepCell <span style="color:#00ffff;">(</span>Dying<span style="color:#00ffff;">,</span> <span style="text-decoration:underline;">_</span><span style="color:#00ffff;">)</span> <span style="color:#ff0000;">=</span> Off    <span style="color:#0000ff;font-style:italic;">-- Dying cells always turn off</span>
<span style="color:#00ffff;">&gt;</span> stepCell <span style="color:#00ffff;">(</span>Off<span style="color:#00ffff;">,</span>   <span style="color:#ff00ff;">2</span><span style="color:#00ffff;">)</span> <span style="color:#ff0000;">=</span> On     <span style="color:#0000ff;font-style:italic;">-- If a dead cell has 2 live neighbors, turn on</span>
<span style="color:#00ffff;">&gt;</span> stepCell <span style="color:#00ffff;">(</span>Off<span style="color:#00ffff;">,</span>   <span style="text-decoration:underline;">_</span><span style="color:#00ffff;">)</span> <span style="color:#ff0000;">=</span> Off    <span style="color:#0000ff;font-style:italic;">--   Otherwise, just stay turned off
</span></pre>
<p>Since we know from the rules that we&#8217;ll need the ability to count a cell&#8217;s live neighbors, let&#8217;s get that out of the way next.</p>
<pre style="font-family:Consolas, Monaco, Monospace;font-size:10px;"><span style="color:#00ffff;">&gt;</span> getPeers world <span style="color:#00ffff;">(</span>x<span style="color:#00ffff;">,</span>y<span style="color:#00ffff;">)</span> <span style="color:#ff0000;">=</span> <span style="color:#00ffff;">(</span>world <span style="color:#00ffff;">!</span> <span style="color:#00ffff;">(</span>x<span style="color:#00ffff;">,</span>y<span style="color:#00ffff;">)</span><span style="color:#00ffff;">,</span> length <span style="color:#00ffff;">.</span> filter <span style="color:#00ffff;">(</span><span style="color:#00ffff;">==</span> On<span style="color:#00ffff;">)</span> <span style="color:#00ffff;">$</span> neighbors<span style="color:#00ffff;">)</span>
<span style="color:#00ffff;">&gt;</span>   <span style="text-decoration:underline;">where</span> neighbors    <span style="color:#ff0000;">=</span> <span style="color:#ff0000;">[</span>getCell x y <span style="color:#ff0000;">|</span> x <span style="color:#ff0000;">&lt;-</span> <span style="color:#ff0000;">[</span>x<span style="color:#0000ff;font-style:italic;">-</span><span style="color:#ff00ff;">1</span> <span style="color:#ff0000;">..</span> x<span style="color:#00ffff;">+</span><span style="color:#ff00ff;">1</span><span style="color:#ff0000;">]</span><span style="color:#00ffff;">,</span> y <span style="color:#ff0000;">&lt;-</span> <span style="color:#ff0000;">[</span>y<span style="color:#0000ff;font-style:italic;">-</span><span style="color:#ff00ff;">1</span> <span style="color:#ff0000;">..</span> y<span style="color:#00ffff;">+</span><span style="color:#ff00ff;">1</span><span style="color:#ff0000;">]</span><span style="color:#ff0000;">]</span>
<span style="color:#00ffff;">&gt;</span>         getCell x y  <span style="color:#ff0000;">=</span> world <span style="color:#00ffff;">!</span> <span style="color:#00ffff;">(</span>clip worldX x<span style="color:#00ffff;">,</span> clip worldY y<span style="color:#00ffff;">)</span>
<span style="color:#00ffff;">&gt;</span>         clip max val <span style="color:#ff0000;">|</span> val <span style="color:#00ffff;">&lt;</span>  <span style="color:#ff00ff;">1</span>  <span style="color:#ff0000;">=</span> clip max <span style="color:#00ffff;">$</span> val <span style="color:#00ffff;">+</span> max <span style="color:#0000ff;font-style:italic;">-</span> <span style="color:#ff00ff;">1</span>
<span style="color:#00ffff;">&gt;</span>                      <span style="color:#ff0000;">|</span> val <span style="color:#00ffff;">&gt;</span> max <span style="color:#ff0000;">=</span> clip max <span style="color:#00ffff;">$</span> val <span style="color:#0000ff;font-style:italic;">-</span> max <span style="color:#00ffff;">+</span> <span style="color:#ff00ff;">1</span>
<span style="color:#00ffff;">&gt;</span>                      <span style="color:#ff0000;">|</span> otherwise <span style="color:#ff0000;">=</span> val</pre>
<p><span style="background-color:#ffffff;">So now we have all the pieces to progress from one world state to the next. For each position in the array, we need to look up all its neighbors, count the live ones, and then pass that data to the <code>stepCell</code> function. The helper function <code>indexArray</code> creates an array of cell indices. We map over this array to generate new values for each cell.</span></p>
<p><span style="background-color:#ffffff;">The <code>`using` parArr rwhnf</code>&#8216;  is some Haskell magic which causes the array to be evaluated in parallel:</span></p>
<pre style="font-family:Consolas, Monaco, Monospace;font-size:10px;"><span style="color:#00ffff;">&gt;</span> indexArray x y <span style="color:#ff0000;">=</span> listArray <span style="color:#00ffff;">(</span><span style="color:#00ffff;">(</span><span style="color:#ff00ff;">1</span><span style="color:#00ffff;">,</span><span style="color:#ff00ff;">1</span><span style="color:#00ffff;">)</span><span style="color:#00ffff;">,</span><span style="color:#00ffff;">(</span>x<span style="color:#00ffff;">,</span>y<span style="color:#00ffff;">)</span><span style="color:#00ffff;">)</span> <span style="color:#ff0000;">[</span><span style="color:#00ffff;">(</span>a<span style="color:#00ffff;">,</span>b<span style="color:#00ffff;">)</span> <span style="color:#ff0000;">|</span> a <span style="color:#ff0000;">&lt;-</span> <span style="color:#ff0000;">[</span><span style="color:#ff00ff;">1</span><span style="color:#ff0000;">..</span>x<span style="color:#ff0000;">]</span><span style="color:#00ffff;">,</span> b <span style="color:#ff0000;">&lt;-</span> <span style="color:#ff0000;">[</span><span style="color:#ff00ff;">1</span><span style="color:#ff0000;">..</span>y<span style="color:#ff0000;">]</span><span style="color:#ff0000;">]</span>
<span style="color:#00ffff;">&gt;</span> stepWorld w    <span style="color:#ff0000;">=</span> newWorld <span style="color:#00ffff;">`using`</span> parArr rwhnf
<span style="color:#00ffff;">&gt;</span>   <span style="text-decoration:underline;">where</span> newWorld <span style="color:#ff0000;">=</span> fmap <span style="color:#00ffff;">(</span>stepCell <span style="color:#00ffff;">.</span> getPeers w<span style="color:#00ffff;">)</span> <span style="color:#00ffff;">$</span> indexArray worldX worldY</pre>
<p>Now we have all we need to run a simulation, but it&#8217;s not quite enough if you insist on getting some pretty pictures. For my fancy display purposes, I happen to like SDL.</p>
<p>The main function initializes SDL, generates a random initial state, produces an infinite list of future world states from that, and then draws each of the states in turn:</p>
<pre style="font-family:Consolas, Monaco, Monospace;font-size:10px;"><span style="color:#00ffff;">&gt;</span> main <span style="color:#ff0000;">=</span> <span style="text-decoration:underline;">do</span> rng <span style="color:#ff0000;">&lt;-</span> newStdGen
<span style="color:#00ffff;">&gt;</span>           SDL<span style="color:#00ffff;">.</span>init <span style="color:#ff0000;">[</span>SDL<span style="color:#00ffff;">.</span>InitVideo<span style="color:#ff0000;">]</span>
<span style="color:#00ffff;">&gt;</span>           SDL<span style="color:#00ffff;">.</span>setCaption <span style="color:#ff00ff;">"Brian's Purely Functional Brain"</span> <span style="color:#ff00ff;">"Brian's Brain"</span>
<span style="color:#00ffff;">&gt;</span>           surface <span style="color:#ff0000;">&lt;-</span> SDL<span style="color:#00ffff;">.</span>setVideoMode screenX screenY <span style="color:#ff00ff;">24</span> <span style="color:#ff0000;">[</span>SDL<span style="color:#00ffff;">.</span>DoubleBuf<span style="color:#ff0000;">]</span>
<span style="color:#00ffff;">&gt;</span>           forkIO <span style="color:#00ffff;">.</span> forever <span style="color:#00ffff;">$</span> waitEvent <span style="color:#00ffff;">&gt;&gt;=</span> <span style="color:#ff0000;">\</span>e <span style="color:#ff0000;">-&gt;</span> when <span style="color:#00ffff;">(</span>e <span style="color:#00ffff;">==</span> Quit<span style="color:#00ffff;">)</span> quit
<span style="color:#00ffff;">&gt;</span>           mapM <span style="color:#00ffff;">(</span>drawWorld surface<span style="color:#00ffff;">)</span> <span style="color:#00ffff;">(</span>iterate stepWorld <span style="color:#00ffff;">$</span> world rng<span style="color:#00ffff;">)</span>
<span style="color:#00ffff;">&gt;</span>   <span style="text-decoration:underline;">where</span> world <span style="color:#ff0000;">=</span> listArray <span style="color:#00ffff;">(</span><span style="color:#00ffff;">(</span><span style="color:#ff00ff;">1</span><span style="color:#00ffff;">,</span><span style="color:#ff00ff;">1</span><span style="color:#00ffff;">)</span><span style="color:#00ffff;">,</span><span style="color:#00ffff;">(</span>worldX<span style="color:#00ffff;">,</span>worldY<span style="color:#00ffff;">)</span><span style="color:#00ffff;">)</span> <span style="color:#00ffff;">.</span> map toEnum <span style="color:#00ffff;">.</span> randomRs <span style="color:#00ffff;">(</span><span style="color:#ff00ff;">0</span><span style="color:#00ffff;">,</span><span style="color:#ff00ff;">2</span><span style="color:#00ffff;">)</span></pre>
<p>And our world drawing function is positively boring. We map over each combination of X and Y values, draw each one, and then flip the resulting image on-screen:</p>
<pre style="font-family:Consolas, Monaco, Monospace;font-size:10px;"><span style="color:#00ffff;">&gt;</span> drawWorld s w <span style="color:#ff0000;">=</span> <span style="text-decoration:underline;">do</span> sequence <span style="color:#ff0000;">[</span>draw x y <span style="color:#ff0000;">|</span> x <span style="color:#ff0000;">&lt;-</span> <span style="color:#ff0000;">[</span><span style="color:#ff00ff;">1</span><span style="color:#ff0000;">..</span>worldX<span style="color:#ff0000;">]</span><span style="color:#00ffff;">,</span> y <span style="color:#ff0000;">&lt;-</span> <span style="color:#ff0000;">[</span><span style="color:#ff00ff;">1</span><span style="color:#ff0000;">..</span>worldY<span style="color:#ff0000;">]</span><span style="color:#ff0000;">]</span>
<span style="color:#00ffff;">&gt;</span>                    SDL<span style="color:#00ffff;">.</span>flip s
<span style="color:#00ffff;">&gt;</span>   <span style="text-decoration:underline;">where</span> draw x y <span style="color:#ff0000;">=</span> SDL<span style="color:#00ffff;">.</span>fillRect s <span style="color:#00ffff;">(</span>Just rect<span style="color:#00ffff;">)</span> <span style="color:#00ffff;">.</span> color <span style="color:#00ffff;">$</span> w <span style="color:#00ffff;">!</span> <span style="color:#00ffff;">(</span>x<span style="color:#00ffff;">,</span>y<span style="color:#00ffff;">)</span>
<span style="color:#00ffff;">&gt;</span>           <span style="text-decoration:underline;">where</span> rect        <span style="color:#ff0000;">=</span> SDL<span style="color:#00ffff;">.</span>Rect <span style="color:#00ffff;">(</span>scale x<span style="color:#00ffff;">)</span> <span style="color:#00ffff;">(</span>scale y<span style="color:#00ffff;">)</span> fillSize fillSize
<span style="color:#00ffff;">&gt;</span>                 scale n     <span style="color:#ff0000;">=</span> <span style="color:#00ffff;">(</span>n <span style="color:#0000ff;font-style:italic;">-</span> <span style="color:#ff00ff;">1</span><span style="color:#00ffff;">)</span> <span style="color:#00ffff;">*</span> cellSize
<span style="color:#00ffff;">&gt;</span>                 color On    <span style="color:#ff0000;">=</span> SDL<span style="color:#00ffff;">.</span>Pixel <span style="color:#ff00ff;">0x00FFFFFF</span>
<span style="color:#00ffff;">&gt;</span>                 color Dying <span style="color:#ff0000;">=</span> SDL<span style="color:#00ffff;">.</span>Pixel <span style="color:#ff00ff;">0x00888888</span>
<span style="color:#00ffff;">&gt;</span>                 color Off   <span style="color:#ff0000;">=</span> SDL<span style="color:#00ffff;">.</span>Pixel <span style="color:#ff00ff;">0x00000000</span></pre>
<p>To take full advantage of the parallelism in this program, you&#8217;ll need to compile with the threaded runtime and run it on multiple OS threads.</p>
<pre style="font-family:Consolas, Monaco, Monospace;font-size:10px;">ghc -O3 -threaded --make BriansBrain.hs
./BriansBrain +RTS -N2</pre>
<p>And then just sit back and watch the mesmerising patterns.</p>
<p>This code is available on <a href="http://hackage.haskell.org/package/brians-brain">Hackage</a> and <a href="http://github.com/willdonnelly/brians-brain">GitHub</a>.</p>
<div id="attachment_306" class="wp-caption alignleft" style="width: 310px"><a href="http://willdonnelly.files.wordpress.com/2009/10/best-screen.png"><img class="size-medium wp-image-306" title="Brian's Purely Functional Brain" src="http://willdonnelly.files.wordpress.com/2009/10/best-screen.png?w=300&#038;h=187" alt="Pretty Colors" width="300" height="187" /></a><p class="wp-caption-text">Pretty Colors</p></div>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/willdonnelly.wordpress.com/293/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/willdonnelly.wordpress.com/293/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/willdonnelly.wordpress.com/293/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/willdonnelly.wordpress.com/293/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/willdonnelly.wordpress.com/293/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/willdonnelly.wordpress.com/293/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/willdonnelly.wordpress.com/293/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/willdonnelly.wordpress.com/293/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/willdonnelly.wordpress.com/293/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/willdonnelly.wordpress.com/293/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.willdonnelly.net&blog=4676114&post=293&subd=willdonnelly&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.willdonnelly.net/2009/10/14/brians-purely-functional-brain/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7a273a4628e287a480462eb09844efeb?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Will Donnelly</media:title>
		</media:content>

		<media:content url="http://willdonnelly.files.wordpress.com/2009/10/best-screen.png?w=300" medium="image">
			<media:title type="html">Brian's Purely Functional Brain</media:title>
		</media:content>
	</item>
		<item>
		<title>Haskell While Loop</title>
		<link>http://blog.willdonnelly.net/2009/09/15/haskell-while-loop/</link>
		<comments>http://blog.willdonnelly.net/2009/09/15/haskell-while-loop/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 02:48:44 +0000</pubDate>
		<dc:creator>Will Donnelly</dc:creator>
				<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://blog.willdonnelly.net/?p=285</guid>
		<description><![CDATA[In the past week, while working on three completely unrelated Haskell projects, I have found myself in need of a while loop. So I came up with this one:
-- &#124; For some reason Control.Monad doesn't provide a 'while'
--   function, even though it has a 'forever' function.
while :: Monad m =&#62; m Bool -&#62; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.willdonnelly.net&blog=4676114&post=285&subd=willdonnelly&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>In the past week, while working on three completely unrelated Haskell projects, I have found myself in need of a while loop. So I came up with this one:</p>
<pre>-- | For some reason Control.Monad doesn't provide a 'while'
--   function, even though it has a 'forever' function.
while :: Monad m =&gt; m Bool -&gt; m a -&gt; m ()
while predicate action = do
    b &lt;- predicate
    if b then action &gt;&gt; while predicate action
         else return ()</pre>
<p>Hoogle shows no functions with that type signature, and I couldn&#8217;t find anything in the documentation that seemed to fit the bill. This seems like a rather fundamental function for getting stuff done, is there something I&#8217;m missing?</p>
<p>EDIT: The most code-golfed version I have yet come up with is `<code>while p a = p &gt;&gt;= flip when (a &gt;&gt; while p a)</code>` can anyone improve on that?</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/willdonnelly.wordpress.com/285/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/willdonnelly.wordpress.com/285/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/willdonnelly.wordpress.com/285/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/willdonnelly.wordpress.com/285/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/willdonnelly.wordpress.com/285/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/willdonnelly.wordpress.com/285/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/willdonnelly.wordpress.com/285/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/willdonnelly.wordpress.com/285/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/willdonnelly.wordpress.com/285/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/willdonnelly.wordpress.com/285/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.willdonnelly.net&blog=4676114&post=285&subd=willdonnelly&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.willdonnelly.net/2009/09/15/haskell-while-loop/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7a273a4628e287a480462eb09844efeb?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Will Donnelly</media:title>
		</media:content>
	</item>
		<item>
		<title>The Magic Dot</title>
		<link>http://blog.willdonnelly.net/2009/08/24/the-magic-dot/</link>
		<comments>http://blog.willdonnelly.net/2009/08/24/the-magic-dot/#comments</comments>
		<pubDate>Mon, 24 Aug 2009 16:45:02 +0000</pubDate>
		<dc:creator>Will Donnelly</dc:creator>
				<category><![CDATA[haskell]]></category>
		<category><![CDATA[fmap]]></category>
		<category><![CDATA[prelude]]></category>

		<guid isPermaLink="false">http://willdonnelly.wordpress.com/?p=279</guid>
		<description><![CDATA[So, reading about functors the other day, I noticed something interesting. Specifically, I noticed that in the case of functions, fmap is equivalent to the composition operation. I think fmap is possibly the coolest function in all of Haskell, so it annoys me when it requires six characters just to use it as an infix [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.willdonnelly.net&blog=4676114&post=279&subd=willdonnelly&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>So, reading about functors the other day, I noticed something interesting. Specifically, I noticed that in the case of functions, <code>fmap</code> is equivalent to the composition operation. I think <code>fmap</code> is possibly the coolest function in all of Haskell, so it annoys me when it requires six characters just to use it as an infix operator.</p>
<p>So I present what is, character-for-character, the coolest Haskell trick I have seen so far:</p>
<pre>import Prelude hiding ((.))
import Control.Monad.Instances
a . b = a `fmap` b
infixr 9 .</pre>
<p>And now, all sorts of fun little tricks are possible, like replacing <code>"map succ $ [0..9]"</code> with <code>"succ . [0..9]"</code>, and replacing <code>"getLine &gt;&gt;= return . (+1) . read"</code> with <code>"(+1) . read . getLine"</code></p>
<p><span style="background-color:#ffffff;">And of course, the same function composition magic still works like always.  Maybe it&#8217;s just me, but something this simple, elegant, and fun to use just makes me happy inside.</span></p>
<p><span style="background-color:#ffffff;">But finally, I have to ask: is there any way of specifically declaring that a module *replaces* the standard Prelude (or even better, individual elements of it)? It would be really nice to just type <code>import Prelude.MagicDot</code> and not have to also bother with adding <code>import Prelude hiding ((.))</code></span></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/willdonnelly.wordpress.com/279/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/willdonnelly.wordpress.com/279/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/willdonnelly.wordpress.com/279/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/willdonnelly.wordpress.com/279/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/willdonnelly.wordpress.com/279/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/willdonnelly.wordpress.com/279/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/willdonnelly.wordpress.com/279/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/willdonnelly.wordpress.com/279/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/willdonnelly.wordpress.com/279/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/willdonnelly.wordpress.com/279/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.willdonnelly.net&blog=4676114&post=279&subd=willdonnelly&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.willdonnelly.net/2009/08/24/the-magic-dot/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7a273a4628e287a480462eb09844efeb?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Will Donnelly</media:title>
		</media:content>
	</item>
		<item>
		<title>Useful Shell Trick</title>
		<link>http://blog.willdonnelly.net/2009/08/06/useful-shell-trick/</link>
		<comments>http://blog.willdonnelly.net/2009/08/06/useful-shell-trick/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 03:40:41 +0000</pubDate>
		<dc:creator>Will Donnelly</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://willdonnelly.wordpress.com/?p=272</guid>
		<description><![CDATA[Sometimes, when doing a bunch of data munging in the shell, I want to pass the contents of a file through a shell pipeline, and then directly out into the file again. Unfortunately, doing this the naive way will obliterate the file&#8217;s contents.
So a while back, I saw a neat little utility called &#8220;sponge&#8221; which [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.willdonnelly.net&blog=4676114&post=272&subd=willdonnelly&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Sometimes, when doing a bunch of data munging in the shell, I want to pass the contents of a file through a shell pipeline, and then directly out into the file again. Unfortunately, doing this the naive way will obliterate the file&#8217;s contents.</p>
<p>So a while back, I saw a neat little utility called &#8220;sponge&#8221; which simply buffered data until it ended, and then output it. While useful, this wasn&#8217;t a standard Unix tool, so I couldn&#8217;t very well rely on having it available.</p>
<p>Well today, after literally <strong>minutes</strong> of searching, I have found a command that seems to work just as well:</p>
<pre>tail -n +0</pre>
<p>It outputs all lines in the input, beginning with line zero. Due, I suppose, to the way tail is implemented, it buffers all input before outputting anything.</p>
<p>Not exactly groundbreaking or anything, but a useful little trick to know when playing around in the shell.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/willdonnelly.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/willdonnelly.wordpress.com/272/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/willdonnelly.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/willdonnelly.wordpress.com/272/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/willdonnelly.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/willdonnelly.wordpress.com/272/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/willdonnelly.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/willdonnelly.wordpress.com/272/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/willdonnelly.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/willdonnelly.wordpress.com/272/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.willdonnelly.net&blog=4676114&post=272&subd=willdonnelly&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.willdonnelly.net/2009/08/06/useful-shell-trick/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7a273a4628e287a480462eb09844efeb?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Will Donnelly</media:title>
		</media:content>
	</item>
		<item>
		<title>Git Short URLs</title>
		<link>http://blog.willdonnelly.net/2009/07/20/git-short-urls/</link>
		<comments>http://blog.willdonnelly.net/2009/07/20/git-short-urls/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 19:27:48 +0000</pubDate>
		<dc:creator>Will Donnelly</dc:creator>
				<category><![CDATA[Note To Self]]></category>

		<guid isPermaLink="false">http://willdonnelly.wordpress.com/?p=268</guid>
		<description><![CDATA[I knew for a fact this feature existed, but even so it took me a half hour to track down the syntax for it. To prevent myself ever needing to do that again, I&#8217;m putting it here.
To create a nice, short URL abbreviation for git, such as &#8220;myserver:&#60;project&#62;.git&#8221;, put the following in &#8216;~/.gitconfig&#8217;
[url "ssh://MY-SERVER/~/"]
  [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.willdonnelly.net&blog=4676114&post=268&subd=willdonnelly&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I knew for a fact this feature existed, but even so it took me a half hour to track down the syntax for it. To prevent myself ever needing to do that again, I&#8217;m putting it here.</p>
<p>To create a nice, short URL abbreviation for git, such as &#8220;myserver:&lt;project&gt;.git&#8221;, put the following in &#8216;~/.gitconfig&#8217;</p>
<pre>[url "ssh://MY-SERVER/~/"]
    insteadOf = myserver:</pre>
<p>That&#8217;s all there is to it.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/willdonnelly.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/willdonnelly.wordpress.com/268/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/willdonnelly.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/willdonnelly.wordpress.com/268/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/willdonnelly.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/willdonnelly.wordpress.com/268/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/willdonnelly.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/willdonnelly.wordpress.com/268/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/willdonnelly.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/willdonnelly.wordpress.com/268/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.willdonnelly.net&blog=4676114&post=268&subd=willdonnelly&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.willdonnelly.net/2009/07/20/git-short-urls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7a273a4628e287a480462eb09844efeb?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Will Donnelly</media:title>
		</media:content>
	</item>
		<item>
		<title>Haskell Exceptions</title>
		<link>http://blog.willdonnelly.net/2009/07/18/haskell-exceptions/</link>
		<comments>http://blog.willdonnelly.net/2009/07/18/haskell-exceptions/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 23:36:31 +0000</pubDate>
		<dc:creator>Will Donnelly</dc:creator>
				<category><![CDATA[haskell]]></category>
		<category><![CDATA[error handling]]></category>

		<guid isPermaLink="false">http://willdonnelly.wordpress.com/?p=266</guid>
		<description><![CDATA[Haskell&#8217;s absolute worst feature, in my opinion, is its system of exceptions. I have yet to see a single use of exceptions that wouldn&#8217;t be better served by the use of a &#8220;Maybe&#8221; or an &#8220;Either&#8221; type. Luckily, in a small nod to sanity, Haskell provides &#8216;Control.Exception.Base.try&#8217;, which returns an Either type, with the exception [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.willdonnelly.net&blog=4676114&post=266&subd=willdonnelly&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Haskell&#8217;s absolute worst feature, in my opinion, is its system of exceptions. I have yet to see a single use of exceptions that wouldn&#8217;t be better served by the use of a &#8220;Maybe&#8221; or an &#8220;Either&#8221; type. Luckily, in a small nod to sanity, Haskell provides &#8216;Control.Exception.Base.try&#8217;, which returns an Either type, with the exception left, and the value right.</p>
<p>Once you have that, it becomes easy to implement some sane functionality for exception handling, such as my current favorite functions, &#8216;defaultOnError&#8217; and &#8216;errorToMaybe&#8217;:</p>
<pre>defaultOnError :: a -&gt; IO a -&gt; IO a
defaultOnError d a = do tryValue &lt;- try a
                        case tryValue of
                             Left  _ -&gt; return d
                             Right v -&gt; return v</pre>
<pre>errorToMaybe :: IO a -&gt; IO (Maybe a)
errorToMaybe a = do tryValue &lt;- try a
                    case tryValue of
                         Left  _ -&gt; return $ Nothing
                         Right v -&gt; return $ Just v</pre>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/willdonnelly.wordpress.com/266/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/willdonnelly.wordpress.com/266/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/willdonnelly.wordpress.com/266/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/willdonnelly.wordpress.com/266/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/willdonnelly.wordpress.com/266/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/willdonnelly.wordpress.com/266/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/willdonnelly.wordpress.com/266/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/willdonnelly.wordpress.com/266/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/willdonnelly.wordpress.com/266/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/willdonnelly.wordpress.com/266/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.willdonnelly.net&blog=4676114&post=266&subd=willdonnelly&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.willdonnelly.net/2009/07/18/haskell-exceptions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7a273a4628e287a480462eb09844efeb?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Will Donnelly</media:title>
		</media:content>
	</item>
		<item>
		<title>What the Hell, XDG?</title>
		<link>http://blog.willdonnelly.net/2009/07/18/what-the-hell-xdg/</link>
		<comments>http://blog.willdonnelly.net/2009/07/18/what-the-hell-xdg/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 21:36:23 +0000</pubDate>
		<dc:creator>Will Donnelly</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://willdonnelly.wordpress.com/?p=264</guid>
		<description><![CDATA[Take a look at the XDG Base Directory Specification. It looks good, doesn&#8217;t it? It really makes sense to have some &#8220;official&#8221; directories to store this kind of stuff in. Now look at the default locations listed:
$XDG_CONFIG_HOME &#8211; Looks like they nailed that pretty well. I might rather see some user-editable config files placed directly [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.willdonnelly.net&blog=4676114&post=264&subd=willdonnelly&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Take a look at the <a href="http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html">XDG Base Directory Specification</a>. It looks good, doesn&#8217;t it? It really makes sense to have some &#8220;official&#8221; directories to store this kind of stuff in. Now look at the default locations listed:</p>
<p>$XDG_CONFIG_HOME &#8211; Looks like they nailed that pretty well. I might rather see some user-editable config files placed directly in &#8216;~/.app/&#8217;, but &#8216;~/.config/app&#8217; is alright as a second choice.</p>
<p>$XDG_CACHE_HOME &#8211; Dead on. It is <strong>incredibly</strong> nice to have a single home for all those nasty little cache files that are a little too persistent to place in &#8216;/tmp&#8217;.</p>
<p>$XDG_DATA_HOME &#8211; Okay, it&#8217;s a little bit ugly to store extra data in &#8216;~/.local/share&#8217;, why couldn&#8217;t they just put it in &#8216;~/.local&#8217;? But on the other hand, most applications don&#8217;t even need extra data of that sort anyway, and it will rarely, if ever, be edited by a human, so it&#8217;s at least a little bit acceptable.</p>
<p>$XDG_DATA_DIRS &#8211; Yeah, they&#8217;re back on form for this one. Those two default directories cover basically every app in existence, so not a single fault can be found there.</p>
<p>$XDG_CONFIG_DIRS &#8211; WHAT THE FUCK! Why the hell do they decide every application needs to store their system-wide configuration in a special &#8216;/etc/xdg&#8217; directory? On my system, I count a grand total of <strong>three</strong> applications in there, of which I use <strong>one</strong> and have configured <strong>zero</strong>. For comparison, I count <strong>157</strong> files and directories in &#8216;/etc&#8217;, other than &#8216;/etc/xdg&#8217;. Obviously, people will kind of expect to see their system-wide config files placed directly in &#8216;/etc&#8217;</p>
<p>What reason can there be for that kind of annoyance? When I have to go in and manually edit a system-wide config file, I will look for it in &#8216;/etc&#8217;. It would never even have <strong>occurred</strong> to me to look in &#8216;/etc/xdg&#8217;. At the very least, couldn&#8217;t the default search path have been &#8216;/etc/xdg&#8217;, followed by &#8216;/etc&#8217;?</p>
<p>This kind of trivial little issue weakens the entire specification, because such a seemingly inconsequential little rough edge can completely sour people&#8217;s opinions of the whole document. And it would be a shame if that happened, because I would rather see the majority of these things become common convention.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/willdonnelly.wordpress.com/264/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/willdonnelly.wordpress.com/264/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/willdonnelly.wordpress.com/264/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/willdonnelly.wordpress.com/264/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/willdonnelly.wordpress.com/264/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/willdonnelly.wordpress.com/264/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/willdonnelly.wordpress.com/264/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/willdonnelly.wordpress.com/264/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/willdonnelly.wordpress.com/264/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/willdonnelly.wordpress.com/264/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.willdonnelly.net&blog=4676114&post=264&subd=willdonnelly&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.willdonnelly.net/2009/07/18/what-the-hell-xdg/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7a273a4628e287a480462eb09844efeb?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Will Donnelly</media:title>
		</media:content>
	</item>
		<item>
		<title>Useful Haskell List Function</title>
		<link>http://blog.willdonnelly.net/2009/07/12/useful-haskell-list-function/</link>
		<comments>http://blog.willdonnelly.net/2009/07/12/useful-haskell-list-function/#comments</comments>
		<pubDate>Sun, 12 Jul 2009 20:09:41 +0000</pubDate>
		<dc:creator>Will Donnelly</dc:creator>
				<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://willdonnelly.wordpress.com/?p=262</guid>
		<description><![CDATA[I keep finding myself with a long list of elements, which I need to split into a list of lists of those elements. For example, I need to go from [0,1,2,3,4,5,6,7,8] to [[0,1,2],[3,4,5],[6,7,8]].
So far, the best function I have found to do this is

split :: Int -&#62; [a] -&#62; [[a]]
split n = unfoldr (\r -&#62; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.willdonnelly.net&blog=4676114&post=262&subd=willdonnelly&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I keep finding myself with a long list of elements, which I need to split into a list of lists of those elements. For example, I need to go from <code>[0,1,2,3,4,5,6,7,8]</code> to <code>[[0,1,2],[3,4,5],[6,7,8]]</code>.</p>
<p>So far, the best function I have found to do this is<br />
<code><br />
split :: Int -&gt; [a] -&gt; [[a]]<br />
split n = unfoldr (\r -&gt; case r of [] -&gt; Nothing; x -&gt; Just $ splitAt n x)<br />
</code></p>
<p>But that seems a little bit overcomplicated for such a simple function. Am I missing a better, more obvious solution?</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/willdonnelly.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/willdonnelly.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/willdonnelly.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/willdonnelly.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/willdonnelly.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/willdonnelly.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/willdonnelly.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/willdonnelly.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/willdonnelly.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/willdonnelly.wordpress.com/262/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.willdonnelly.net&blog=4676114&post=262&subd=willdonnelly&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.willdonnelly.net/2009/07/12/useful-haskell-list-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7a273a4628e287a480462eb09844efeb?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Will Donnelly</media:title>
		</media:content>
	</item>
		<item>
		<title>Escaping Strings in a Shell Pipeline</title>
		<link>http://blog.willdonnelly.net/2009/06/25/escaping-strings-in-a-shell-pipeline/</link>
		<comments>http://blog.willdonnelly.net/2009/06/25/escaping-strings-in-a-shell-pipeline/#comments</comments>
		<pubDate>Thu, 25 Jun 2009 22:15:46 +0000</pubDate>
		<dc:creator>Will Donnelly</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[sed]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://willdonnelly.wordpress.com/?p=259</guid>
		<description><![CDATA[A useful little sed script I wrote, which turned out to be much more useful than I expected. It simply takes a list of files, one per line, and outputs that same list, but with all the spaces and special characters escaped. It&#8217;s mainly intended for use right between find and xargs, as in &#8220;find [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.willdonnelly.net&blog=4676114&post=259&subd=willdonnelly&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>A useful little sed script I wrote, which turned out to be much more useful than I expected. It simply takes a list of files, one per line, and outputs that same list, but with all the spaces and special characters escaped. It&#8217;s mainly intended for use right between find and xargs, as in &#8220;find . -name &#8216;asd&#8217; -print | shell-escape | xargs -I % echo %&#8217;</p>
<p>The list of characters is taken from <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_02">IEEE Std 1003.1</a></p>
<pre><span style="color:#007700;">#!/bin/sed -f
#
# Shell-Escape
# A very basic script to escape characters in the shell.
# Mainly intended for use in shell pipelines, right between
# 'find' and 'xargs'</span>

<span style="color:#0000FF;">s</span><span style="color:#ff0000;">@</span>\\<span style="color:#ff0000;">@</span>\\\\<span style="color:#ff0000;">@</span><span style="color:#0000FF;">g</span>
<span style="color:#0000FF;">s</span><span style="color:#ff0000;">@</span> <span style="color:#ff0000;">@</span>\\ <span style="color:#ff0000;">@</span><span style="color:#0000FF;">g</span>
<span style="color:#0000FF;">s</span><span style="color:#ff0000;">@</span>'<span style="color:#ff0000;">@</span>\\'<span style="color:#ff0000;">@</span><span style="color:#0000FF;">g</span>
<span style="color:#0000FF;">s</span><span style="color:#ff0000;">@</span>"<span style="color:#ff0000;">@</span>\\"<span style="color:#ff0000;">@</span><span style="color:#0000FF;">g</span>
<span style="color:#0000FF;">s</span><span style="color:#ff0000;">@</span>(<span style="color:#ff0000;">@</span>\\(<span style="color:#ff0000;">@</span><span style="color:#0000FF;">g</span>
<span style="color:#0000FF;">s</span><span style="color:#ff0000;">@</span>)<span style="color:#ff0000;">@</span>\\)<span style="color:#ff0000;">@</span><span style="color:#0000FF;">g</span>
<span style="color:#0000FF;">s</span><span style="color:#ff0000;">@</span>|<span style="color:#ff0000;">@</span>\\|<span style="color:#ff0000;">@</span><span style="color:#0000FF;">g</span>
<span style="color:#0000FF;">s</span><span style="color:#ff0000;">@</span>&amp;<span style="color:#ff0000;">@</span>\\&amp;<span style="color:#ff0000;">@</span><span style="color:#0000FF;">g</span>
<span style="color:#0000FF;">s</span><span style="color:#ff0000;">@</span>;<span style="color:#ff0000;">@</span>\\;<span style="color:#ff0000;">@</span><span style="color:#0000FF;">g</span>
<span style="color:#0000FF;">s</span><span style="color:#ff0000;">@</span>&lt;<span style="color:#ff0000;">@</span>\\&lt;<span style="color:#ff0000;">@</span><span style="color:#0000FF;">g</span>
<span style="color:#0000FF;">s</span><span style="color:#ff0000;">@</span>&gt;<span style="color:#ff0000;">@</span>\\&gt;<span style="color:#ff0000;">@</span><span style="color:#0000FF;">g</span>
<span style="color:#0000FF;">s</span><span style="color:#ff0000;">@</span>`<span style="color:#ff0000;">@</span>\\`<span style="color:#ff0000;">@</span><span style="color:#0000FF;">g</span></pre>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/willdonnelly.wordpress.com/259/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/willdonnelly.wordpress.com/259/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/willdonnelly.wordpress.com/259/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/willdonnelly.wordpress.com/259/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/willdonnelly.wordpress.com/259/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/willdonnelly.wordpress.com/259/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/willdonnelly.wordpress.com/259/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/willdonnelly.wordpress.com/259/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/willdonnelly.wordpress.com/259/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/willdonnelly.wordpress.com/259/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.willdonnelly.net&blog=4676114&post=259&subd=willdonnelly&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.willdonnelly.net/2009/06/25/escaping-strings-in-a-shell-pipeline/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7a273a4628e287a480462eb09844efeb?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Will Donnelly</media:title>
		</media:content>
	</item>
	</channel>
</rss>