<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Erlends hjem i skyene &#187; haskell</title>
	<atom:link href="http://hamberg.no/erlend/category/haskell/feed/" rel="self" type="application/rss+xml" />
	<link>http://hamberg.no/erlend</link>
	<description>meta = &#38;meta;</description>
	<lastBuildDate>Tue, 17 Jan 2012 18:24:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>BK-Trees</title>
		<link>http://hamberg.no/erlend/2012/01/17/bk-trees/</link>
		<comments>http://hamberg.no/erlend/2012/01/17/bk-trees/#comments</comments>
		<pubDate>Tue, 17 Jan 2012 18:24:45 +0000</pubDate>
		<dc:creator>Erlend</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[english]]></category>
		<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://hamberg.no/erlend/?p=1274</guid>
		<description><![CDATA[A BK-Tree is a really cool data structure for building a "dictionary" of similar words. It can be used to guess that you meant “cat” when you wrote “cta”. It works by building a tree with words from a dictionary by using the first word as a root node and then attaching subsequent words ...]]></description>
			<content:encoded><![CDATA[<p class="wp-flattr-button"></p><p>A <a title="Wikipedia: BK Tree" href="http://en.wikipedia.org/wiki/BK-tree">BK-Tree</a> is a really cool data structure for building a &#8220;dictionary&#8221; of similar words. It can be used to guess that you meant “cat” when you wrote “cta”. It works by building a tree with words from a dictionary by using the first word as a root node and then attaching subsequent words with a branch of length <em>d(root_word, new_word)</em> where <em>d</em> is a function for finding the “distance” between two words. This is usually the <a title="Wikipedia: Levenshtein distance" href="http://en.wikipedia.org/wiki/Levenshtein_distance">Levenshtein distance</a>, i.e. the minimum number of edits needed to transform one string into the other.</p>
<p>If the branch is “taken”, i.e. there is already another word connected along a branch of length <em>d(root_word, new_word)</em>, the insert operation is done on this word instead. That is the whole algorithm for constructing the BK-tree.</p>
<p>So if we have the following list of words: <code>["cat", "cut", "hat", "man", "hit"]</code>, we will start by creating a “cat” node with no children. To add “cut” we calculate the Levensteinh distance to be one and insert it under the “cat” node.</p>
<div id="attachment_1304" class="wp-caption aligncenter" style="width: 139px"><a href="http://hamberg.no/erlend/wp-content/uploads/2011/12/bk16.png"><img class="size-full wp-image-1304" title="bk1" src="http://hamberg.no/erlend/wp-content/uploads/2011/12/bk16.png" alt="" width="129" height="275" /></a><p class="wp-caption-text">1) Cut is inserted under cat with a branch of length one</p></div>
<div id="attachment_1305" class="wp-caption aligncenter" style="width: 291px"><a href="http://hamberg.no/erlend/wp-content/uploads/2011/12/bk24.png"><img class="size-full wp-image-1305" title="bk2" src="http://hamberg.no/erlend/wp-content/uploads/2011/12/bk24.png" alt="" width="281" height="275" /></a><p class="wp-caption-text">2) The Levensteinh distance between “cat” and “man” is two, so “man” is connected with a branch of length two</p></div>
<div id="attachment_1306" class="wp-caption aligncenter" style="width: 291px"><a href="http://hamberg.no/erlend/wp-content/uploads/2011/12/bk34.png"><img class="size-full wp-image-1306" title="bk3" src="http://hamberg.no/erlend/wp-content/uploads/2011/12/bk34.png" alt="" width="281" height="458" /></a><p class="wp-caption-text">3) d(“hat”,“cat”) = 1, so the insertion operation is done on the “cut” node, and “hat” is connected to “cut” with a branch of length two (d(“cut”,“hat”)=2)</p></div>
<div id="attachment_1306" class="wp-caption aligncenter" style="width: 291px"><a href="http://hamberg.no/erlend/wp-content/uploads/2011/12/bk45.png"><img class="alignleft size-full wp-image-1307" title="bk4" src="http://hamberg.no/erlend/wp-content/uploads/2011/12/bk45.png" alt="" width="281" height="458" /></a><p class="wp-caption-text">4) d(“cat”,“man”) = 2, so the insertion operation is done on the “man” node, and “hit” is connected to “man” with a branch of length three (d(“man”,“hit”)=3)</p></div>
<p>The lookup algorithm is also quite simple: We find the distance from the query word to the root node and then find all the child nodes in the range <em>[(d-n),(d+n)]</em> where <em>d</em> is the distance and <em>n</em> is the maximum distance we will allow. We then recursively query the nodes that matched. The result is a list of words satisfying <em>d(query_word, word) ≤ n</em>.</p>
<p>The following code is a Haskell implementation of BK-Trees.</p>
<p><script src="https://gist.github.com/1447021.js"> </script></p>
<p>It even works! Here are a few examples where I used a dictionary of ~5800 words:</p>
<p><code>λ&gt; query 1 "thie" bk_tree<br />
["tie","the","this","thief"]<br />
λ&gt; query 1 "catt" bk_tree<br />
["cat","cart"]</code></p>
 <p><a href="http://hamberg.no/erlend/?flattrss_redirect&amp;id=1274&amp;md5=49a65a9c7a042ff860d8f4528083e3aa" title="Flattr" target="_blank"><img src="http://hamberg.no/erlend/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://hamberg.no/erlend/2012/01/17/bk-trees/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Game of Life</title>
		<link>http://hamberg.no/erlend/2010/03/20/game-of-life/</link>
		<comments>http://hamberg.no/erlend/2010/03/20/game-of-life/#comments</comments>
		<pubDate>Sat, 20 Mar 2010 18:58:32 +0000</pubDate>
		<dc:creator>Erlend</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[english]]></category>
		<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://hamberg.no/erlend/?p=1094</guid>
		<description><![CDATA[While reading about cellular automata in preparation for an essay it struck me that I have never actually written Conway's Game of Life. No, really!

To correct this embarrassing fact I quickly wrote a version in Haskell using the GLUT bindings.





It is very simple, but it works. :-)
import Graphics.UI.GLUT hiding (get)
import Graphics.Rendering.GLU.Raw (gluOrtho2D)
import Data.IORef
import System.Random

-- ...]]></description>
			<content:encoded><![CDATA[<p class="wp-flattr-button"></p><p>While reading about cellular automata in preparation for an essay it struck me that I have never actually written <a href="http://en.wikipedia.org/wiki/Conway's Game of Life">Conway&#8217;s Game of Life</a>. No, really!</p>
<p>To correct this embarrassing fact I quickly wrote a version in Haskell using the <a href="http://hackage.haskell.org/package/GLUT">GLUT bindings</a>.</p>
<div id="attachment_1097" class="wp-caption aligncenter" style="width: 310px"><a href="http://hamberg.no/erlend/wp-content/uploads/2010/03/gol.png"><img class="size-medium wp-image-1097" title="Conway's Game of Life" src="http://hamberg.no/erlend/wp-content/uploads/2010/03/gol-300x232.png" alt="" width="300" height="232" /></a><p class="wp-caption-text">Conway&#39;s Game of Life</p></div>
<p><span id="more-1094"></span></p>
<p>It is very simple, but it works. <img src='http://hamberg.no/erlend/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">import</span> Graphics<span style="color: #339933; font-weight: bold;">.</span>UI<span style="color: #339933; font-weight: bold;">.</span>GLUT <span style="color: #06c; font-weight: bold;">hiding</span> <span style="color: green;">&#40;</span>get<span style="color: green;">&#41;</span>
<span style="color: #06c; font-weight: bold;">import</span> Graphics<span style="color: #339933; font-weight: bold;">.</span>Rendering<span style="color: #339933; font-weight: bold;">.</span>GLU<span style="color: #339933; font-weight: bold;">.</span>Raw <span style="color: green;">&#40;</span>gluOrtho2D<span style="color: green;">&#41;</span>
<span style="color: #06c; font-weight: bold;">import</span> Data<span style="color: #339933; font-weight: bold;">.</span>IORef
<span style="color: #06c; font-weight: bold;">import</span> System<span style="color: #339933; font-weight: bold;">.</span>Random
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- dimensions of our cellular space</span>
width  <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">80</span> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span>
height <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">60</span> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- takes a two-dimensional list and returns the neighbours of (x,y)</span>
neighbours <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-</span>&amp;gt; <span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-</span>&amp;gt; <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span>
neighbours m <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span>y<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">map</span> <span style="color: green;">&#40;</span>\<span style="color: green;">&#40;</span>x'<span style="color: #339933; font-weight: bold;">,</span>y'<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-</span>&amp;gt; m <span style="color: #339933; font-weight: bold;">!!</span> y' <span style="color: #339933; font-weight: bold;">!!</span> x'<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">filter</span> valid neighbours'
    <span style="color: #06c; font-weight: bold;">where</span> height'       <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">length</span> m
          width'        <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">length</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">head</span> m<span style="color: green;">&#41;</span>
          valid <span style="color: green;">&#40;</span>x'<span style="color: #339933; font-weight: bold;">,</span>y'<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> x' &amp;gt;<span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">0</span> &amp;amp;&amp;amp; x' &amp;lt; width' &amp;amp;&amp;amp; y' &amp;gt;<span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">0</span> &amp;amp;&amp;amp; y' &amp;lt; height'
          neighbours'   <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span>y<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span>y<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span>y<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: #5d478b; font-style: italic;">-- neighbours over</span>
                           <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span>y<span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span>y<span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span>             <span style="color: #5d478b; font-style: italic;">-- neighbours left/right</span>
                           <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span>y<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span>y<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span>y<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: green;">&#41;</span><span style="color: green;">&#93;</span> <span style="color: #5d478b; font-style: italic;">-- neighbours under</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- updates all cells according to the rules in liveOrDead</span>
update <span style="color: #339933; font-weight: bold;">::</span> IORef <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Bool</span><span style="color: green;">&#93;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-</span>&amp;gt; <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
update c <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
    cells &amp;lt;<span style="color: #339933; font-weight: bold;">-</span> readIORef c
&nbsp;
    <span style="color: #06c; font-weight: bold;">let</span> coords <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span>y<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">|</span> y &amp;lt;<span style="color: #339933; font-weight: bold;">-</span> <span style="color: green;">&#91;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">..</span><span style="color: green;">&#40;</span>height<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span><span style="color: green;">&#41;</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> x &amp;lt;<span style="color: #339933; font-weight: bold;">-</span> <span style="color: green;">&#91;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">..</span><span style="color: green;">&#40;</span>width<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span><span style="color: green;">&#41;</span><span style="color: green;">&#93;</span><span style="color: green;">&#93;</span>
&nbsp;
    nextGen &amp;lt;<span style="color: #339933; font-weight: bold;">-</span> <span style="font-weight: bold;">mapM</span> <span style="color: green;">&#40;</span>\<span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span>y<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-</span>&amp;gt; <span style="color: #06c; font-weight: bold;">do</span>
            <span style="color: #06c; font-weight: bold;">let</span> cell <span style="color: #339933; font-weight: bold;">=</span> cells <span style="color: #339933; font-weight: bold;">!!</span> y <span style="color: #339933; font-weight: bold;">!!</span> x
            <span style="color: #06c; font-weight: bold;">let</span> ns   <span style="color: #339933; font-weight: bold;">=</span> neighbours cells <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span>y<span style="color: green;">&#41;</span>
            <span style="font-weight: bold;">return</span> <span style="color: #339933; font-weight: bold;">$</span> liveOrDead cell <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="font-weight: bold;">length</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">filter</span> <span style="font-weight: bold;">id</span><span style="color: green;">&#41;</span> ns<span style="color: green;">&#41;</span>
        <span style="color: green;">&#41;</span> coords
&nbsp;
    writeIORef c <span style="color: green;">&#40;</span>nLists width nextGen<span style="color: green;">&#41;</span>
    display c
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- survival rule: a live cell only lives on if it has 2 or 3 live neighbours</span>
<span style="color: #5d478b; font-style: italic;">-- birth rule: a dead cell becomes a live cell if it has 3 live neighbours</span>
liveOrDead <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Bool</span> <span style="color: #339933; font-weight: bold;">-</span>&amp;gt; <span style="color: #cccc00; font-weight: bold;">Int</span> <span style="color: #339933; font-weight: bold;">-</span>&amp;gt; <span style="color: #cccc00; font-weight: bold;">Bool</span>
liveOrDead True nLive <span style="color: #339933; font-weight: bold;">=</span> nLive `<span style="font-weight: bold;">elem</span>` <span style="color: green;">&#91;</span><span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">3</span><span style="color: green;">&#93;</span>
liveOrDead False nLive <span style="color: #339933; font-weight: bold;">=</span> nLive <span style="color: #339933; font-weight: bold;">==</span> <span style="color: red;">3</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- utility function: split a list into sublists of length n</span>
nLists <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span> <span style="color: #339933; font-weight: bold;">-</span>&amp;gt; <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-</span>&amp;gt; <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span><span style="color: green;">&#93;</span>
nLists <span style="color: #339933; font-weight: bold;">_</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
nLists n ls <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">take</span> n ls : nLists n <span style="color: green;">&#40;</span><span style="font-weight: bold;">drop</span> n ls<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- utility function: draws a square at (x,y) with size w×h</span>
drawQuad <span style="color: #339933; font-weight: bold;">::</span> GLdouble <span style="color: #339933; font-weight: bold;">-</span>&amp;gt; GLdouble <span style="color: #339933; font-weight: bold;">-</span>&amp;gt; GLdouble <span style="color: #339933; font-weight: bold;">-</span>&amp;gt; GLdouble <span style="color: #339933; font-weight: bold;">-</span>&amp;gt; <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
drawQuad x y w h <span style="color: #339933; font-weight: bold;">=</span>
    renderPrimitive Quads <span style="color: #339933; font-weight: bold;">$</span> <span style="color: #06c; font-weight: bold;">do</span>
        vertex <span style="color: green;">&#40;</span>Vertex3  x     y    <span style="color: red;">0</span><span style="color: green;">&#41;</span>
        vertex <span style="color: green;">&#40;</span>Vertex3 <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">+</span>w<span style="color: green;">&#41;</span>  y    <span style="color: red;">0</span><span style="color: green;">&#41;</span>
        vertex <span style="color: green;">&#40;</span>Vertex3 <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">+</span>w<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>y<span style="color: #339933; font-weight: bold;">-</span>h<span style="color: green;">&#41;</span> <span style="color: red;">0</span><span style="color: green;">&#41;</span>
        vertex <span style="color: green;">&#40;</span>Vertex3  x    <span style="color: green;">&#40;</span>y<span style="color: #339933; font-weight: bold;">-</span>h<span style="color: green;">&#41;</span> <span style="color: red;">0</span><span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- draw each cell as a coloured square</span>
display <span style="color: #339933; font-weight: bold;">::</span> IORef <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Bool</span><span style="color: green;">&#93;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-</span>&amp;gt; <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
display c <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
    cells &amp;lt;<span style="color: #339933; font-weight: bold;">-</span> readIORef c
&nbsp;
    <span style="color: #06c; font-weight: bold;">let</span> h <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">fromIntegral</span> height
    <span style="color: #06c; font-weight: bold;">let</span> w <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">fromIntegral</span> width
&nbsp;
    <span style="font-weight: bold;">mapM_</span> <span style="color: green;">&#40;</span>\<span style="color: green;">&#40;</span>n<span style="color: #339933; font-weight: bold;">,</span>b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-</span>&amp;gt; <span style="color: #06c; font-weight: bold;">do</span>
        <span style="color: #06c; font-weight: bold;">if</span> b
          <span style="color: #06c; font-weight: bold;">then</span> currentColor <span style="color: #339933; font-weight: bold;">$=</span> Color4 <span style="color: red;">1.0</span> <span style="color: red;">0.8</span> <span style="color: red;">0.6</span> <span style="color: red;">1.0</span>
          <span style="color: #06c; font-weight: bold;">else</span> currentColor <span style="color: #339933; font-weight: bold;">$=</span> Color4 <span style="color: red;">0.4</span> <span style="color: red;">0.5</span> <span style="color: red;">0.4</span> <span style="color: red;">1.0</span>
        <span style="color: #06c; font-weight: bold;">let</span> x <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">/</span>w<span style="color: #339933; font-weight: bold;">*</span><span style="font-weight: bold;">fromIntegral</span> <span style="color: green;">&#40;</span>n `<span style="font-weight: bold;">mod</span>` width<span style="color: green;">&#41;</span>
        <span style="color: #06c; font-weight: bold;">let</span> y <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">-</span><span style="color: green;">&#40;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">/</span>h<span style="color: #339933; font-weight: bold;">*</span><span style="font-weight: bold;">fromIntegral</span> <span style="color: green;">&#40;</span>n `<span style="font-weight: bold;">div</span>` width<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
        drawQuad x y <span style="color: green;">&#40;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">/</span>w<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">/</span>h<span style="color: green;">&#41;</span>
        <span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">zip</span> <span style="color: green;">&#91;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">..</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">concat</span> cells<span style="color: green;">&#41;</span>
&nbsp;
    swapBuffers
&nbsp;
main <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
main <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
    g &amp;lt;<span style="color: #339933; font-weight: bold;">-</span> newStdGen
    <span style="color: #339933; font-weight: bold;">_</span> &amp;lt;<span style="color: #339933; font-weight: bold;">-</span> getArgsAndInitialize
&nbsp;
    <span style="color: #5d478b; font-style: italic;">-- random starting values</span>
    cells &amp;lt;<span style="color: #339933; font-weight: bold;">-</span> newIORef <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>nLists width <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">take</span> <span style="color: green;">&#40;</span>width<span style="color: #339933; font-weight: bold;">*</span>height<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">.</span> randoms<span style="color: green;">&#41;</span> g<span style="color: green;">&#41;</span>
&nbsp;
    <span style="color: #339933; font-weight: bold;">_</span> &amp;lt;<span style="color: #339933; font-weight: bold;">-</span> createWindow <span style="background-color: #3cb371;">&quot;Conway's Game of Life&quot;</span>
    initialDisplayMode    <span style="color: #339933; font-weight: bold;">$=</span> <span style="color: green;">&#91;</span>DoubleBuffered<span style="color: green;">&#93;</span>
    windowSize            <span style="color: #339933; font-weight: bold;">$=</span> Size <span style="color: red;">800</span> <span style="color: red;">600</span>
    displayCallback       <span style="color: #339933; font-weight: bold;">$=</span> display cells
    idleCallback          <span style="color: #339933; font-weight: bold;">$=</span> Just <span style="color: green;">&#40;</span>update cells<span style="color: green;">&#41;</span>
    gluOrtho2D <span style="color: red;">0</span> <span style="color: red;">1</span> <span style="color: red;">0</span> <span style="color: red;">1</span>    <span style="color: #5d478b; font-style: italic;">-- orthogonal projection</span>
    mainLoop              <span style="color: #5d478b; font-style: italic;">-- start main loop</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://hamberg.no/erlend/2010/03/20/game-of-life/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HsUnixCompat.hs on Debian</title>
		<link>http://hamberg.no/erlend/2010/01/17/hsunixcompat-hs-on-debian/</link>
		<comments>http://hamberg.no/erlend/2010/01/17/hsunixcompat-hs-on-debian/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 17:44:28 +0000</pubDate>
		<dc:creator>Erlend</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://hamberg.no/erlend/?p=1091</guid>
		<description><![CDATA[Just so that a others (hopefully) will be spared the annoying and time-consuming work of tracking down the source of the following error message:
* Missing header file: HsUnixCompat.h
On my Debian system (Debian 5.0.3 – "Lenny") the missing package was libbsd-dev:
$ cabal unpack unix-compat
Unpacking unix-compat-0.1.2.1...
$ cd unix-compat-0.1.2.1/
$ runhaskell Setup.lhs configure -v3

/usr/bin/gcc returned ExitFailure 1 with ...]]></description>
			<content:encoded><![CDATA[<p class="wp-flattr-button"></p><p>Just so that a others (hopefully) will be spared the annoying and time-consuming work of tracking down the source of the following error message:</p>
<blockquote><p>* Missing header file: HsUnixCompat.h</p></blockquote>
<p>On my Debian system (Debian 5.0.3 – &#8220;Lenny&#8221;) the missing package was <strong>libbsd-dev</strong>:</p>
<blockquote><p>$ <strong>cabal unpack unix-compat</strong><br />
Unpacking unix-compat-0.1.2.1&#8230;<br />
$ <strong>cd unix-compat-0.1.2.1/</strong><br />
$ <strong>runhaskell Setup.lhs configure -v3</strong><br />
[…]<br />
/usr/bin/gcc returned ExitFailure 1 with error message:<br />
In file included from include/HsUnixCompat.h:1,<br />
from /tmp/18515.c:1:<br />
/usr/lib/ghc-6.10.4/unix-2.3.2.0/include/HsUnix.h:79:21: error: libutil.h: No such file or directory<br />
$ <strong>apt-file search libutil.h</strong><br />
libbsd-dev: /usr/include/libutil.h</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://hamberg.no/erlend/2010/01/17/hsunixcompat-hs-on-debian/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

