<?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>Bonsai Code</title>
	<atom:link href="http://bonsaicode.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://bonsaicode.wordpress.com</link>
	<description>Art is the elimination of the unnecessary</description>
	<lastBuildDate>Tue, 24 Jan 2012 22:53:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='bonsaicode.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/5392f645850a5af88dcfec9bfaa6cda9?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Bonsai Code</title>
		<link>http://bonsaicode.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://bonsaicode.wordpress.com/osd.xml" title="Bonsai Code" />
	<atom:link rel='hub' href='http://bonsaicode.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Programming Praxis &#8211; A Dozen Lines Of Code</title>
		<link>http://bonsaicode.wordpress.com/2012/01/24/programming-praxis-a-dozen-lines-of-code/</link>
		<comments>http://bonsaicode.wordpress.com/2012/01/24/programming-praxis-a-dozen-lines-of-code/#comments</comments>
		<pubDate>Tue, 24 Jan 2012 22:49:07 +0000</pubDate>
		<dc:creator>Remco Niemeijer</dc:creator>
				<category><![CDATA[Programming Praxis]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[praxis]]></category>
		<category><![CDATA[kata]]></category>
		<category><![CDATA[bonsai]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[life]]></category>
		<category><![CDATA[of]]></category>
		<category><![CDATA[programing]]></category>
		<category><![CDATA[dozen]]></category>
		<category><![CDATA[lines]]></category>

		<guid isPermaLink="false">http://bonsaicode.wordpress.com/?p=856</guid>
		<description><![CDATA[In today&#8217;s Programming Praxis exercise, our goal is to make any program we want, as long as it&#8217;s cool and it fits within 12 lines of code. I decided to make an implementation of Conway&#8217;s Game of Life: it&#8217;s interesting, it&#8217;s visual, it&#8217;s Turing complete&#8230; how much cooler can you get? Let&#8217;s get started, shall [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bonsaicode.wordpress.com&amp;blog=7474313&amp;post=856&amp;subd=bonsaicode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In <a title="Original exercise" href="http://programmingpraxis.com/2012/01/24/a-dozen-lines-of-code/" target="_blank">today&#8217;s</a> Programming Praxis exercise, our goal is to make any program we want, as long as it&#8217;s cool and it fits within 12 lines of code. I decided to make an implementation of Conway&#8217;s Game of Life: it&#8217;s interesting, it&#8217;s visual, it&#8217;s Turing complete&#8230; how much cooler can you get? Let&#8217;s get started, shall we?</p>
<p>First, some imports. I&#8217;m not counting these as lines of code since to me a line of code is something that can contain a bug.</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';"><span style="color:#ff00e5;">import qualified</span> Data<span style="color:#000000;">.</span>List<span style="color:#000000;">.</span>Key <span style="color:#ff00e5;">as</span> K
<span style="color:#ff00e5;">import qualified</span> Data<span style="color:#000000;">.</span>Map <span style="color:#ff00e5;">as</span> M</pre>
<p>First we define the rule that the game of life is based on: cells stay alive if they have 2 or 3 neighbors, and they become alive if they have 3 neighbors.</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">rule <span style="color:#000000;">(</span>cy<span style="color:#000000;">,</span>cx<span style="color:#000000;">)</span> m <span style="color:#000000;">=</span> <span style="color:#0066ff;">elem</span> ns <span style="color:#000000;">$</span> <span style="color:#ff00e5;">if</span> alive <span style="color:#000000;">(</span>cy<span style="color:#000000;">,</span>cx<span style="color:#000000;">)</span> <span style="color:#ff00e5;">then</span> <span style="color:#000000;">[</span><span style="color:#666666;">2</span><span style="color:#000000;">,</span><span style="color:#666666;">3</span><span style="color:#000000;">]</span> <span style="color:#ff00e5;">else</span> <span style="color:#000000;">[</span><span style="color:#666666;">3</span><span style="color:#000000;">]</span> <span style="color:#ff00e5;">where</span>
    ns <span style="color:#000000;">=</span> <span style="color:#ff9900;">sum</span> <span style="color:#000000;">[</span><span style="color:#666666;">1</span> | y <span style="color:#000000;">&lt;- [-</span><span style="color:#666666;">1</span><span style="color:#000000;">.</span><span style="color:#666666;">.1</span><span style="color:#000000;">],</span> x <span style="color:#000000;">&lt;- [-</span><span style="color:#666666;">1</span><span style="color:#000000;">.</span><span style="color:#666666;">.1</span><span style="color:#000000;">], (</span>y<span style="color:#000000;">,</span>x<span style="color:#000000;">) /= (</span><span style="color:#666666;">0</span><span style="color:#000000;">,</span><span style="color:#666666;">0</span><span style="color:#000000;">),</span> alive <span style="color:#000000;">(</span>cy<span style="color:#000000;">+</span>y<span style="color:#000000;">,</span>cx<span style="color:#000000;">+</span>x<span style="color:#000000;">)]</span>
    alive <span style="color:#000000;">c</span><span style="color:#000000;"> =</span> M<span style="color:#000000;">.</span><span style="color:#0066ff;">lookup</span> <span style="color:#000000;">c</span> m <span style="color:#000000;">==</span> Just <span style="color:#000000;">'</span>x<span style="color:#000000;">'</span></pre>
<p>We calculate the next generation of a grid by determining the rectangle that holds the active cells, adding a one-cell border (since that&#8217;s the furthest influence distance) and calculating the new state for each cell in the resulting rectangle.</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">step m <span style="color:#000000;">=</span> <span style="color:#ff00e5;">if</span> <span style="color:#0066ff;">null</span> on <span style="color:#ff00e5;">then</span> M<span style="color:#000000;">.</span><span style="color:#ff9900;">empty</span> <span style="color:#ff00e5;">else</span> M<span style="color:#000000;">.</span>fromList
    <span style="color:#000000;">[((</span>y<span style="color:#000000;">,</span>x<span style="color:#000000;">),</span><span style="color:#ff00e5;">if</span> rule <span style="color:#000000;">(</span>y<span style="color:#000000;">,</span>x<span style="color:#000000;">)</span> m <span style="color:#ff00e5;">then</span> <span style="color:#000000;">'</span>x<span style="color:#000000;">'</span> <span style="color:#ff00e5;">else</span> <span style="color:#000000;">'.')</span> | y <span style="color:#000000;">&lt;-</span> <span style="color:#0066ff;">range fst</span><span style="color:#000000;">,</span> x <span style="color:#000000;">&lt;-</span> <span style="color:#0066ff;">range snd</span><span style="color:#000000;">]</span>
    <span style="color:#ff00e5;">where</span> on <span style="color:#000000;">=</span> M<span style="color:#000000;">.</span>keys <span style="color:#000000;">$</span> M<span style="color:#000000;">.</span><span style="color:#0066ff;">filter</span> <span style="color:#000000;">(== '</span>x<span style="color:#000000;">')</span> m
          <span style="color:#0066ff;">range</span> f <span style="color:#000000;">= [</span><span style="color:#0066ff;">minimum</span> <span style="color:#000000;">(</span><span style="color:#0066ff;">map</span> f on<span style="color:#000000;">) -</span> <span style="color:#666666;">1</span><span style="color:#000000;">..</span><span style="color:#0066ff;">maximum</span> <span style="color:#000000;">(</span><span style="color:#0066ff;">map</span> f on<span style="color:#000000;">) +</span> <span style="color:#666666;">1</span><span style="color:#000000;">]</span></pre>
<p>Next, we need some functions to load and show a generation:</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">load s <span style="color:#000000;">=</span> M<span style="color:#000000;">.</span>fromList <span style="color:#000000;">[((</span>y<span style="color:#000000;">,</span>x<span style="color:#000000;">),</span>c<span style="color:#000000;">)</span> | <span style="color:#000000;">(</span>y<span style="color:#000000;">,</span>l<span style="color:#000000;">)&lt;-</span><span style="color:#0066ff;">zip</span> <span style="color:#000000;">[</span><span style="color:#666666;">0</span><span style="color:#000000;">..] $</span> <span style="color:#0066ff;">lines</span> s<span style="color:#000000;">, (</span>x<span style="color:#000000;">,</span>c<span style="color:#000000;">)&lt;-</span><span style="color:#0066ff;">zip</span> <span style="color:#000000;">[</span><span style="color:#666666;">0</span><span style="color:#000000;">..]</span> l<span style="color:#000000;">]</span>

display <span style="color:#000000;">=</span> <span style="color:#0066ff;">mapM_</span> <span style="color:#000000;">(</span><span style="color:#0066ff;">putStrLn</span> <span style="color:#000000;">.</span> <span style="color:#0066ff;">map snd</span><span style="color:#000000;">) . ([] <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  .</span> K<span style="color:#000000;">.</span><span style="color:#0066ff;">group</span> <span style="color:#000000;">(</span><span style="color:#0066ff;">fst</span> <span style="color:#000000;">.</span> <span style="color:#0066ff;">fst</span><span style="color:#000000;">) .</span> M<span style="color:#000000;">.</span><span style="color:#0066ff;">assocs</span></pre>
<p>The program reads the input from a text file that holds the starting situation, e.g.</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">.....
.xxx.
.x...
..x..
.....</pre>
<p>and then prints all of the subsequent generations until the pattern stabilizes, i.e. it finds two subsequent identical generations.</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">main <span style="color:#000000;">=</span> <span style="color:#0066ff;">mapM_</span> <span style="color:#000000;">(</span>display <span style="color:#000000;">.</span> <span style="color:#0066ff;">snd</span><span style="color:#000000;">) .</span> <span style="color:#0066ff;">takeWhile</span> <span style="color:#000000;">(</span><span style="color:#0066ff;">uncurry</span> <span style="color:#000000;">(/=)) .</span>
    <span style="color:#000000;">(\</span>l <span style="color:#000000;">-&gt;</span> <span style="color:#0066ff;">zip</span> l <span style="color:#000000;">$</span> <span style="color:#0066ff;">tail</span> l<span style="color:#000000;">) .</span> <span style="color:#0066ff;">iterate</span> step <span style="color:#000000;">.</span> load <span style="color:#000000;">=&lt;&lt;</span> <span style="color:#0066ff;">readFile</span> <span style="color:#666666;">"life.txt"</span></pre>
<p>And that&#8217;s it. A cool little program that&#8217;s even one line under our budget of 12.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bonsaicode.wordpress.com/856/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bonsaicode.wordpress.com/856/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bonsaicode.wordpress.com/856/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bonsaicode.wordpress.com/856/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bonsaicode.wordpress.com/856/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bonsaicode.wordpress.com/856/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bonsaicode.wordpress.com/856/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bonsaicode.wordpress.com/856/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bonsaicode.wordpress.com/856/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bonsaicode.wordpress.com/856/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bonsaicode.wordpress.com/856/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bonsaicode.wordpress.com/856/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bonsaicode.wordpress.com/856/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bonsaicode.wordpress.com/856/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bonsaicode.wordpress.com&amp;blog=7474313&amp;post=856&amp;subd=bonsaicode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bonsaicode.wordpress.com/2012/01/24/programming-praxis-a-dozen-lines-of-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/af10cfb68163f74baccb0107d33a207b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Remco Niemeijer</media:title>
		</media:content>
	</item>
		<item>
		<title>Programming Praxis &#8211; McNugget Numbers</title>
		<link>http://bonsaicode.wordpress.com/2011/12/09/programming-praxis-mcnugget-numbers/</link>
		<comments>http://bonsaicode.wordpress.com/2011/12/09/programming-praxis-mcnugget-numbers/#comments</comments>
		<pubDate>Fri, 09 Dec 2011 12:24:45 +0000</pubDate>
		<dc:creator>Remco Niemeijer</dc:creator>
				<category><![CDATA[Programming Praxis]]></category>
		<category><![CDATA[bonsai]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[kata]]></category>
		<category><![CDATA[mcnugget]]></category>
		<category><![CDATA[numbers]]></category>
		<category><![CDATA[praxis]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://bonsaicode.wordpress.com/?p=853</guid>
		<description><![CDATA[In today&#8217;s Programming Praxis exercise, our goal is to determine all the numbers that are not McNugget numbers, i.e. numbers that cannot be created by summing multiples of 6, 9 and 20. Let&#8217;s get started, shall we? A quick import: import Data.List The code is pretty straightforward: just take all the numbers up to 180 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bonsaicode.wordpress.com&amp;blog=7474313&amp;post=853&amp;subd=bonsaicode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In <a title="Original exercise" href="http://programmingpraxis.com/2011/12/09/mcnugget-numbers/" target="_blank">today&#8217;s</a> Programming Praxis exercise, our goal is to determine all the numbers that are not McNugget numbers, i.e. numbers that cannot be created by summing multiples of 6, 9 and 20. Let&#8217;s get started, shall we?</p>
<p>A quick import:</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';"><span style="color:#ff00e5;">import</span> Data<span style="color:#000000;">.</span>List</pre>
<p>The code is pretty straightforward: just take all the numbers up to 180 that cannot be created by a linear combination of 6, 9 and 20.</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">notMcNuggets <span style="color:#000000;">:: [</span><span style="color:#ff9900;font-weight:bold;">Integer</span><span style="color:#000000;">]</span>
notMcNuggets <span style="color:#000000;">= [</span><span style="color:#666666;">1</span><span style="color:#000000;">.</span><span style="color:#666666;">.180</span><span style="color:#000000;">]</span> <span style="color:#ff0000;">\\</span>
    <span style="color:#000000;">[</span>a<span style="color:#000000;">+</span>b<span style="color:#000000;">+</span>c | a <span style="color:#000000;">&lt;- [</span><span style="color:#666666;">0</span><span style="color:#000000;">,</span><span style="color:#666666;">6</span><span style="color:#000000;">.</span><span style="color:#666666;">.180</span><span style="color:#000000;">],</span> b <span style="color:#000000;">&lt;- [</span><span style="color:#666666;">0</span><span style="color:#000000;">,</span><span style="color:#666666;">9</span><span style="color:#000000;">.</span><span style="color:#666666;">.180</span><span style="color:#000000;">-</span>a<span style="color:#000000;">],</span> c <span style="color:#000000;">&lt;- [</span><span style="color:#666666;">0</span><span style="color:#000000;">,</span><span style="color:#666666;">20</span><span style="color:#000000;">.</span><span style="color:#666666;">.180</span><span style="color:#000000;">-</span>a<span style="color:#000000;">-</span>b<span style="color:#000000;">]]</span></pre>
<p>To test whether everything works correctly:</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">main <span style="color:#000000;">::</span> <span style="color:#ff9900;font-weight:bold;">IO</span> <span style="color:#000000;">()</span>
main <span style="color:#000000;">=</span> <span style="color:#0066ff;">print</span> notMcNuggets</pre>
<p>Yup. Nice and simple.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bonsaicode.wordpress.com/853/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bonsaicode.wordpress.com/853/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bonsaicode.wordpress.com/853/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bonsaicode.wordpress.com/853/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bonsaicode.wordpress.com/853/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bonsaicode.wordpress.com/853/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bonsaicode.wordpress.com/853/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bonsaicode.wordpress.com/853/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bonsaicode.wordpress.com/853/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bonsaicode.wordpress.com/853/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bonsaicode.wordpress.com/853/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bonsaicode.wordpress.com/853/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bonsaicode.wordpress.com/853/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bonsaicode.wordpress.com/853/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bonsaicode.wordpress.com&amp;blog=7474313&amp;post=853&amp;subd=bonsaicode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bonsaicode.wordpress.com/2011/12/09/programming-praxis-mcnugget-numbers/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/af10cfb68163f74baccb0107d33a207b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Remco Niemeijer</media:title>
		</media:content>
	</item>
		<item>
		<title>Programming Praxis &#8211; Pascal’s Triangle</title>
		<link>http://bonsaicode.wordpress.com/2011/12/06/programming-praxis-pascals-triangle/</link>
		<comments>http://bonsaicode.wordpress.com/2011/12/06/programming-praxis-pascals-triangle/#comments</comments>
		<pubDate>Tue, 06 Dec 2011 10:19:53 +0000</pubDate>
		<dc:creator>Remco Niemeijer</dc:creator>
				<category><![CDATA[Programming Praxis]]></category>
		<category><![CDATA[bonsai]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[kata]]></category>
		<category><![CDATA[pascal]]></category>
		<category><![CDATA[praxis]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[triangle]]></category>

		<guid isPermaLink="false">http://bonsaicode.wordpress.com/?p=850</guid>
		<description><![CDATA[In today&#8217;s Programming Praxis our task is to neatly display Pascal&#8217;s triangle. Let&#8217;s get started, shall we? A quick import to making printing slightly more convenient: import Text.Printf Calculating Pascal&#8217;s triangle is trivial: pascal :: [[Integer]] pascal = iterate (\prev -&#62; 1 : zipWith (+) prev (tail prev) ++ [1]) [1] To display the triangle [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bonsaicode.wordpress.com&amp;blog=7474313&amp;post=850&amp;subd=bonsaicode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In <a title="Original exercise" href="http://programmingpraxis.com/2011/12/06/pascals-triangle/">today&#8217;s</a> Programming Praxis our task is to neatly display Pascal&#8217;s triangle. Let&#8217;s get started, shall we?</p>
<p>A quick import to making printing slightly more convenient:</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';"><span style="color:#ff00e5;">import</span> Text<span style="color:#000000;">.</span>Printf</pre>
<p>Calculating Pascal&#8217;s triangle is trivial:</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">pascal <span style="color:#000000;">:: [[</span><span style="color:#ff9900;font-weight:bold;">Integer</span><span style="color:#000000;">]]</span>
pascal <span style="color:#000000;">=</span> <span style="color:#0066ff;">iterate</span> <span style="color:#000000;">(\</span>prev <span style="color:#000000;">-&gt;</span> <span style="color:#666666;">1</span> <span style="color:#000000;">:</span> <span style="color:#0066ff;">zipWith</span> <span style="color:#000000;">(+)</span> prev <span style="color:#000000;">(</span><span style="color:#0066ff;">tail</span> prev<span style="color:#000000;">) ++ [</span><span style="color:#666666;">1</span><span style="color:#000000;">]) [</span><span style="color:#666666;">1</span><span style="color:#000000;">]</span></pre>
<p>To display the triangle correctly, we need to prepend the appropriate amount of spacing to each line based on the longest (i.e. last) line.</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">prettyPascal <span style="color:#000000;">::</span> <span style="color:#ff9900;font-weight:bold;">Int</span> <span style="color:#000000;">-&gt;</span> <span style="color:#ff9900;font-weight:bold;">IO</span> <span style="color:#000000;">()</span>
prettyPascal n <span style="color:#000000;">=</span> <span style="color:#0066ff;">mapM_</span> <span style="color:#000000;">(</span><span style="color:#ff0000;">\r</span> <span style="color:#000000;">-&gt;</span> printf <span style="color:#666666;">"%*s</span><span style="color:#ff0000;">\n</span><span style="color:#666666;">"</span> <span style="color:#000000;">(</span><span style="color:#0066ff;">div</span> <span style="color:#000000;">(</span>longest <span style="color:#000000;">+</span> <span style="color:#ff9900;">length</span> r<span style="color:#000000;">)</span> <span style="color:#666666;">2</span><span style="color:#000000;">)</span> r<span style="color:#000000;">)</span> rows
    <span style="color:#ff00e5;">where</span> rows <span style="color:#000000;">=</span> <span style="color:#0066ff;">map</span> <span style="color:#000000;">(</span><span style="color:#0066ff;">unwords</span> <span style="color:#000000;">.</span> <span style="color:#0066ff;">map show</span><span style="color:#000000;">) $</span> <span style="color:#0066ff;">take</span> <span style="color:#000000;">(</span>n <span style="color:#000000;">+</span> <span style="color:#666666;">1</span><span style="color:#000000;">)</span> pascal
          longest <span style="color:#000000;">=</span> <span style="color:#ff9900;">length</span> <span style="color:#000000;">$</span> <span style="color:#0066ff;">last</span> rows</pre>
<p>An that&#8217;s all there is to it. A quick test to see if everything is working proprely:</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">main <span style="color:#000000;">::</span> <span style="color:#ff9900;font-weight:bold;">IO</span> <span style="color:#000000;">()</span>
main <span style="color:#000000;">=</span> prettyPascal <span style="color:#666666;">10</span></pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bonsaicode.wordpress.com/850/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bonsaicode.wordpress.com/850/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bonsaicode.wordpress.com/850/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bonsaicode.wordpress.com/850/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bonsaicode.wordpress.com/850/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bonsaicode.wordpress.com/850/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bonsaicode.wordpress.com/850/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bonsaicode.wordpress.com/850/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bonsaicode.wordpress.com/850/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bonsaicode.wordpress.com/850/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bonsaicode.wordpress.com/850/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bonsaicode.wordpress.com/850/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bonsaicode.wordpress.com/850/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bonsaicode.wordpress.com/850/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bonsaicode.wordpress.com&amp;blog=7474313&amp;post=850&amp;subd=bonsaicode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bonsaicode.wordpress.com/2011/12/06/programming-praxis-pascals-triangle/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/af10cfb68163f74baccb0107d33a207b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Remco Niemeijer</media:title>
		</media:content>
	</item>
		<item>
		<title>Programming Praxis &#8211; Brainfuck</title>
		<link>http://bonsaicode.wordpress.com/2011/10/04/programming-praxis-brainfuck/</link>
		<comments>http://bonsaicode.wordpress.com/2011/10/04/programming-praxis-brainfuck/#comments</comments>
		<pubDate>Tue, 04 Oct 2011 18:31:51 +0000</pubDate>
		<dc:creator>Remco Niemeijer</dc:creator>
				<category><![CDATA[Programming Praxis]]></category>
		<category><![CDATA[bonsai]]></category>
		<category><![CDATA[brainfuck]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[interpreter]]></category>
		<category><![CDATA[kata]]></category>
		<category><![CDATA[praxis]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://bonsaicode.wordpress.com/?p=845</guid>
		<description><![CDATA[In today&#8217;s Programming Praxis exercise, our goal is to implement a Brainfuck interpreter. Let&#8217;s get started, shall we? Since we&#8217;ll be doing a lot of moving left and right in the program and cells a zipper is an obvious data structure to use. import Data.List.Zipper Running a program is a simple matter of executing all [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bonsaicode.wordpress.com&amp;blog=7474313&amp;post=845&amp;subd=bonsaicode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In <a title="Original exercise" href="http://programmingpraxis.com/2011/10/04/brainfuck/" target="_blank">today&#8217;s</a> Programming Praxis exercise, our goal is to implement a Brainfuck interpreter. Let&#8217;s get started, shall we?</p>
<p>Since we&#8217;ll be doing a lot of moving left and right in the program and cells a zipper is an obvious data structure to use.</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';"><span style="color:#ff00e5;">import</span> Data<span style="color:#000000;">.</span>List<span style="color:#000000;">.</span>Zipper</pre>
<p>Running a program is a simple matter of executing all the steps until we arrive at the end, starting with a zeroed-out list.</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">run <span style="color:#000000;">::</span> <span style="color:#ff9900;font-weight:bold;">String</span> <span style="color:#000000;">-&gt;</span> <span style="color:#ff9900;font-weight:bold;">IO String</span>
run <span style="color:#000000;">=</span> <span style="color:#0066ff;">fmap</span> toList <span style="color:#000000;">.</span> <span style="color:#0066ff;">flip</span> step <span style="color:#000000;">(</span>fromList <span style="color:#000000;">$</span> <span style="color:#0066ff;">replicate</span> <span style="color:#666666;">30000</span> <span style="color:#000000;">'\</span>NUL<span style="color:#000000;">') .</span> fromList

step <span style="color:#000000;">::</span> Zipper <span style="color:#ff9900;font-weight:bold;">Char</span> <span style="color:#000000;">-&gt;</span> Zipper <span style="color:#ff9900;font-weight:bold;">Char</span> <span style="color:#000000;">-&gt;</span> <span style="color:#ff9900;font-weight:bold;">IO</span> <span style="color:#000000;">(</span>Zipper <span style="color:#ff9900;font-weight:bold;">Char</span><span style="color:#000000;">)</span>
step prog s <span style="color:#000000;">=</span> <span style="color:#ff00e5;">if</span> endp prog <span style="color:#ff00e5;">then</span> <span style="color:#0066ff;">return</span> s <span style="color:#ff00e5;">else</span>
              <span style="color:#0066ff;">uncurry</span> step <span style="color:#000000;">=&lt;&lt;</span> instruction <span style="color:#000000;">(</span>cursor prog<span style="color:#000000;">)</span> prog s</pre>
<p>Using the zipper, most of the instructions are fairly trivial. The brackets are the only ones that require a bit of extra work.</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">instruction <span style="color:#000000;">::</span> <span style="color:#ff9900;font-weight:bold;">Char</span> <span style="color:#000000;">-&gt;</span> Zipper <span style="color:#ff9900;font-weight:bold;">Char</span> <span style="color:#000000;">-&gt;</span> Zipper <span style="color:#ff9900;font-weight:bold;">Char</span> <span style="color:#000000;">-&gt;</span> <span style="color:#ff9900;font-weight:bold;">IO</span> <span style="color:#000000;">(</span>Zipper <span style="color:#ff9900;font-weight:bold;">Char</span><span style="color:#000000;">,</span> Zipper <span style="color:#ff9900;font-weight:bold;">Char</span><span style="color:#000000;">)</span>
instruction <span style="color:#000000;">'&lt;'</span> prog s <span style="color:#000000;">=</span> <span style="color:#0066ff;">return</span> <span style="color:#000000;">(</span>right prog<span style="color:#000000;">,</span> left s<span style="color:#000000;">)</span>
instruction <span style="color:#000000;">'&gt;'</span> prog s <span style="color:#000000;">=</span> <span style="color:#0066ff;">return</span> <span style="color:#000000;">(</span>right prog<span style="color:#000000;">,</span> right s<span style="color:#000000;">)</span>
instruction <span style="color:#000000;">'+'</span> prog s <span style="color:#000000;">=</span> <span style="color:#0066ff;">return</span> <span style="color:#000000;">(</span>right prog<span style="color:#000000;">,</span> replace <span style="color:#000000;">(</span><span style="color:#0066ff;">succ</span> <span style="color:#000000;">$</span> cursor s<span style="color:#000000;">)</span> s<span style="color:#000000;">)</span>
instruction <span style="color:#000000;">'-'</span> prog s <span style="color:#000000;">=</span> <span style="color:#0066ff;">return</span> <span style="color:#000000;">(</span>right prog<span style="color:#000000;">,</span> replace <span style="color:#000000;">(</span><span style="color:#0066ff;">pred</span> <span style="color:#000000;">$</span> cursor s<span style="color:#000000;">)</span> s<span style="color:#000000;">)</span>
instruction <span style="color:#000000;">'.'</span> prog s <span style="color:#000000;">=</span> <span style="color:#0066ff;">putStr</span> <span style="color:#000000;">[</span>cursor s<span style="color:#000000;">] &gt;&gt;</span> <span style="color:#0066ff;">return</span> <span style="color:#000000;">(</span>right prog<span style="color:#000000;">,</span> s<span style="color:#000000;">)</span>
instruction <span style="color:#000000;">','</span> prog s <span style="color:#000000;">=</span> <span style="color:#0066ff;">fmap</span> <span style="color:#000000;">((,) (</span>right prog<span style="color:#000000;">) .</span> <span style="color:#0066ff;">flip</span> replace s<span style="color:#000000;">)</span> <span style="color:#0066ff;">getChar</span>
instruction <span style="color:#000000;">'['</span> prog s <span style="color:#000000;">=</span> <span style="color:#0066ff;">return</span> <span style="color:#000000;">$ (</span><span style="color:#ff00e5;">if</span> cursor s <span style="color:#000000;">== '\</span>NUL<span style="color:#000000;">'</span> <span style="color:#ff00e5;">then</span>
                             right <span style="color:#000000;">$</span> move right <span style="color:#000000;">'[' ']'</span> prog <span style="color:#ff00e5;">else</span> right prog<span style="color:#000000;">,</span> s<span style="color:#000000;">)</span>
instruction <span style="color:#000000;">']'</span> prog s <span style="color:#000000;">=</span> <span style="color:#0066ff;">return</span> <span style="color:#000000;">(</span>move left <span style="color:#000000;">']' '['</span> prog<span style="color:#000000;">,</span> s<span style="color:#000000;">)</span>
instruction _   prog s <span style="color:#000000;">=</span> <span style="color:#0066ff;">return</span> <span style="color:#000000;">(</span>right prog<span style="color:#000000;">,</span> s<span style="color:#000000;">)</span></pre>
<p>Moving the cursor to the corresponding bracket requires stepping over any nested sets of brackets.</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">move <span style="color:#000000;">:: (</span>Zipper <span style="color:#ff9900;font-weight:bold;">Char</span> <span style="color:#000000;">-&gt;</span> Zipper <span style="color:#ff9900;font-weight:bold;">Char</span><span style="color:#000000;">) -&gt;</span> <span style="color:#ff9900;font-weight:bold;">Char</span> <span style="color:#000000;">-&gt;</span> <span style="color:#ff9900;font-weight:bold;">Char</span> <span style="color:#000000;">-&gt;</span> Zipper <span style="color:#ff9900;font-weight:bold;">Char</span> <span style="color:#000000;">-&gt;</span> Zipper <span style="color:#ff9900;font-weight:bold;">Char</span>
move dir open close <span style="color:#000000;">=</span> f <span style="color:#666666;">0</span> <span style="color:#000000;">.</span> dir <span style="color:#ff00e5;">where</span>
    f <span style="color:#666666;">0</span> z | cursor z <span style="color:#000000;">==</span> close <span style="color:#000000;">=</span> z
    f n z <span style="color:#000000;">=</span> f <span style="color:#000000;">(</span><span style="color:#ff00e5;">if</span> cursor z <span style="color:#000000;">==</span> open  <span style="color:#ff00e5;">then</span> n <span style="color:#000000;">+</span> <span style="color:#666666;">1</span> <span style="color:#ff00e5;">else</span>
               <span style="color:#ff00e5;">if</span> cursor z <span style="color:#000000;">==</span> close <span style="color:#ff00e5;">then</span> n <span style="color:#000000;">-</span> <span style="color:#666666;">1</span> <span style="color:#ff00e5;">else</span> n<span style="color:#000000;">) $</span> dir z</pre>
<p>All that's left to do is to run the program, which prints the expected Hello World message.</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">main <span style="color:#000000;">::</span> <span style="color:#ff9900;font-weight:bold;">IO</span> <span style="color:#000000;">()</span>
main <span style="color:#000000;">=</span> run <span style="color:#666666;">"++++++++++[&gt;+++++++&gt;++++++++++&gt;+++&gt;+&lt;\</span>
<span style="color:#666666;"> \&lt;&lt;&lt;-]&gt;++.&gt;+.+++++++..+++.&gt;++.&lt;&lt;++++++\</span>
<span style="color:#666666;"> \+++++++++.&gt;.+++.------.--------.&gt;+.&gt;."</span> <span style="color:#000000;">&gt;&gt;</span> <span style="color:#0066ff;">return</span> <span style="color:#000000;">()</span></pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bonsaicode.wordpress.com/845/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bonsaicode.wordpress.com/845/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bonsaicode.wordpress.com/845/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bonsaicode.wordpress.com/845/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bonsaicode.wordpress.com/845/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bonsaicode.wordpress.com/845/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bonsaicode.wordpress.com/845/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bonsaicode.wordpress.com/845/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bonsaicode.wordpress.com/845/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bonsaicode.wordpress.com/845/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bonsaicode.wordpress.com/845/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bonsaicode.wordpress.com/845/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bonsaicode.wordpress.com/845/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bonsaicode.wordpress.com/845/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bonsaicode.wordpress.com&amp;blog=7474313&amp;post=845&amp;subd=bonsaicode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bonsaicode.wordpress.com/2011/10/04/programming-praxis-brainfuck/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/af10cfb68163f74baccb0107d33a207b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Remco Niemeijer</media:title>
		</media:content>
	</item>
		<item>
		<title>Programming Praxis &#8211; Tetrahedral Numbers</title>
		<link>http://bonsaicode.wordpress.com/2011/09/13/programming-praxis-tetrahedral-numbers/</link>
		<comments>http://bonsaicode.wordpress.com/2011/09/13/programming-praxis-tetrahedral-numbers/#comments</comments>
		<pubDate>Tue, 13 Sep 2011 11:25:58 +0000</pubDate>
		<dc:creator>Remco Niemeijer</dc:creator>
				<category><![CDATA[Programming Praxis]]></category>
		<category><![CDATA[bonsai]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[kata]]></category>
		<category><![CDATA[numbers]]></category>
		<category><![CDATA[praxis]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tetrahedral]]></category>
		<category><![CDATA[triangular]]></category>

		<guid isPermaLink="false">http://bonsaicode.wordpress.com/?p=842</guid>
		<description><![CDATA[In today&#8217;s Programming Praxis, our goal is to find the base of the three-sided pyramid that has 169179692512835000 spheres in it. Let&#8217;s get started, shall we? A quick import: import Data.List The tetrahedral numbers are based on the triangular numbers, so let&#8217;s start with those. triangular :: [Integer] triangular = scanl1 (+) [1..] The tetrahedral [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bonsaicode.wordpress.com&amp;blog=7474313&amp;post=842&amp;subd=bonsaicode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In <a title="Original exercise" href="http://programmingpraxis.com/2011/09/13/tetrahedral-numbers/" target="_blank">today&#8217;s</a> Programming Praxis, our goal is to find the base of the three-sided pyramid that has 169179692512835000 spheres in it. Let&#8217;s get started, shall we?</p>
<p>A quick import:</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">
<span style="color:#ff00e5;">import</span> Data<span style="color:#000000;">.</span>List
</pre>
<p>The tetrahedral numbers are based on the triangular numbers, so let&#8217;s start with those.</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">
triangular <span style="color:#000000;">:: [</span><span style="color:#ff9900;font-weight:bold;">Integer</span><span style="color:#000000;">]</span>
triangular <span style="color:#000000;">=</span> <span style="color:#0066ff;">scanl1</span> <span style="color:#000000;">(+) [</span><span style="color:#666666;">1</span><span style="color:#000000;">..]</span>
</pre>
<p>The tetrahedral numbers are formed in much the same way as the triangular ones.</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">
tetrahedral <span style="color:#000000;">:: [</span><span style="color:#ff9900;font-weight:bold;">Integer</span><span style="color:#000000;">]</span>
tetrahedral <span style="color:#000000;">=</span> <span style="color:#0066ff;">scanl1</span> <span style="color:#000000;">(+)</span> triangular
</pre>
<p>All that&#8217;s left to do is to is to find the base of the pyramid.</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">
main <span style="color:#000000;">::</span> <span style="color:#ff9900;font-weight:bold;">IO</span> <span style="color:#000000;">()</span>
main <span style="color:#000000;">=</span> <span style="color:#0066ff;">print</span> <span style="color:#000000;">.</span> <span style="color:#0066ff;">maybe</span> <span style="color:#666666;">0</span> <span style="color:#0066ff;">succ</span> <span style="color:#000000;">$</span>
       <span style="color:#0066ff;">findIndex</span> <span style="color:#000000;">(==</span> <span style="color:#666666;">169179692512835000</span><span style="color:#000000;">)</span> tetrahedral
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bonsaicode.wordpress.com/842/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bonsaicode.wordpress.com/842/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bonsaicode.wordpress.com/842/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bonsaicode.wordpress.com/842/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bonsaicode.wordpress.com/842/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bonsaicode.wordpress.com/842/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bonsaicode.wordpress.com/842/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bonsaicode.wordpress.com/842/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bonsaicode.wordpress.com/842/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bonsaicode.wordpress.com/842/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bonsaicode.wordpress.com/842/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bonsaicode.wordpress.com/842/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bonsaicode.wordpress.com/842/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bonsaicode.wordpress.com/842/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bonsaicode.wordpress.com&amp;blog=7474313&amp;post=842&amp;subd=bonsaicode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bonsaicode.wordpress.com/2011/09/13/programming-praxis-tetrahedral-numbers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/af10cfb68163f74baccb0107d33a207b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Remco Niemeijer</media:title>
		</media:content>
	</item>
		<item>
		<title>Programming Praxis &#8211; First Non-Repeating Character</title>
		<link>http://bonsaicode.wordpress.com/2011/08/19/programming-praxis-first-non-repeating-character/</link>
		<comments>http://bonsaicode.wordpress.com/2011/08/19/programming-praxis-first-non-repeating-character/#comments</comments>
		<pubDate>Fri, 19 Aug 2011 17:28:39 +0000</pubDate>
		<dc:creator>Remco Niemeijer</dc:creator>
				<category><![CDATA[Programming Praxis]]></category>
		<category><![CDATA[bonsai]]></category>
		<category><![CDATA[character]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[element]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[kata]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[non-repeating]]></category>
		<category><![CDATA[praxis]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://bonsaicode.wordpress.com/?p=835</guid>
		<description><![CDATA[In today&#8217;s Programming Praxis exercise, our goal is to find the first character in a string that does not occur anywhere else in the string. Let&#8217;s get started, shall we? Some imports: import Data.List import qualified Data.Map as M To find the first non-repeated element, we mark each element as unique and insert them into [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bonsaicode.wordpress.com&amp;blog=7474313&amp;post=835&amp;subd=bonsaicode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In <a title="Original exercise" href="http://programmingpraxis.com/2011/08/19/first-non-repeating-character/" target="_blank">today&#8217;s</a> Programming Praxis exercise, our goal is to find the first character in a string that does not occur anywhere else in the string. Let&#8217;s get started, shall we?</p>
<p>Some imports:</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';"><span style="color:#ff00e5;">import</span> Data<span style="color:#000000;">.</span>List
<span style="color:#ff00e5;">import qualified</span> Data<span style="color:#000000;">.</span>Map <span style="color:#ff00e5;">as</span> M</pre>
<p>To find the first non-repeated element, we mark each element as unique and insert them into a map. When a duplicate element is found, we remove its unique status. Afterwards, we search the list for the first unique element.</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">firstUnique <span style="color:#000000;">::</span> Ord a <span style="color:#000000;">=&gt; [</span>a<span style="color:#000000;">] -&gt;</span> <span style="color:#ff9900;font-weight:bold;">Maybe</span> a
firstUnique xs <span style="color:#000000;">=</span> <span style="color:#0066ff;">find</span> <span style="color:#000000;">(</span>M<span style="color:#000000;">.</span>fromListWith <span style="color:#000000;">(\</span>_ _ <span style="color:#000000;">-&gt;</span> <span style="color:#ff9900;">False</span><span style="color:#000000;">)</span>
                           <span style="color:#000000;">(</span><span style="color:#0066ff;">zip</span> xs <span style="color:#000000;">$</span> <span style="color:#0066ff;">repeat</span> <span style="color:#ff9900;">True</span><span style="color:#000000;">)</span> M<span style="color:#000000;">.!)</span> xs</pre>
<p>Some tests to see if everything is working properly:</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">main <span style="color:#000000;">::</span> <span style="color:#ff9900;font-weight:bold;">IO</span> <span style="color:#000000;">()</span>
main <span style="color:#000000;">=</span> <span style="color:#ff00e5;">do</span> <span style="color:#0066ff;">print</span> <span style="color:#000000;">$</span> firstUnique <span style="color:#666666;">"aabcbcdeef"</span>   <span style="color:#000000;">==</span> Just <span style="color:#000000;">'</span>d<span style="color:#000000;">'</span>
          <span style="color:#0066ff;">print</span> <span style="color:#000000;">$</span> firstUnique <span style="color:#666666;">"aabcbcfeed"</span>   <span style="color:#000000;">==</span> Just <span style="color:#000000;">'</span>f<span style="color:#000000;">'</span>
          <span style="color:#0066ff;">print</span> <span style="color:#000000;">$</span> firstUnique <span style="color:#666666;">"aabcbcdeefdf"</span> <span style="color:#000000;">==</span> Nothing</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bonsaicode.wordpress.com/835/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bonsaicode.wordpress.com/835/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bonsaicode.wordpress.com/835/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bonsaicode.wordpress.com/835/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bonsaicode.wordpress.com/835/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bonsaicode.wordpress.com/835/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bonsaicode.wordpress.com/835/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bonsaicode.wordpress.com/835/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bonsaicode.wordpress.com/835/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bonsaicode.wordpress.com/835/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bonsaicode.wordpress.com/835/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bonsaicode.wordpress.com/835/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bonsaicode.wordpress.com/835/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bonsaicode.wordpress.com/835/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bonsaicode.wordpress.com&amp;blog=7474313&amp;post=835&amp;subd=bonsaicode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bonsaicode.wordpress.com/2011/08/19/programming-praxis-first-non-repeating-character/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/af10cfb68163f74baccb0107d33a207b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Remco Niemeijer</media:title>
		</media:content>
	</item>
		<item>
		<title>Programming Praxis &#8211; Hett’s Problem 1.28</title>
		<link>http://bonsaicode.wordpress.com/2011/08/09/programming-praxis-hett%e2%80%99s-problem-1-28/</link>
		<comments>http://bonsaicode.wordpress.com/2011/08/09/programming-praxis-hett%e2%80%99s-problem-1-28/#comments</comments>
		<pubDate>Tue, 09 Aug 2011 17:29:17 +0000</pubDate>
		<dc:creator>Remco Niemeijer</dc:creator>
				<category><![CDATA[Programming Praxis]]></category>
		<category><![CDATA[bonsai]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[hett]]></category>
		<category><![CDATA[kata]]></category>
		<category><![CDATA[length]]></category>
		<category><![CDATA[lists]]></category>
		<category><![CDATA[praxis]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[sort]]></category>

		<guid isPermaLink="false">http://bonsaicode.wordpress.com/?p=832</guid>
		<description><![CDATA[In today&#8217;s Programming Praxis, our goal is to sort a list of lists by length and by length frequency. Let&#8217;s get started, shall we? A quick import: import qualified Data.List.Key as K Sorting by length is trivial. byLength :: [[a]] -&#62; [[a]] byLength = K.sort length Sorting by frequency of the list lengths is a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bonsaicode.wordpress.com&amp;blog=7474313&amp;post=832&amp;subd=bonsaicode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In <a title="Original exercise" href="http://programmingpraxis.com/2011/08/09/hetts-problem-128/" target="_blank">today&#8217;s</a> Programming Praxis, our goal is to sort a list of lists by length and by length frequency. Let&#8217;s get started, shall we?</p>
<p>A quick import:</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">
<span style="color:#ff00e5;">import qualified</span> Data<span style="color:#000000;">.</span>List<span style="color:#000000;">.</span>Key <span style="color:#ff00e5;">as</span> K
</pre>
<p>Sorting by length is trivial.</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">
byLength <span style="color:#000000;">:: [[</span>a<span style="color:#000000;">]] -&gt; [[</span>a<span style="color:#000000;">]]</span>
byLength <span style="color:#000000;">=</span> K<span style="color:#000000;">.</span><span style="color:#0066ff;">sort</span> <span style="color:#ff9900;">length</span>
</pre>
<p>Sorting by frequency of the list lengths is a bit more complicated since we need to group and ungroup the lists, but still a one-liner.</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">
byLengthFreq <span style="color:#000000;">:: [[</span>a<span style="color:#000000;">]] -&gt; [[</span>a<span style="color:#000000;">]]</span>
byLengthFreq <span style="color:#000000;">=</span> <span style="color:#0066ff;">concat</span> <span style="color:#000000;">.</span> byLength <span style="color:#000000;">.</span> K<span style="color:#000000;">.</span><span style="color:#0066ff;">group</span> <span style="color:#ff9900;">length</span> <span style="color:#000000;">.</span> byLength
</pre>
<p>Some tests to see if everything is working properly:</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">
main <span style="color:#000000;">::</span> <span style="color:#ff9900;font-weight:bold;">IO</span> <span style="color:#000000;">()</span>
main <span style="color:#000000;">=</span> <span style="color:#ff00e5;">do</span> <span style="color:#0066ff;">print</span> <span style="color:#000000;">$</span> byLength <span style="color:#000000;">[</span><span style="color:#666666;">&quot;abc&quot;</span><span style="color:#000000;">,</span><span style="color:#666666;">&quot;de&quot;</span><span style="color:#000000;">,</span><span style="color:#666666;">&quot;fgh&quot;</span><span style="color:#000000;">,</span><span style="color:#666666;">&quot;de&quot;</span><span style="color:#000000;">,</span><span style="color:#666666;">&quot;ijkl&quot;</span><span style="color:#000000;">,</span><span style="color:#666666;">&quot;mn&quot;</span><span style="color:#000000;">,</span><span style="color:#666666;">&quot;o&quot;</span><span style="color:#000000;">]</span>
                        <span style="color:#000000;">== [</span><span style="color:#666666;">&quot;o&quot;</span><span style="color:#000000;">,</span><span style="color:#666666;">&quot;de&quot;</span><span style="color:#000000;">,</span><span style="color:#666666;">&quot;de&quot;</span><span style="color:#000000;">,</span><span style="color:#666666;">&quot;mn&quot;</span><span style="color:#000000;">,</span><span style="color:#666666;">&quot;abc&quot;</span><span style="color:#000000;">,</span><span style="color:#666666;">&quot;fgh&quot;</span><span style="color:#000000;">,</span><span style="color:#666666;">&quot;ijkl&quot;</span><span style="color:#000000;">]</span>
          <span style="color:#0066ff;">print</span> <span style="color:#000000;">$</span> byLengthFreq <span style="color:#000000;">[</span><span style="color:#666666;">&quot;abc&quot;</span><span style="color:#000000;">,</span><span style="color:#666666;">&quot;de&quot;</span><span style="color:#000000;">,</span><span style="color:#666666;">&quot;fgh&quot;</span><span style="color:#000000;">,</span><span style="color:#666666;">&quot;de&quot;</span><span style="color:#000000;">,</span><span style="color:#666666;">&quot;ijkl&quot;</span><span style="color:#000000;">,</span><span style="color:#666666;">&quot;mn&quot;</span><span style="color:#000000;">,</span><span style="color:#666666;">&quot;o&quot;</span><span style="color:#000000;">]</span>
                            <span style="color:#000000;">== [</span><span style="color:#666666;">&quot;o&quot;</span><span style="color:#000000;">,</span><span style="color:#666666;">&quot;ijkl&quot;</span><span style="color:#000000;">,</span><span style="color:#666666;">&quot;abc&quot;</span><span style="color:#000000;">,</span><span style="color:#666666;">&quot;fgh&quot;</span><span style="color:#000000;">,</span><span style="color:#666666;">&quot;de&quot;</span><span style="color:#000000;">,</span><span style="color:#666666;">&quot;de&quot;</span><span style="color:#000000;">,</span><span style="color:#666666;">&quot;mn&quot;</span><span style="color:#000000;">]</span>
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bonsaicode.wordpress.com/832/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bonsaicode.wordpress.com/832/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bonsaicode.wordpress.com/832/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bonsaicode.wordpress.com/832/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bonsaicode.wordpress.com/832/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bonsaicode.wordpress.com/832/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bonsaicode.wordpress.com/832/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bonsaicode.wordpress.com/832/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bonsaicode.wordpress.com/832/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bonsaicode.wordpress.com/832/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bonsaicode.wordpress.com/832/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bonsaicode.wordpress.com/832/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bonsaicode.wordpress.com/832/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bonsaicode.wordpress.com/832/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bonsaicode.wordpress.com&amp;blog=7474313&amp;post=832&amp;subd=bonsaicode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bonsaicode.wordpress.com/2011/08/09/programming-praxis-hett%e2%80%99s-problem-1-28/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/af10cfb68163f74baccb0107d33a207b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Remco Niemeijer</media:title>
		</media:content>
	</item>
		<item>
		<title>End of an Era</title>
		<link>http://bonsaicode.wordpress.com/2011/07/01/end-of-an-era/</link>
		<comments>http://bonsaicode.wordpress.com/2011/07/01/end-of-an-era/#comments</comments>
		<pubDate>Fri, 01 Jul 2011 10:49:00 +0000</pubDate>
		<dc:creator>Remco Niemeijer</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[an]]></category>
		<category><![CDATA[end]]></category>
		<category><![CDATA[era]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[of]]></category>
		<category><![CDATA[platform]]></category>
		<category><![CDATA[praxis]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://bonsaicode.wordpress.com/?p=826</guid>
		<description><![CDATA[Last tuesday I got my PhD. Unfortunately, that has some consequences for this blog. Since I&#8217;ll now be working at a real job, which I&#8217;m starting monday, I won&#8217;t be able to do any Programming Praxis exercises during working hours anymore like I have for the past couple years. I&#8217;ll try and do some in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bonsaicode.wordpress.com&amp;blog=7474313&amp;post=826&amp;subd=bonsaicode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Last tuesday I got my PhD. Unfortunately, that has some consequences for this blog. Since I&#8217;ll now be working at a real job, which I&#8217;m starting monday, I won&#8217;t be able to do any Programming Praxis exercises during working hours anymore like I have for the past couple years. I&#8217;ll try and do some in the evenings now and then, but since I have a lot of other stuff to do, the frequency will be significantly reduced.</p>
<p>I hope you guys have enjoyed this blog over the past few years and I hope I&#8217;ve gotten at least a few of you to try out Haskell. If you haven&#8217;t, what are you waiting for? Go download the <a title="Haskell Platform" href="http://hackage.haskell.org/platform/" target="_blank">Haskell Platform</a> and get started! Looking for something to practise on? Go do some of the exercises from <a title="Programming Praxis" href="http://programmingpraxis.com/" target="_blank">Programming Praxis</a>. I&#8217;m looking forward to reading your solutions.</p>
<p>Regards,<br />
Remco Niemeijer</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bonsaicode.wordpress.com/826/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bonsaicode.wordpress.com/826/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bonsaicode.wordpress.com/826/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bonsaicode.wordpress.com/826/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bonsaicode.wordpress.com/826/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bonsaicode.wordpress.com/826/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bonsaicode.wordpress.com/826/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bonsaicode.wordpress.com/826/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bonsaicode.wordpress.com/826/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bonsaicode.wordpress.com/826/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bonsaicode.wordpress.com/826/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bonsaicode.wordpress.com/826/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bonsaicode.wordpress.com/826/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bonsaicode.wordpress.com/826/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bonsaicode.wordpress.com&amp;blog=7474313&amp;post=826&amp;subd=bonsaicode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bonsaicode.wordpress.com/2011/07/01/end-of-an-era/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/af10cfb68163f74baccb0107d33a207b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Remco Niemeijer</media:title>
		</media:content>
	</item>
		<item>
		<title>Programming Praxis &#8211; Feet And Inches</title>
		<link>http://bonsaicode.wordpress.com/2011/07/01/programming-praxis-feet-and-inches/</link>
		<comments>http://bonsaicode.wordpress.com/2011/07/01/programming-praxis-feet-and-inches/#comments</comments>
		<pubDate>Fri, 01 Jul 2011 10:34:01 +0000</pubDate>
		<dc:creator>Remco Niemeijer</dc:creator>
				<category><![CDATA[Programming Praxis]]></category>
		<category><![CDATA[bonsai]]></category>
		<category><![CDATA[carpenter]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[feet]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[inches]]></category>
		<category><![CDATA[kata]]></category>
		<category><![CDATA[praxis]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://bonsaicode.wordpress.com/?p=822</guid>
		<description><![CDATA[In today&#8217;s Programming Praxis exercise, our goal is to convert a decimal length value to the fractions used by carpenters. Let&#8217;s get started, shall we? Some imports: import Data.Ratio import Text.Printf To get proper rounding we first multiply by 32 and then see how many feet, inches and fractions of an inch there are. toCarpenter [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bonsaicode.wordpress.com&amp;blog=7474313&amp;post=822&amp;subd=bonsaicode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In <a title="Original exercise" href="http://programmingpraxis.com/2011/07/01/feet-and-inches/" target="_blank">today&#8217;s</a> Programming Praxis exercise, our goal is to convert a decimal length value to the fractions used by carpenters. Let&#8217;s get started, shall we?</p>
<p>Some imports:</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';"><span style="color:#ff00e5;">import</span> Data<span style="color:#000000;">.</span><span style="color:#ff9900;font-weight:bold;">Ratio</span>
<span style="color:#ff00e5;">import</span> Text<span style="color:#000000;">.</span>Printf</pre>
<p>To get proper rounding we first multiply by 32 and then see how many feet, inches and fractions of an inch there are.</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">toCarpenter <span style="color:#000000;">::</span> RealFrac a <span style="color:#000000;">=&gt;</span> a <span style="color:#000000;">-&gt; (</span><span style="color:#ff9900;font-weight:bold;">Int</span><span style="color:#000000;">,</span> <span style="color:#ff9900;font-weight:bold;">Int</span><span style="color:#000000;">,</span> <span style="color:#ff9900;font-weight:bold;">Ratio Int</span><span style="color:#000000;">)</span>
toCarpenter l <span style="color:#000000;">= (</span>feet<span style="color:#000000;">,</span> <span style="color:#0066ff;">div</span> r <span style="color:#666666;">32</span><span style="color:#000000;">,</span> <span style="color:#0066ff;">mod</span> r <span style="color:#666666;">32</span> <span style="color:#000000;">%</span> <span style="color:#666666;">32</span><span style="color:#000000;">)</span> <span style="color:#ff00e5;">where</span>
    <span style="color:#000000;">(</span>feet<span style="color:#000000;">,</span> r<span style="color:#000000;">) =</span> <span style="color:#0066ff;">divMod</span> <span style="color:#000000;">(</span><span style="color:#ff9900;">round</span> <span style="color:#000000;">$</span> l <span style="color:#000000;">*</span> <span style="color:#666666;">32</span><span style="color:#000000;">) (</span><span style="color:#666666;">32</span> <span style="color:#000000;">*</span> <span style="color:#666666;">12</span><span style="color:#000000;">)</span></pre>
<p>Formatting the text is a unfortunately a tad unwieldy due to the number of special cases.</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">feetAndInches <span style="color:#000000;">::</span> RealFrac a <span style="color:#000000;">=&gt;</span> a <span style="color:#000000;">-&gt;</span> <span style="color:#ff9900;font-weight:bold;">String</span>
feetAndInches l <span style="color:#000000;">=</span> <span style="color:#ff00e5;">case</span> toCarpenter l <span style="color:#ff00e5;">of</span>
    <span style="color:#000000;">(</span><span style="color:#666666;">0</span><span style="color:#000000;">,</span><span style="color:#666666;">0</span><span style="color:#000000;">,</span><span style="color:#666666;">0</span><span style="color:#000000;">) -&gt;</span> <span style="color:#666666;">"0 feet 0 inches"</span>
    <span style="color:#000000;">(</span>f<span style="color:#000000;">,</span>i<span style="color:#000000;">,</span>t<span style="color:#000000;">) -&gt;</span> showUnit <span style="color:#666666;">"foot"</span> <span style="color:#666666;">"feet"</span> <span style="color:#000000;">(</span>f <span style="color:#000000;">%</span> <span style="color:#666666;">1</span><span style="color:#000000;">) ++</span>
               <span style="color:#000000;">(</span><span style="color:#ff00e5;">if</span> f <span style="color:#000000;">&gt;</span> <span style="color:#666666;">0</span> <span style="color:#000000;">&amp;&amp; (</span>i<span style="color:#000000;">%</span><span style="color:#666666;">1</span> <span style="color:#000000;">+</span> t<span style="color:#000000;">) &gt;</span> <span style="color:#666666;">0</span> <span style="color:#ff00e5;">then</span> <span style="color:#666666;">" "</span> <span style="color:#ff00e5;">else</span> <span style="color:#666666;">""</span><span style="color:#000000;">) ++</span>
               showUnit <span style="color:#666666;">"inch"</span> <span style="color:#666666;">"inches"</span> <span style="color:#000000;">(</span>i <span style="color:#000000;">%</span> <span style="color:#666666;">1</span> <span style="color:#000000;">+</span> t<span style="color:#000000;">)</span>
    <span style="color:#ff00e5;">where</span>
    showUnit _ _ <span style="color:#666666;">0</span> <span style="color:#000000;">=</span> <span style="color:#666666;">""</span>
    showUnit s m n <span style="color:#000000;">=</span> printf <span style="color:#666666;">"%s %s"</span> <span style="color:#000000;">(</span>showVal n<span style="color:#000000;">) $</span> <span style="color:#ff00e5;">if</span> n <span style="color:#000000;">&lt;=</span> <span style="color:#666666;">1</span> <span style="color:#ff00e5;">then</span> s <span style="color:#ff00e5;">else</span> m
    showVal v | d <span style="color:#000000;">==</span> <span style="color:#666666;">1</span>    <span style="color:#000000;">=</span> <span style="color:#0066ff;">show</span> n
              | v <span style="color:#000000;">&lt;</span> <span style="color:#666666;">1</span>     <span style="color:#000000;">=</span> printf <span style="color:#666666;">"%d/%d"</span> n d
              | <span style="color:#0066ff;">otherwise</span> <span style="color:#000000;">=</span> printf <span style="color:#666666;">"%d and %d/%d"</span> <span style="color:#000000;">(</span><span style="color:#0066ff;">div</span> n d<span style="color:#000000;">) (</span><span style="color:#0066ff;">mod</span> n d<span style="color:#000000;">)</span> d
              <span style="color:#ff00e5;">where</span> <span style="color:#000000;">(</span>n<span style="color:#000000;">,</span>d<span style="color:#000000;">) = (</span><span style="color:#0066ff;">numerator</span> v<span style="color:#000000;">,</span> <span style="color:#0066ff;">denominator</span> v<span style="color:#000000;">)</span></pre>
<p>Some tests to see if everything is working properly:</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">main <span style="color:#000000;">::</span> <span style="color:#ff9900;font-weight:bold;">IO</span> <span style="color:#000000;">()</span>
main <span style="color:#000000;">=</span> <span style="color:#ff00e5;">do</span> <span style="color:#0066ff;">print</span> <span style="color:#000000;">$</span> feetAndInches <span style="color:#666666;">0</span>       <span style="color:#000000;">==</span> <span style="color:#666666;">"0 feet 0 inches"</span>
          <span style="color:#0066ff;">print</span> <span style="color:#000000;">$</span> feetAndInches <span style="color:#666666;">0.2785</span>  <span style="color:#000000;">==</span> <span style="color:#666666;">"9/32 inch"</span>
          <span style="color:#0066ff;">print</span> <span style="color:#000000;">$</span> feetAndInches <span style="color:#666666;">1.6895</span>  <span style="color:#000000;">==</span> <span style="color:#666666;">"1 and 11/16 inches"</span>
          <span style="color:#0066ff;">print</span> <span style="color:#000000;">$</span> feetAndInches <span style="color:#666666;">11.9999</span> <span style="color:#000000;">==</span> <span style="color:#666666;">"1 foot"</span>
          <span style="color:#0066ff;">print</span> <span style="color:#000000;">$</span> feetAndInches <span style="color:#666666;">12.2785</span> <span style="color:#000000;">==</span> <span style="color:#666666;">"1 foot 9/32 inch"</span>
          <span style="color:#0066ff;">print</span> <span style="color:#000000;">$</span> feetAndInches <span style="color:#666666;">71.9999</span> <span style="color:#000000;">==</span> <span style="color:#666666;">"6 feet"</span>
          <span style="color:#0066ff;">print</span> <span style="color:#000000;">$</span> feetAndInches <span style="color:#666666;">72</span>      <span style="color:#000000;">==</span> <span style="color:#666666;">"6 feet"</span>
          <span style="color:#0066ff;">print</span> <span style="color:#000000;">$</span> feetAndInches <span style="color:#666666;">72.3492</span> <span style="color:#000000;">==</span> <span style="color:#666666;">"6 feet 11/32 inch"</span>
          <span style="color:#0066ff;">print</span> <span style="color:#000000;">$</span> feetAndInches <span style="color:#666666;">72.9999</span> <span style="color:#000000;">==</span> <span style="color:#666666;">"6 feet 1 inch"</span>
          <span style="color:#0066ff;">print</span> <span style="color:#000000;">$</span> feetAndInches <span style="color:#666666;">73</span>      <span style="color:#000000;">==</span> <span style="color:#666666;">"6 feet 1 inch"</span>
          <span style="color:#0066ff;">print</span> <span style="color:#000000;">$</span> feetAndInches <span style="color:#666666;">73.0135</span> <span style="color:#000000;">==</span> <span style="color:#666666;">"6 feet 1 inch"</span>
          <span style="color:#0066ff;">print</span> <span style="color:#000000;">$</span> feetAndInches <span style="color:#666666;">73.0185</span> <span style="color:#000000;">==</span> <span style="color:#666666;">"6 feet 1 and 1/32 inches"</span>
          <span style="color:#0066ff;">print</span> <span style="color:#000000;">$</span> feetAndInches <span style="color:#666666;">73.8218</span> <span style="color:#000000;">==</span> <span style="color:#666666;">"6 feet 1 and 13/16 inches"</span></pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bonsaicode.wordpress.com/822/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bonsaicode.wordpress.com/822/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bonsaicode.wordpress.com/822/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bonsaicode.wordpress.com/822/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bonsaicode.wordpress.com/822/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bonsaicode.wordpress.com/822/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bonsaicode.wordpress.com/822/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bonsaicode.wordpress.com/822/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bonsaicode.wordpress.com/822/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bonsaicode.wordpress.com/822/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bonsaicode.wordpress.com/822/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bonsaicode.wordpress.com/822/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bonsaicode.wordpress.com/822/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bonsaicode.wordpress.com/822/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bonsaicode.wordpress.com&amp;blog=7474313&amp;post=822&amp;subd=bonsaicode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bonsaicode.wordpress.com/2011/07/01/programming-praxis-feet-and-inches/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/af10cfb68163f74baccb0107d33a207b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Remco Niemeijer</media:title>
		</media:content>
	</item>
		<item>
		<title>Programming Praxis &#8211; Thank God It’s Friday!</title>
		<link>http://bonsaicode.wordpress.com/2011/06/24/programming-praxis-thank-god-it%e2%80%99s-friday/</link>
		<comments>http://bonsaicode.wordpress.com/2011/06/24/programming-praxis-thank-god-it%e2%80%99s-friday/#comments</comments>
		<pubDate>Fri, 24 Jun 2011 09:45:38 +0000</pubDate>
		<dc:creator>Remco Niemeijer</dc:creator>
				<category><![CDATA[Programming Praxis]]></category>
		<category><![CDATA[bonsai]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[conway]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[gauss]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[kata]]></category>
		<category><![CDATA[praxis]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[sakamoto]]></category>
		<category><![CDATA[weekday]]></category>

		<guid isPermaLink="false">http://bonsaicode.wordpress.com/?p=818</guid>
		<description><![CDATA[In today&#8217;s Programming Praxis exercise, our goal is to implement three functions related to dates: two ways to calculate the day of the week for a given date and one to calculate the &#8216;doomsday&#8217; of a given year. Let&#8217;s get started, shall we? To make the results a bit easier to work with we make [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bonsaicode.wordpress.com&amp;blog=7474313&amp;post=818&amp;subd=bonsaicode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In <a title="Original exercise" href="http://programmingpraxis.com/2011/06/24/thank-god-its-friday/" target="_blank">today&#8217;s</a> Programming Praxis exercise, our goal is to implement three functions related to dates: two ways to calculate the day of the week for a given date and one to calculate the &#8216;doomsday&#8217; of a given year. Let&#8217;s get started, shall we?</p>
<p>To make the results a bit easier to work with we make a data type with the days of the week.</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">
<span style="color:#000000;font-weight:bold;">data</span> Weekday <span style="color:#ff0000;">=</span> Sun | Mon | Tue | Wed | Thu | Fri | Sat <span style="color:#000000;font-weight:bold;">deriving</span> <span style="color:#ff0000;">(</span>Enum<span style="color:#ff0000;">,</span> Eq<span style="color:#ff0000;">,</span> <span style="color:#0000ff;">Show</span><span style="color:#ff0000;">)</span>
</pre>
<p>The algorithms are just a bit a math.</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">
gauss <span style="color:#ff0000;">::</span> <span style="color:#0000ff;">Int</span> <span style="color:#ff0000;">-&gt;</span> <span style="color:#0000ff;">Int</span> <span style="color:#ff0000;">-&gt;</span> <span style="color:#0000ff;">Int</span> <span style="color:#ff0000;">-&gt;</span> Weekday
gauss y m d <span style="color:#ff0000;">=</span> <span style="color:#ec7f15;">toEnum</span> <span style="color:#ff0000;">$</span> <span style="color:#ec7f15;">mod</span> <span style="color:#ff0000;">(</span>d <span style="color:#ff0000;">+</span> <span style="color:#0000ff;font-weight:bold;">floor</span> <span style="color:#ff0000;">(</span><span style="color:#a900a9;">2.6</span> <span style="color:#ff0000;">*</span> <span style="color:#ec7f15;">fromIntegral</span>
  <span style="color:#ff0000;">(</span><span style="color:#ec7f15;">mod</span> <span style="color:#ff0000;">(</span>m <span style="color:#ff0000;">-</span> <span style="color:#a900a9;">2</span><span style="color:#ff0000;">)</span> <span style="color:#a900a9;">12</span><span style="color:#ff0000;">) -</span> <span style="color:#a900a9;">0.2</span><span style="color:#ff0000;">) +</span> y<span style="color:#ff0000;">' +</span> <span style="color:#ec7f15;">div</span> y<span style="color:#ff0000;">'</span> <span style="color:#a900a9;">4</span> <span style="color:#ff0000;">+</span> <span style="color:#ec7f15;">div</span> c <span style="color:#a900a9;">4</span> <span style="color:#ff0000;">-</span> <span style="color:#a900a9;">2</span><span style="color:#ff0000;">*</span>c<span style="color:#ff0000;">)</span> <span style="color:#a900a9;">7</span> <span style="color:#000000;font-weight:bold;">where</span>
    <span style="color:#ff0000;">(</span>c<span style="color:#ff0000;">,</span>y<span style="color:#ff0000;">') =</span> <span style="color:#ec7f15;">divMod</span> <span style="color:#ff0000;">(</span><span style="color:#000000;font-weight:bold;">if</span> m <span style="color:#ff0000;">&lt;</span> <span style="color:#a900a9;">3</span> <span style="color:#000000;font-weight:bold;">then</span> y <span style="color:#ff0000;">-</span> <span style="color:#a900a9;">1</span> <span style="color:#000000;font-weight:bold;">else</span> y<span style="color:#ff0000;">)</span> <span style="color:#a900a9;">100</span>

sakamoto <span style="color:#ff0000;">::</span> <span style="color:#0000ff;">Int</span> <span style="color:#ff0000;">-&gt;</span> <span style="color:#0000ff;">Int</span> <span style="color:#ff0000;">-&gt;</span> <span style="color:#0000ff;">Int</span> <span style="color:#ff0000;">-&gt;</span> Weekday
sakamoto y m d <span style="color:#ff0000;">=</span> <span style="color:#ec7f15;">toEnum</span> <span style="color:#ff0000;">$</span> <span style="color:#ec7f15;">mod</span> <span style="color:#ff0000;">(</span>y <span style="color:#ff0000;">+</span> <span style="color:#ec7f15;">div</span> y <span style="color:#a900a9;">4</span> <span style="color:#ff0000;">-</span> <span style="color:#ec7f15;">div</span> y <span style="color:#a900a9;">100</span> <span style="color:#ff0000;">+</span>
    <span style="color:#ec7f15;">div</span> y <span style="color:#a900a9;">400</span> <span style="color:#ff0000;">+ [</span><span style="color:#a900a9;">0</span><span style="color:#ff0000;">,</span><span style="color:#a900a9;">3</span><span style="color:#ff0000;">,</span><span style="color:#a900a9;">2</span><span style="color:#ff0000;">,</span><span style="color:#a900a9;">5</span><span style="color:#ff0000;">,</span><span style="color:#a900a9;">0</span><span style="color:#ff0000;">,</span><span style="color:#a900a9;">3</span><span style="color:#ff0000;">,</span><span style="color:#a900a9;">5</span><span style="color:#ff0000;">,</span><span style="color:#a900a9;">1</span><span style="color:#ff0000;">,</span><span style="color:#a900a9;">4</span><span style="color:#ff0000;">,</span><span style="color:#a900a9;">6</span><span style="color:#ff0000;">,</span><span style="color:#a900a9;">2</span><span style="color:#ff0000;">,</span><span style="color:#a900a9;">4</span><span style="color:#ff0000;">] !! (</span>m <span style="color:#ff0000;">-</span> <span style="color:#a900a9;">1</span><span style="color:#ff0000;">) +</span> d<span style="color:#ff0000;">)</span> <span style="color:#a900a9;">7</span>

conway <span style="color:#ff0000;">::</span> <span style="color:#0000ff;">Int</span> <span style="color:#ff0000;">-&gt;</span> Weekday
conway y <span style="color:#ff0000;">=</span> <span style="color:#ec7f15;">toEnum</span> <span style="color:#ff0000;">$</span> <span style="color:#ec7f15;">mod</span> <span style="color:#ff0000;">(</span>q <span style="color:#ff0000;">+</span> r <span style="color:#ff0000;">+</span> <span style="color:#ec7f15;">div</span> r <span style="color:#a900a9;">4</span> <span style="color:#ff0000;">+</span> <span style="color:#a900a9;">5</span><span style="color:#ff0000;">*(</span>c<span style="color:#ff0000;">+</span><span style="color:#a900a9;">1</span><span style="color:#ff0000;">) +</span> <span style="color:#ec7f15;">div</span> c <span style="color:#a900a9;">4</span> <span style="color:#ff0000;">+</span> <span style="color:#a900a9;">4</span><span style="color:#ff0000;">)</span> <span style="color:#a900a9;">7</span>
    <span style="color:#000000;font-weight:bold;">where</span> <span style="color:#ff0000;">(</span>c<span style="color:#ff0000;">, (</span>q<span style="color:#ff0000;">,</span>r<span style="color:#ff0000;">)) = (</span><span style="color:#ec7f15;">div</span> y <span style="color:#a900a9;">100</span><span style="color:#ff0000;">,</span> <span style="color:#ec7f15;">divMod</span> <span style="color:#ff0000;">(</span><span style="color:#ec7f15;">mod</span> y <span style="color:#a900a9;">100</span><span style="color:#ff0000;">)</span> <span style="color:#a900a9;">12</span><span style="color:#ff0000;">)</span>
</pre>
<p>Some tests to see if everything is working properly:</p>
<pre style="color:#000000;background-color:#ffffff;font-size:9pt;font-family:'Courier New';">
main <span style="color:#ff0000;">::</span> <span style="color:#0000ff;">IO</span> <span style="color:#ff0000;">()</span>
main <span style="color:#ff0000;">=</span> <span style="color:#000000;font-weight:bold;">do</span> <span style="color:#ec7f15;">print</span> <span style="color:#ff0000;">$</span> gauss <span style="color:#a900a9;">2011 6 24</span> <span style="color:#ff0000;">==</span> Fri
          <span style="color:#ec7f15;">print</span> <span style="color:#ff0000;">$</span> sakamoto <span style="color:#a900a9;">2011 6 24</span> <span style="color:#ff0000;">==</span> Fri
          <span style="color:#ec7f15;">print</span> <span style="color:#ff0000;">$</span> conway <span style="color:#a900a9;">2011</span> <span style="color:#ff0000;">==</span> Mon
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bonsaicode.wordpress.com/818/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bonsaicode.wordpress.com/818/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bonsaicode.wordpress.com/818/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bonsaicode.wordpress.com/818/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bonsaicode.wordpress.com/818/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bonsaicode.wordpress.com/818/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bonsaicode.wordpress.com/818/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bonsaicode.wordpress.com/818/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bonsaicode.wordpress.com/818/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bonsaicode.wordpress.com/818/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bonsaicode.wordpress.com/818/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bonsaicode.wordpress.com/818/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bonsaicode.wordpress.com/818/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bonsaicode.wordpress.com/818/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bonsaicode.wordpress.com&amp;blog=7474313&amp;post=818&amp;subd=bonsaicode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bonsaicode.wordpress.com/2011/06/24/programming-praxis-thank-god-it%e2%80%99s-friday/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/af10cfb68163f74baccb0107d33a207b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Remco Niemeijer</media:title>
		</media:content>
	</item>
	</channel>
</rss>
