<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Mark Needham</title>
	
	<link>http://www.markhneedham.com/blog</link>
	<description>Thoughts on Software Development</description>
	<lastBuildDate>Wed, 16 May 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/MarkNeedham" /><feedburner:info uri="markneedham" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>MarkNeedham</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Haskell: Writing a custom equality operator</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/S_OjdxEJtdQ/</link>
		<comments>http://www.markhneedham.com/blog/2012/05/16/haskell-writing-a-custom-equality-operator/#comments</comments>
		<pubDate>Wed, 16 May 2012 13:16:48 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=4295</guid>
		<description><![CDATA[In the comments on my post about generating random numbers to test a function David Turner suggested that this was exactly the use case for which QuickCheck was intended for so I&#8217;ve been learning a bit more about that this week. I started with a simple property to check that the brute force (bf) and [...]]]></description>
			<content:encoded><![CDATA[<p>In the comments on my post about <a href="http://www.markhneedham.com/blog/2012/05/08/haskell-generating-random-numbers/">generating random numbers</a> to test a function David Turner suggested that this was exactly the use case for which <a href="http://book.realworldhaskell.org/read/testing-and-quality-assurance.html">QuickCheck</a> was intended for so I&#8217;ve been learning a bit more about that this week.</p>
<p>I started with a simple property to check that the brute force (bf) and divide and conquer (dc) versions of the algorithm returned the same result, assuming that there were enough values in the list to have a closest pair:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">prop<span style="color: #339933; font-weight: bold;">_</span>closest<span style="color: #339933; font-weight: bold;">_</span>pairs xs <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">length</span> xs <span style="color: #339933; font-weight: bold;">&gt;=</span> <span style="color: red;">2</span> <span style="color: #339933; font-weight: bold;">==&gt;</span> dcClosest xs <span style="color: #339933; font-weight: bold;">==</span> <span style="color: green;">&#40;</span>fromJust <span style="color: #339933; font-weight: bold;">$</span> bfClosest xs<span style="color: green;">&#41;</span></pre></div></div>

<p>I could then run that as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: #06c; font-weight: bold;">import</span> QuickCheck<span style="color: #339933; font-weight: bold;">.</span>Test
<span style="color: #339933; font-weight: bold;">&gt;</span> quickCheck<span style="color: green;">&#40;</span>prop<span style="color: #339933; font-weight: bold;">_</span>closest<span style="color: #339933; font-weight: bold;">_</span>pairs <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Double</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: #cccc00; font-weight: bold;">Double</span><span style="color: green;">&#41;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> Property<span style="color: green;">&#41;</span></pre></div></div>

<p>It failed pretty quickly because the bf and dc versions of the algorithm sometimes return the pairs in a different order.</p>
<p>e.g. bf may say the closest pair is (2.0, 0.0), (2.1, 1.1) while dc says it&#8217;s (2.1, 1.1), (2.0, 0.0) which will lead to the quick check property failing because those values aren&#8217;t equal:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: red;">2.0</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">0.0</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#40;</span><span style="color: red;">2.1</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">1.1</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>  <span style="color: #339933; font-weight: bold;">==</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: red;">2.1</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">1.1</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#40;</span><span style="color: red;">2.0</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">0.0</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
False</pre></div></div>

<p>The best way I could think of to get around this problem was to create a type to represent a pair of points and then write a custom equality operator.</p>
<p>I initially ended up with the following:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">type</span> Point a <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>a<span style="color: #339933; font-weight: bold;">,</span> a<span style="color: green;">&#41;</span>
<span style="color: #06c; font-weight: bold;">data</span> Pair a <span style="color: #339933; font-weight: bold;">=</span> P <span style="color: green;">&#40;</span>Point a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>Point a<span style="color: green;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">instance</span> <span style="color: #cccc00; font-weight: bold;">Eq</span> <span style="color: green;">&#40;</span>Pair a<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
	P a b <span style="color: #339933; font-weight: bold;">==</span> P c d <span style="color: #339933; font-weight: bold;">=</span> a <span style="color: #339933; font-weight: bold;">==</span> c <span style="color: #339933; font-weight: bold;">&amp;&amp;</span> b <span style="color: #339933; font-weight: bold;">==</span> d <span style="color: #339933; font-weight: bold;">||</span> a <span style="color: #339933; font-weight: bold;">==</span> d <span style="color: #339933; font-weight: bold;">&amp;&amp;</span> b <span style="color: #339933; font-weight: bold;">==</span> c</pre></div></div>

<p>Which didn&#8217;t actually compile:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">qc_test.hs:41:58:
    No instance for (Eq a)
      arising from a use of `=='
    In the second argument of `(&amp;&amp;)', namely `b == c'
    In the second argument of `(||)', namely `a == d &amp;&amp; b == c'
    In the expression: a == c &amp;&amp; b == d || a == d &amp;&amp; b == c</pre></div></div>

<p>The problem is that while we&#8217;ve made Pair an instance of the Equality type class there&#8217;s no guarantee that the value contained inside it is an instance of the Equality type class which means we might not be able to compare its values.</p>
<p>We need to add a class constraint to make sure that <a href="http://learnyouahaskell.com/making-our-own-types-and-typeclasses#typeclasses-102">the value inside P is a part of Eq</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Eq</span> a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> <span style="color: #cccc00; font-weight: bold;">Eq</span> <span style="color: green;">&#40;</span>Pair a<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
	P a b <span style="color: #339933; font-weight: bold;">==</span> P c d <span style="color: #339933; font-weight: bold;">=</span> a <span style="color: #339933; font-weight: bold;">==</span> c <span style="color: #339933; font-weight: bold;">&amp;&amp;</span> b <span style="color: #339933; font-weight: bold;">==</span> d <span style="color: #339933; font-weight: bold;">||</span> a <span style="color: #339933; font-weight: bold;">==</span> d <span style="color: #339933; font-weight: bold;">&amp;&amp;</span> b <span style="color: #339933; font-weight: bold;">==</span> c</pre></div></div>

<p>Now we&#8217;re saying that we want to make Pair an instance of the Equality type class but only when the value that Pair contains is already an instance of the Equality type class.</p>
<p>In this case we&#8217;re just storing pairs of doubles inside the Pair so it will work fine.</p>
<p>Now if we compare the two points from above we&#8217;ll see that they&#8217;re equal:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #339933; font-weight: bold;">&gt;</span> P <span style="color: green;">&#40;</span><span style="color: red;">2.0</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">0.0</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="color: red;">2.1</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">1.1</span><span style="color: green;">&#41;</span>  <span style="color: #339933; font-weight: bold;">==</span> P <span style="color: green;">&#40;</span><span style="color: red;">2.1</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">1.1</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="color: red;">2.0</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">0.0</span><span style="color: green;">&#41;</span>
True</pre></div></div>

<p>I had to go and change the existing code to make use of this new but it didn&#8217;t take more than 5-10 minutes to do that.</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/S_OjdxEJtdQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2012/05/16/haskell-writing-a-custom-equality-operator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2012/05/16/haskell-writing-a-custom-equality-operator/</feedburner:origLink></item>
		<item>
		<title>Haskell: Removing if statements</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/vLlzF5HBCI4/</link>
		<comments>http://www.markhneedham.com/blog/2012/05/12/haskell-removing-if-statements/#comments</comments>
		<pubDate>Sat, 12 May 2012 15:46:31 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=4289</guid>
		<description><![CDATA[When I was looking over my solution to the closest pairs algorithm which I wrote last week I realised there there were quite a few if statements, something I haven&#8217;t seen in other Haskell code I&#8217;ve read. This is the initial version that I wrote: dcClosest :: &#40;Ord a, Floating a&#41; =&#62; &#91;Point a&#93; -&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>When I was looking over <a href="http://www.markhneedham.com/blog/2012/05/09/haskell-closest-pairs-algorithm/">my solution to the closest pairs algorithm</a> which I wrote last week I realised there there were quite a few if statements, something I haven&#8217;t seen in other Haskell code I&#8217;ve read.</p>
<p>This is the initial version that I wrote:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">dcClosest <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Ord</span> a<span style="color: #339933; font-weight: bold;">,</span> <span style="color: #cccc00; font-weight: bold;">Floating</span> a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> <span style="color: green;">&#91;</span>Point a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>Point a<span style="color: #339933; font-weight: bold;">,</span> Point a<span style="color: green;">&#41;</span>
dcClosest pairs
  <span style="color: #06c; font-weight: bold;">if</span> <span style="font-weight: bold;">length</span> pairs <span style="color: #339933; font-weight: bold;">&lt;=</span> <span style="color: red;">3</span> <span style="color: #06c; font-weight: bold;">then</span> <span style="color: #339933; font-weight: bold;">=</span> fromJust <span style="color: #339933; font-weight: bold;">$</span> bfClosest pairs    
  <span style="color: #06c; font-weight: bold;">else</span> 
    <span style="font-weight: bold;">foldl</span> <span style="color: green;">&#40;</span>\closest <span style="color: green;">&#40;</span>p1:p2:<span style="color: #339933; font-weight: bold;">_</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #06c; font-weight: bold;">if</span> distance <span style="color: green;">&#40;</span>p1<span style="color: #339933; font-weight: bold;">,</span> p2<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&lt;</span> distance closest <span style="color: #06c; font-weight: bold;">then</span> <span style="color: green;">&#40;</span>p1<span style="color: #339933; font-weight: bold;">,</span> p2<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">else</span> closest<span style="color: green;">&#41;</span> 
          closestPair 
          <span style="color: green;">&#40;</span>windowed <span style="color: red;">2</span> pairsWithinMinimumDelta<span style="color: green;">&#41;</span>
  <span style="color: #06c; font-weight: bold;">where</span> sortedByX <span style="color: #339933; font-weight: bold;">=</span> sortBy <span style="font-weight: bold;">compare</span> pairs	      
        <span style="color: green;">&#40;</span>leftByX:rightByX:<span style="color: #339933; font-weight: bold;">_</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> chunk <span style="color: green;">&#40;</span><span style="font-weight: bold;">length</span> sortedByX `<span style="font-weight: bold;">div</span>` <span style="color: red;">2</span><span style="color: green;">&#41;</span> sortedByX
        closestPair <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">if</span> distance closestLeftPair <span style="color: #339933; font-weight: bold;">&lt;</span> distance closestRightPair <span style="color: #06c; font-weight: bold;">then</span> closestLeftPair <span style="color: #06c; font-weight: bold;">else</span> closestRightPair  
          <span style="color: #06c; font-weight: bold;">where</span> closestLeftPair <span style="color: #339933; font-weight: bold;">=</span>  dcClosest leftByX
                closestRightPair <span style="color: #339933; font-weight: bold;">=</span> dcClosest rightByX
        pairsWithinMinimumDelta <span style="color: #339933; font-weight: bold;">=</span> sortBy <span style="color: green;">&#40;</span><span style="font-weight: bold;">compare</span> `on` <span style="font-weight: bold;">snd</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">filter</span> withinMinimumDelta sortedByX
          <span style="color: #06c; font-weight: bold;">where</span> withinMinimumDelta <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span> <span style="color: #339933; font-weight: bold;">_</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">abs</span> <span style="color: green;">&#40;</span>xMidPoint <span style="color: #339933; font-weight: bold;">-</span> x<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&lt;=</span> distance closestPair   
                  <span style="color: #06c; font-weight: bold;">where</span> <span style="color: green;">&#40;</span>xMidPoint<span style="color: #339933; font-weight: bold;">,</span> <span style="color: #339933; font-weight: bold;">_</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">last</span> leftByX</pre></div></div>

<p>We can remove the first if statement which checks the length of the list and replace it with pattern matching code like so:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">dcClosest <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Ord</span> a<span style="color: #339933; font-weight: bold;">,</span> <span style="color: #cccc00; font-weight: bold;">Floating</span> a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> <span style="color: green;">&#91;</span>Point a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>Point a<span style="color: #339933; font-weight: bold;">,</span> Point a<span style="color: green;">&#41;</span>
dcClosest pairs
  <span style="color: #339933; font-weight: bold;">|</span> <span style="font-weight: bold;">length</span> pairs <span style="color: #339933; font-weight: bold;">&lt;=</span> <span style="color: red;">3</span> <span style="color: #339933; font-weight: bold;">=</span> fromJust <span style="color: #339933; font-weight: bold;">$</span> bfClosest pairs    
  <span style="color: #339933; font-weight: bold;">|</span> <span style="font-weight: bold;">otherwise</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">foldl</span> <span style="color: green;">&#40;</span>\closest <span style="color: green;">&#40;</span>p1:p2:<span style="color: #339933; font-weight: bold;">_</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #06c; font-weight: bold;">if</span> distance <span style="color: green;">&#40;</span>p1<span style="color: #339933; font-weight: bold;">,</span> p2<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&lt;</span> distance closest <span style="color: #06c; font-weight: bold;">then</span> <span style="color: green;">&#40;</span>p1<span style="color: #339933; font-weight: bold;">,</span> p2<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">else</span> closest<span style="color: green;">&#41;</span>
                      closestPair 
                      <span style="color: green;">&#40;</span>windowed <span style="color: red;">2</span> pairsWithinMinimumDelta<span style="color: green;">&#41;</span>
    <span style="color: #339933; font-weight: bold;">...</span></pre></div></div>

<p>We can also get rid of the if statement inside the first argument passed to &#8216;foldl&#8217; and replace it with a call to &#8216;minimumBy&#8217;:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">dcClosest <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Ord</span> a<span style="color: #339933; font-weight: bold;">,</span> <span style="color: #cccc00; font-weight: bold;">Floating</span> a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> <span style="color: green;">&#91;</span>Point a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>Point a<span style="color: #339933; font-weight: bold;">,</span> Point a<span style="color: green;">&#41;</span>
dcClosest pairs
  <span style="color: #339933; font-weight: bold;">|</span> <span style="font-weight: bold;">length</span> pairs <span style="color: #339933; font-weight: bold;">&lt;=</span> <span style="color: red;">3</span> <span style="color: #339933; font-weight: bold;">=</span> fromJust <span style="color: #339933; font-weight: bold;">$</span> bfClosest pairs    
  <span style="color: #339933; font-weight: bold;">|</span> <span style="font-weight: bold;">otherwise</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">foldl</span> <span style="color: green;">&#40;</span>\closest <span style="color: green;">&#40;</span>p1:p2:<span style="color: #339933; font-weight: bold;">_</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> minimumBy <span style="color: green;">&#40;</span><span style="font-weight: bold;">compare</span> `on` distance<span style="color: green;">&#41;</span> <span style="color: green;">&#91;</span>closest<span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#40;</span>p1<span style="color: #339933; font-weight: bold;">,</span> p2<span style="color: green;">&#41;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span>
                      closestPair 
                      <span style="color: green;">&#40;</span>windowed <span style="color: red;">2</span> pairsWithinMinimumDelta<span style="color: green;">&#41;</span>
    <span style="color: #339933; font-weight: bold;">...</span></pre></div></div>

<p>We can do the same to replace the if statement where we work out the closestPair which results in this final version of the code:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">dcClosest <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Ord</span> a<span style="color: #339933; font-weight: bold;">,</span> <span style="color: #cccc00; font-weight: bold;">Floating</span> a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> <span style="color: green;">&#91;</span>Point a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>Point a<span style="color: #339933; font-weight: bold;">,</span> Point a<span style="color: green;">&#41;</span>
dcClosest pairs
  <span style="color: #339933; font-weight: bold;">|</span> <span style="font-weight: bold;">length</span> pairs <span style="color: #339933; font-weight: bold;">&lt;=</span> <span style="color: red;">3</span> <span style="color: #339933; font-weight: bold;">=</span> fromJust <span style="color: #339933; font-weight: bold;">$</span> bfClosest pairs    
  <span style="color: #339933; font-weight: bold;">|</span> <span style="font-weight: bold;">otherwise</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">foldl</span> <span style="color: green;">&#40;</span>\closest <span style="color: green;">&#40;</span>p1:p2:<span style="color: #339933; font-weight: bold;">_</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> minimumBy <span style="color: green;">&#40;</span><span style="font-weight: bold;">compare</span> `on` distance<span style="color: green;">&#41;</span> <span style="color: green;">&#91;</span>closest<span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#40;</span>p1<span style="color: #339933; font-weight: bold;">,</span> p2<span style="color: green;">&#41;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span>
                      closestPair 
                      <span style="color: green;">&#40;</span>windowed <span style="color: red;">2</span> pairsWithinMinimumDelta<span style="color: green;">&#41;</span>
  <span style="color: #06c; font-weight: bold;">where</span> sortedByX <span style="color: #339933; font-weight: bold;">=</span> sortBy <span style="font-weight: bold;">compare</span> pairs	      
        <span style="color: green;">&#40;</span>leftByX:rightByX:<span style="color: #339933; font-weight: bold;">_</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> chunk <span style="color: green;">&#40;</span><span style="font-weight: bold;">length</span> sortedByX `<span style="font-weight: bold;">div</span>` <span style="color: red;">2</span><span style="color: green;">&#41;</span> sortedByX    
        closestPair <span style="color: #339933; font-weight: bold;">=</span> minimumBy <span style="color: green;">&#40;</span><span style="font-weight: bold;">compare</span> `on` distance<span style="color: green;">&#41;</span> <span style="color: green;">&#91;</span>closestLeftPair<span style="color: #339933; font-weight: bold;">,</span> closestRightPair<span style="color: green;">&#93;</span>
          <span style="color: #06c; font-weight: bold;">where</span> closestLeftPair <span style="color: #339933; font-weight: bold;">=</span>  dcClosest leftByX
                closestRightPair <span style="color: #339933; font-weight: bold;">=</span> dcClosest rightByX     
        pairsWithinMinimumDelta <span style="color: #339933; font-weight: bold;">=</span> sortBy <span style="color: green;">&#40;</span><span style="font-weight: bold;">compare</span> `on` <span style="font-weight: bold;">snd</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">filter</span> withinMinimumDelta sortedByX
          <span style="color: #06c; font-weight: bold;">where</span> withinMinimumDelta <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span> <span style="color: #339933; font-weight: bold;">_</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">abs</span> <span style="color: green;">&#40;</span>xMidPoint <span style="color: #339933; font-weight: bold;">-</span> x<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&lt;=</span> distance closestPair
                  <span style="color: #06c; font-weight: bold;">where</span> <span style="color: green;">&#40;</span>xMidPoint<span style="color: #339933; font-weight: bold;">,</span> <span style="color: #339933; font-weight: bold;">_</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">last</span> leftByX</pre></div></div>

<p>It takes up marginally less space and I think the change to use pattern matching on the length of &#8216;pairs&#8217; makes the biggest difference as the code is now lined up at the same level of indentation.</p>
<p>The other changes would have more of an impact if there were more than 2 things being compared &#8211; right now I think either of the versions of the code are equally readable.</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/vLlzF5HBCI4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2012/05/12/haskell-removing-if-statements/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2012/05/12/haskell-removing-if-statements/</feedburner:origLink></item>
		<item>
		<title>neo4j/Cypher: Finding the shortest path between two nodes while applying predicates</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/wTV1Fdf8Enc/</link>
		<comments>http://www.markhneedham.com/blog/2012/05/12/neo4jcypher-finding-the-shortest-path-between-two-nodes-while-applying-predicates/#comments</comments>
		<pubDate>Sat, 12 May 2012 14:55:30 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[neo4j]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=4284</guid>
		<description><![CDATA[As I mentioned in a blog post about a week ago I decided to restructure the ThoughtWorks graph I&#8217;ve modelled in neo4j so that I could explicitly model projects and clients. As a result I had to update a traversal I&#8217;d written for finding the shortest path between two people in the graph. The original [...]]]></description>
			<content:encoded><![CDATA[<p>As I <a href="http://www.markhneedham.com/blog/2012/05/05/neo4j-what-question-do-you-want-to-answer/">mentioned in a blog post about a week ago</a> I decided to restructure the ThoughtWorks graph I&#8217;ve modelled in neo4j so that I could explicitly model projects and clients.</p>
<p>As a result I had to update a traversal I&#8217;d written for finding the shortest path between two people in the graph.</p>
<p>The original traversal query I had was really simple because I had a direct connection between the people nodes:</p>
<p><img src="http://www.markhneedham.com/blog/wp-content/uploads/2012/05/initial.png"></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">neo = <span style="color:#6666ff; font-weight:bold;">Neography::Rest</span>.<span style="color:#9900CC;">new</span>
paths = neo.<span style="color:#9900CC;">get_paths</span><span style="color:#006600; font-weight:bold;">&#40;</span>start_node,
                      destination_node,
                      <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">&quot;type&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;colleagues&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>,
                      depth = <span style="color:#006666;">3</span>,
                      algorithm = <span style="color:#996600;">&quot;shortestPath&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
           .<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;nodes&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
           .<span style="color:#9900CC;">uniq</span>
paths.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>p<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>node<span style="color:#006600; font-weight:bold;">|</span> neo.<span style="color:#9900CC;">get_node_properties</span><span style="color:#006600; font-weight:bold;">&#40;</span>node, <span style="color:#996600;">&quot;name&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;name&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>I changed the way the graph was modelled so that you needed to follow a &#8216;worked_on&#8217; relationship to a project in order to go between people:</p>
<p><img src="http://www.markhneedham.com/blog/wp-content/uploads/2012/05/v2.png" alt="V2" title="v2.png" border="0" width="576" height="203" /></p>
<p>In the first version I&#8217;d written some pre processing code in Ruby to check whether or not people worked on the project at the same time before creating the relationship between the nodes.</p>
<p>It wasn&#8217;t possible to do that with the new structure since I was working out if there was a colleagues relationship dynamically.</p>
<p>I therefore added a &#8216;start_date&#8217; and &#8216;end_date&#8217; property to the &#8216;worked_on&#8217; relationship between a person node and project node so that I&#8217;d be able to take it into account when traversing the graph.]</p>
<p>I initially thought it would be possible to do this using cypher and wrote the following query:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">start_node_id = neo.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:get_id</span>, start_node<span style="color:#006600; font-weight:bold;">&#41;</span>
destination_node_id = neo.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:get_id</span>, destination_node<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
query =  <span style="color:#996600;">&quot; START a=node(#{start_node_id}), x=node(#{destination_node_id})&quot;</span> 
query <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot; MATCH p = allShortestPaths( a-[:worked_on*]-x )&quot;</span> 
query <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot; RETURN p, extract(person in nodes(p) : person.name)&quot;</span>
paths = neo.<span style="color:#9900CC;">execute_query</span><span style="color:#006600; font-weight:bold;">&#40;</span>query<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>I wasn&#8217;t sure how to do the filtering on &#8216;start_date&#8217; and &#8216;end_date&#8217; and <a href="https://groups.google.com/forum/?fromgroups#!topic/neo4j/t0UMIqqpZA4">Andres pointed out</a> that it&#8217;s not actually currently possible to take relationship properties into account when traversing a graph with <a href="https://github.com/neo4j/community/tree/master/cypher">cypher</a> so we need to do the filtering on &#8216;start_date&#8217; and &#8216;end_date&#8217; in code.</p>
<p>My first attempt to do that looked like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">paths = neo.<span style="color:#9900CC;">get_paths</span><span style="color:#006600; font-weight:bold;">&#40;</span>node1, node2, <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">&quot;type&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;worked_on&quot;</span>, <span style="color:#996600;">&quot;direction&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;all&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>, 
                      depth = <span style="color:#006666;">5</span>, algorithm = <span style="color:#996600;">&quot;shortestPath&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">uniq</span>
matching = paths.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>row<span style="color:#006600; font-weight:bold;">|</span>
  relationshipPairs = row<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;relationships&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each_slice</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_a</span>
  relationshipPairs.<span style="color:#9900CC;">all</span>? <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>pair<span style="color:#006600; font-weight:bold;">|</span>
    r1 = neo.<span style="color:#9900CC;">get_relationship</span><span style="color:#006600; font-weight:bold;">&#40;</span>pair<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;data&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    r2 = neo.<span style="color:#9900CC;">get_relationship</span><span style="color:#006600; font-weight:bold;">&#40;</span>pair<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;data&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    r1<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;start_date&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&lt;</span>= r2<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;end_date&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> r1<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;end_date&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&gt;</span>= r2<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;start_date&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The problem with this approach is that it&#8217;s really slow due to the fact that I&#8217;m pulling back every relationship back in order to check the start and end dates. </p>
<p>Michael Hunger <a href="https://groups.google.com/forum/?fromgroups#!topic/neo4j/H1HRTVZp1ls">suggested an alternative approach</a> where I still used a cypher query but returned the relationships instead of just the nodes:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">start_node_id = neo.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:get_id</span>, start_node<span style="color:#006600; font-weight:bold;">&#41;</span>
destination_node_id = neo.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:get_id</span>, destination_node<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
query =  <span style="color:#996600;">&quot; START a=node(#{start_node_id}), x=node(#{destination_node_id})&quot;</span> 
query <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot; MATCH p = allShortestPaths( a-[:worked_on*]-x )&quot;</span> 
query <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot; RETURN p, rels(p), extract(person in nodes(p) : person.name)&quot;</span>
paths = neo.<span style="color:#9900CC;">execute_query</span><span style="color:#006600; font-weight:bold;">&#40;</span>query<span style="color:#006600; font-weight:bold;">&#41;</span>
matching = paths<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;data&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>row<span style="color:#006600; font-weight:bold;">|</span>
  relationship_pairs = row<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each_slice</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_a</span>
  relationship_pairs.<span style="color:#9900CC;">all</span>? <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>pair<span style="color:#006600; font-weight:bold;">|</span>
    r1 = pair<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;data&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    r2 = pair<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;data&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    r1<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;start_date&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&lt;</span>= r2<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;end_date&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> r1<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;end_date&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&gt;</span>= r2<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;start_date&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
matching.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each_with_index</span>.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>x,idx<span style="color:#006600; font-weight:bold;">|</span> idx.<span style="color:#9900CC;">even</span>? <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:first<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">uniq</span></pre></div></div>

<p>This approach mostly works although it runs into problems in the scenario where two people have worked on the same project but not at the same time.</p>
<p>In that scenario the above code will return the relationship between them but it will then be filtered out by the start date/end date logic which means we won&#8217;t see a shortest path between those nodes.</p>
<p>I&#8217;m not sure how to solve that problem so for the moment I&#8217;m going to take the <a href="http://www.markhneedham.com/blog/2012/05/05/neo4j-what-question-do-you-want-to-answer/#comment-520250796">Josh Adell&#8217;s suggestion to keep the &#8216;colleagues&#8217; relationship between nodes</a> and use that relationship for the shortest path traversal.</p>
<p>The &#8216;worked_on&#8217; relationship is still useful for other things that I want to do but not this particular one.</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/wTV1Fdf8Enc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2012/05/12/neo4jcypher-finding-the-shortest-path-between-two-nodes-while-applying-predicates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2012/05/12/neo4jcypher-finding-the-shortest-path-between-two-nodes-while-applying-predicates/</feedburner:origLink></item>
		<item>
		<title>Haskell: Explicit type declarations in GHCI</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/z2vG-GYcXSM/</link>
		<comments>http://www.markhneedham.com/blog/2012/05/10/haskell-explicit-type-declarations-in-ghci/#comments</comments>
		<pubDate>Thu, 10 May 2012 07:11:17 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=4279</guid>
		<description><![CDATA[On a few occasions I&#8217;ve wanted to be able to explicitly define the type of something when trying things out in the Haskell REPL (GHCI) but I didn&#8217;t actually realise this was possible until a couple of days ago. For example say we want to use the read function to parse an input string into [...]]]></description>
			<content:encoded><![CDATA[<p>On a few occasions I&#8217;ve wanted to be able to explicitly define the type of something when trying things out in the Haskell REPL (GHCI) but I didn&#8217;t actually realise this was possible until a couple of days ago.</p>
<p>For example say we want to use the <cite><a href="http://zvon.org/other/haskell/Outputprelude/read_f.html">read</a></cite> function to parse an input string into an integer.</p>
<p>We could do this:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #339933; font-weight: bold;">&gt;</span> <span style="font-weight: bold;">read</span> <span style="background-color: #3cb371;">&quot;1&quot;</span> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span>
<span style="color: red;">1</span></pre></div></div>

<p>But if we just evaluate the function alone and try and assign the result without casting to a type we get an exception:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: #06c; font-weight: bold;">let</span> x  <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">read</span> <span style="background-color: #3cb371;">&quot;1&quot;</span>
&nbsp;
<span style="color: #339933; font-weight: bold;">&lt;</span>interactive<span style="color: #339933; font-weight: bold;">&gt;</span>:<span style="color: red;">1</span>:<span style="color: red;">10</span>:
    Ambiguous <span style="color: #06c; font-weight: bold;">type</span> variable `a0' <span style="color: #06c; font-weight: bold;">in</span> the constraint:
      <span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Read</span> a0<span style="color: green;">&#41;</span> arising from a use <span style="color: #06c; font-weight: bold;">of</span> `<span style="font-weight: bold;">read</span>'
    Probable fix: add a <span style="color: #06c; font-weight: bold;">type</span> signature that fixes these <span style="color: #06c; font-weight: bold;">type</span> variable<span style="color: green;">&#40;</span>s<span style="color: green;">&#41;</span>
    In the expression: <span style="font-weight: bold;">read</span> <span style="background-color: #3cb371;">&quot;1&quot;</span>
    In an equation for `x': x <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">read</span> <span style="background-color: #3cb371;">&quot;1&quot;</span></pre></div></div>

<p>sepp2k shows <a href="http://stackoverflow.com/questions/3093133/how-to-provide-explicit-type-declarations-for-functions-when-using-ghci">how we can provide a type declaration in GHCI in his Stack Over Flow answer</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: #06c; font-weight: bold;">let</span> x<span style="color: #339933; font-weight: bold;">::</span><span style="color: #cccc00; font-weight: bold;">Int</span>; x <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">read</span> <span style="background-color: #3cb371;">&quot;1&quot;</span>
<span style="color: #339933; font-weight: bold;">&gt;</span> x
<span style="color: red;">1</span></pre></div></div>

<p>We can also use it when creating a list of integers to ensure they are of type &#8216;Int&#8217; rather than &#8216;Integer&#8217; for example:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: #06c; font-weight: bold;">let</span> y <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</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>
<span style="color: #339933; font-weight: bold;">&gt;</span> :t y
y <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Integer</span><span style="color: green;">&#93;</span>
&nbsp;
<span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: #06c; font-weight: bold;">let</span> y<span style="color: #339933; font-weight: bold;">::</span><span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#93;</span>; y <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</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>
<span style="color: #339933; font-weight: bold;">&gt;</span> :t y
y <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#93;</span></pre></div></div>

<p>It&#8217;s a pretty simple thing but I didn&#8217;t know it was even possible!</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/z2vG-GYcXSM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2012/05/10/haskell-explicit-type-declarations-in-ghci/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2012/05/10/haskell-explicit-type-declarations-in-ghci/</feedburner:origLink></item>
		<item>
		<title>Haskell: Closest Pairs Algorithm</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/XXl-9Wt1fRg/</link>
		<comments>http://www.markhneedham.com/blog/2012/05/09/haskell-closest-pairs-algorithm/#comments</comments>
		<pubDate>Wed, 09 May 2012 00:05:56 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=4267</guid>
		<description><![CDATA[As I mentioned in a post a couple of days ago I&#8217;ve been writing the closest pairs algorithm in Haskell and while the brute force version works for small numbers of pairs it starts to fall apart as the number of pairs increases: time ./closest_pairs 100 bf ./closest_pairs 100 bf 0.01s user 0.00s system 87% [...]]]></description>
			<content:encoded><![CDATA[<p>As I mentioned in <a href="http://www.markhneedham.com/blog/2012/05/07/haskell-maximum-int-value/">a post a couple of days ago</a> I&#8217;ve been writing the closest pairs algorithm in Haskell and while the brute force version works for small numbers of pairs it starts to fall apart as the number of pairs increases:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">time ./closest_pairs 100 bf 
./closest_pairs 100 bf  0.01s user 0.00s system 87% cpu 0.016 total
&nbsp;
time ./closest_pairs 1000 bf
./closest_pairs 1000 bf  3.59s user 0.01s system 99% cpu 3.597 total
&nbsp;
time ./closest_pairs 5000 bf
./closest_pairs 5000  554.09s user 0.36s system 99% cpu 9:14.46 total</pre></div></div>

<p>Luckily there&#8217;s a divide and conquer algorithm we can use which brings down the running time from O(n<sup>2</sup) to O(n log(n)) which is significantly quicker as n increases in size.</p>
<p>That <a href="http://en.wikipedia.org/wiki/Closest_pair_of_points_problem">algorithm is defined like so</a>:</p>
<blockquote>
<ol>
<li>Sort points along the x-coordinate</li>
<li>Split the set of points into two equal-sized subsets by a vertical line x = x<sub>mid</sub></li>
<li>Solve the problem recursively in the left and right subsets. This will give the left-side and right-side minimal distances d<sub>Lmin</sub>  and d<sub>Rmin</sub>  respectively.</li>
<li>Find the minimal distance d<sub>LRmin</sub>  among the pair of points in which one point lies on the left of the dividing vertical and the second point lies to the right (a split pair).</li>
<li>The final answer is the minimum among d<sub>Lmin</sub>, d<sub>Rmin</sub>, and d<sub>LRmin</sub>.</li>
</ol>
</blockquote>
<p>By step 4 in which we&#8217;ll look for a closest pair where the x and y values are in opposite subsets  we don&#8217;t need to consider the whole list of points again since we already know that the closest pair of points is no further apart than <cite>dist</cite> = min(d<sub>Lmin</sub>, d<sub>Rmin</sub>). </p>
<p>We can therefore filter out a lot of the values by only keeping values which are within <cite>dist</cite> of the middle x value. </p>
<p>If a point is further away than that then we already know that the distance between it and any point on the other side will be greater than <cite>dist</cite> so there&#8217;s no point in considering it.</p>
<p>I used the <a href="http://rosettacode.org/wiki/Closest-pair_problem#C.23">C# version from Rosetta Code</a> as a template and this is what I ended up with:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">import</span> Data<span style="color: #339933; font-weight: bold;">.</span><span style="color: #cccc00; font-weight: bold;">Maybe</span>
<span style="color: #06c; font-weight: bold;">import</span> Data<span style="color: #339933; font-weight: bold;">.</span>List
<span style="color: #06c; font-weight: bold;">import</span> Data<span style="color: #339933; font-weight: bold;">.</span>List<span style="color: #339933; font-weight: bold;">.</span>Split
<span style="color: #06c; font-weight: bold;">import</span> Data<span style="color: #339933; font-weight: bold;">.</span>Function
&nbsp;
dcClosest <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Ord</span> a<span style="color: #339933; font-weight: bold;">,</span> <span style="color: #cccc00; font-weight: bold;">Floating</span> a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> <span style="color: green;">&#91;</span>Point a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>Point a<span style="color: #339933; font-weight: bold;">,</span> Point a<span style="color: green;">&#41;</span>
dcClosest pairs
  <span style="color: #06c; font-weight: bold;">if</span> <span style="font-weight: bold;">length</span> pairs <span style="color: #339933; font-weight: bold;">&lt;=</span> <span style="color: red;">3</span> <span style="color: #06c; font-weight: bold;">then</span> <span style="color: #339933; font-weight: bold;">=</span> fromJust <span style="color: #339933; font-weight: bold;">$</span> bfClosest pairs    
  <span style="color: #06c; font-weight: bold;">else</span> 
    <span style="font-weight: bold;">foldl</span> <span style="color: green;">&#40;</span>\closest <span style="color: green;">&#40;</span>p1:p2:<span style="color: #339933; font-weight: bold;">_</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #06c; font-weight: bold;">if</span> distance <span style="color: green;">&#40;</span>p1<span style="color: #339933; font-weight: bold;">,</span> p2<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&lt;</span> distance closest <span style="color: #06c; font-weight: bold;">then</span> <span style="color: green;">&#40;</span>p1<span style="color: #339933; font-weight: bold;">,</span> p2<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">else</span> closest<span style="color: green;">&#41;</span> 
          closestPair 
          <span style="color: green;">&#40;</span>windowed <span style="color: red;">2</span> pairsWithinMinimumDelta<span style="color: green;">&#41;</span>
  <span style="color: #06c; font-weight: bold;">where</span> sortedByX <span style="color: #339933; font-weight: bold;">=</span> sortBy <span style="font-weight: bold;">compare</span> pairs	      
        <span style="color: green;">&#40;</span>leftByX:rightByX:<span style="color: #339933; font-weight: bold;">_</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> chunk <span style="color: green;">&#40;</span><span style="font-weight: bold;">length</span> sortedByX `<span style="font-weight: bold;">div</span>` <span style="color: red;">2</span><span style="color: green;">&#41;</span> sortedByX
        closestPair <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">if</span> distance closestLeftPair <span style="color: #339933; font-weight: bold;">&lt;</span> distance closestRightPair <span style="color: #06c; font-weight: bold;">then</span> closestLeftPair <span style="color: #06c; font-weight: bold;">else</span> closestRightPair  
          <span style="color: #06c; font-weight: bold;">where</span> closestLeftPair <span style="color: #339933; font-weight: bold;">=</span>  dcClosest leftByX
                closestRightPair <span style="color: #339933; font-weight: bold;">=</span> dcClosest rightByX
        pairsWithinMinimumDelta <span style="color: #339933; font-weight: bold;">=</span> sortBy <span style="color: green;">&#40;</span><span style="font-weight: bold;">compare</span> `on` <span style="font-weight: bold;">snd</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">filter</span> withinMinimumDelta sortedByX
          <span style="color: #06c; font-weight: bold;">where</span> withinMinimumDelta <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span> <span style="color: #339933; font-weight: bold;">_</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">abs</span> <span style="color: green;">&#40;</span>xMidPoint <span style="color: #339933; font-weight: bold;">-</span> x<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&lt;=</span> distance closestPair   
                  <span style="color: #06c; font-weight: bold;">where</span> <span style="color: green;">&#40;</span>xMidPoint<span style="color: #339933; font-weight: bold;">,</span> <span style="color: #339933; font-weight: bold;">_</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">last</span> leftByX</pre></div></div>

<p>If the number of pairs is 3 or less then we can just use the brute force algorithm since the whole splitting the list in half thing doesn&#8217;t work so well with a list of less than 4 items.</p>
<p>The main function is a fold which goes over all the pairs of points where we&#8217;ve determined that there might be a closest pair with one point on the left hand side of our &#8216;sorted by x list&#8217; and the other on the right hand side. </p>
<p>We use the &#8216;<a href="http://www.markhneedham.com/blog/2012/02/28/haskell-creating-a-sliding-window-over-a-collection/">windowed</a>&#8216; function to pair up the points which in this case does something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #339933; font-weight: bold;">&gt;</span> windowed <span style="color: red;">2</span> <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">0</span><span style="color: green;">&#41;</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><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><span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">2</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#40;</span><span style="color: red;">1.1</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">1.2</span><span style="color: green;">&#41;</span><span style="color: green;">&#93;</span>
<span style="color: green;">&#91;</span><span style="color: green;">&#91;</span><span style="color: green;">&#40;</span><span style="color: red;">0.0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">0.0</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span><span style="color: red;">1.0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">1.0</span><span style="color: green;">&#41;</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;">&#40;</span><span style="color: red;">1.0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">1.0</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span><span style="color: red;">2.0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">2.0</span><span style="color: green;">&#41;</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;">&#40;</span><span style="color: red;">2.0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">2.0</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span><span style="color: red;">1.1</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">1.2</span><span style="color: green;">&#41;</span><span style="color: green;">&#93;</span><span style="color: green;">&#93;</span></pre></div></div>

<p>We pass along the closest pair that we&#8217;ve found from pairs of points on the left and right hand sides of the list as our seed value and check whether any of the split pairs are closer together than that one. By the time the fold is finished we will have the closest pair in the list.</p>
<p>The code in the where section is used to split the list into two halves, sort it by x and then work out which side of the list has the closest pair. That&#8217;s all done recursively and then the last bit of code works out which values we need to consider for the split pairs part of the algorithm.</p>
<p>I learnt about the &#8216;<a href="http://stackoverflow.com/questions/2788195/haskell-sorting">on</a>&#8216; function while writing this algorithm which makes it really easy to pick a function to sort a collection with e.g. &#8216;compare `on` snd&#8217; lets us sort a list in ascending order based on the second value in a tuple.</p>
<p>One problem with the way I&#8217;ve written this algorithm is that it places a lot of focus on the split pair bit of the code when actually a big part of why it works is that we&#8217;re sorting it into an order that makes it easier to work with and then dividing the problem by 2 each time.</p>
<p>My current thinking is that perhaps I should have had that code in the main body of the function rather than hiding it away in the where section.</p>
<p>It isn&#8217;t actually necessary to execute the foldl all the way through since we&#8217;ll often know there&#8217;s not going to be a split pair before we go through all the pairs. I thought it reads pretty nicely as it is thought and it runs pretty quickly as it is anyway!</p>
<p>Using the same data set as before (and getting the same answers!):</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; time ./closest_pairs 1000 dc
./closest_pairs 1000 dc  0.02s user 0.00s system 91% cpu 0.024 total
&nbsp;
&gt; time ./closest_pairs 5000 dc
./closest_pairs 5000 dc  0.11s user 0.01s system 97% cpu 0.118 total</pre></div></div>

<p>I described the code for <a href="http://www.markhneedham.com/blog/2012/05/08/haskell-generating-random-numbers/">generating random numbers</a> in an earlier blog post but I&#8217;ll include it again to show how the algorithm is wired up:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">import</span> Control<span style="color: #339933; font-weight: bold;">.</span><span style="color: #cccc00; font-weight: bold;">Monad</span><span style="color: #339933; font-weight: bold;">.</span>State <span style="color: green;">&#40;</span>State<span style="color: #339933; font-weight: bold;">,</span> evalState<span style="color: #339933; font-weight: bold;">,</span> get<span style="color: #339933; font-weight: bold;">,</span> put<span style="color: green;">&#41;</span>
<span style="color: #06c; font-weight: bold;">import</span> System<span style="color: #339933; font-weight: bold;">.</span>Random <span style="color: green;">&#40;</span>StdGen<span style="color: #339933; font-weight: bold;">,</span> mkStdGen<span style="color: #339933; font-weight: bold;">,</span> random<span style="color: green;">&#41;</span>
<span style="color: #06c; font-weight: bold;">import</span> System
&nbsp;
<span style="color: #06c; font-weight: bold;">type</span> R a <span style="color: #339933; font-weight: bold;">=</span> State StdGen a
rand <span style="color: #339933; font-weight: bold;">::</span> R <span style="color: #cccc00; font-weight: bold;">Double</span>
rand <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
  gen <span style="color: #339933; font-weight: bold;">&lt;-</span> get
  <span style="color: #06c; font-weight: bold;">let</span> <span style="color: green;">&#40;</span>r<span style="color: #339933; font-weight: bold;">,</span> gen'<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> random gen
  put gen'
  <span style="font-weight: bold;">return</span> r
&nbsp;
randPair <span style="color: #339933; font-weight: bold;">::</span> R <span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Double</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: #cccc00; font-weight: bold;">Double</span><span style="color: green;">&#41;</span>
randPair <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
  x <span style="color: #339933; font-weight: bold;">&lt;-</span> rand
  y <span style="color: #339933; font-weight: bold;">&lt;-</span> rand
  <span style="font-weight: bold;">return</span> <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span>y<span style="color: green;">&#41;</span>
&nbsp;
runRandom <span style="color: #339933; font-weight: bold;">::</span> R a <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Int</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> a
runRandom action seed <span style="color: #339933; font-weight: bold;">=</span> evalState action <span style="color: #339933; font-weight: bold;">$</span> mkStdGen seed
&nbsp;
normals <span style="color: #339933; font-weight: bold;">::</span> R <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Double</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: #cccc00; font-weight: bold;">Double</span><span style="color: green;">&#41;</span><span style="color: green;">&#93;</span>
normals <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">mapM</span> <span style="color: green;">&#40;</span>\<span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> randPair<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">repeat</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
&nbsp;
main <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span> 
	args <span style="color: #339933; font-weight: bold;">&lt;-</span> getArgs
	<span style="color: #06c; font-weight: bold;">let</span> numberOfPairs <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">read</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">head</span> args<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span>
	<span style="color: #06c; font-weight: bold;">if</span> <span style="font-weight: bold;">length</span> args <span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: red;">1</span> <span style="color: #339933; font-weight: bold;">&amp;&amp;</span> args <span style="color: #339933; font-weight: bold;">!!</span> <span style="color: red;">1</span> <span style="color: #339933; font-weight: bold;">==</span> <span style="background-color: #3cb371;">&quot;bf&quot;</span> <span style="color: #06c; font-weight: bold;">then</span> 
		<span style="font-weight: bold;">putStrLn</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">show</span> <span style="color: green;">&#40;</span> <span style="color: green;">&#40;</span>bfClosest <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">take</span> numberOfPairs <span style="color: #339933; font-weight: bold;">$</span> runRandom normals <span style="color: red;">42</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
	<span style="color: #06c; font-weight: bold;">else</span> 
		<span style="font-weight: bold;">putStrLn</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">show</span> <span style="color: green;">&#40;</span> <span style="color: green;">&#40;</span>dcClosest <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">take</span> numberOfPairs <span style="color: #339933; font-weight: bold;">$</span> runRandom normals <span style="color: red;">42</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre></div></div>

<p>The <a href="http://hpaste.org/68299">full code is on hpaste</a> if anyone has any suggestions for how to improve it.</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/XXl-9Wt1fRg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2012/05/09/haskell-closest-pairs-algorithm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2012/05/09/haskell-closest-pairs-algorithm/</feedburner:origLink></item>
		<item>
		<title>Haskell: Generating random numbers</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/btm4TpYfOR8/</link>
		<comments>http://www.markhneedham.com/blog/2012/05/08/haskell-generating-random-numbers/#comments</comments>
		<pubDate>Tue, 08 May 2012 22:09:17 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=4257</guid>
		<description><![CDATA[As I mentioned in my last post I&#8217;ve been coding the closest pairs algorithm in Haskell and needed to create some pairs of coordinates to test it against. I&#8217;ve tried to work out how to create lists of random numbers in Haskell before and always ended up giving up because it seemed way more difficult [...]]]></description>
			<content:encoded><![CDATA[<p>As I <a href="http://www.markhneedham.com/blog/2012/05/07/haskell-maximum-int-value/">mentioned in my last post</a> I&#8217;ve been coding the closest pairs algorithm in Haskell and needed to create some pairs of coordinates to test it against.</p>
<p>I&#8217;ve tried to work out how to create lists of random numbers in Haskell before and always ended up giving up because it seemed way more difficult than it should be but this time I came across a <a href="http://stackoverflow.com/questions/2110535/sampling-sequences-of-random-numbers-in-haskell">really good explanation of how to do it by jrockway on Stack Overflow</a>.</p>
<p>I thought it&#8217;d be interesting to work up to his answer by first looking at the functions available to help us generate random numbers.</p>
<p><a href="http://hackage.haskell.org/packages/archive/random/latest/doc/html/System-Random.html#v:mkStdGen">mkStdGen</a> is a function which creates a random number generator for a given seed value and seems like a good place to start.</p>
<blockquote><p>
mkStdGen :: Int -> StdGen</p>
<p>The function mkStdGen provides an alternative way of producing an initial generator, by mapping an Int into a generator. Again, distinct arguments should be likely to produce distinct generators.
</p></blockquote>
<p>We can combine that with &#8216;randomR&#8217; to give us a random value in a range that we choose or with &#8216;random&#8217; to get a random value in a default range.</p>
<blockquote><p>
randomR :: RandomGen g => (a, a) -> g -> (a, g)</p>
<p>Takes a range (lo,hi) and a random number generator g, and returns a random value uniformly distributed in the closed interval [lo,hi], together with a new generator. It is unspecified what happens if lo>hi. For continuous types there is no requirement that the values lo and hi are ever produced, but they may be, depending on the implementation and the interval.
</p></blockquote>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #339933; font-weight: bold;">&gt;</span> <span style="font-weight: bold;">fst</span> <span style="color: #339933; font-weight: bold;">$</span> randomR <span style="color: green;">&#40;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">10</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>mkStdGen <span style="color: red;">66</span><span style="color: green;">&#41;</span>
<span style="color: red;">8</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: #06c; font-weight: bold;">let</span> randomNumber <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span>; x <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">fst</span> <span style="color: #339933; font-weight: bold;">$</span> random <span style="color: green;">&#40;</span>mkStdGen <span style="color: red;">66</span><span style="color: green;">&#41;</span>
<span style="color: #339933; font-weight: bold;">&gt;</span> randomNumber
<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">4755267682593970340</span></pre></div></div>

<p>For now I&#8217;m ignoring the new random generator that is provided as the second part of the tuple that these two functions return.</p>
<p>If we want to create an infinite list of random numbers then something like this will do the trick:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">let</span> values <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#93;</span>; values <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">map</span> <span style="font-weight: bold;">fst</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">scanl</span> <span style="color: green;">&#40;</span>\<span style="color: green;">&#40;</span>r<span style="color: #339933; font-weight: bold;">,</span> gen<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> random gen<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>random <span style="color: green;">&#40;</span>mkStdGen <span style="color: red;">1</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">repeat</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span></pre></div></div>

<p>We use the &#8216;repeat&#8217; function to create an infinite sequence of nothing then run a scanl across that infinite sequence, called &#8216;random&#8217; with a new instance of the random number generator each time:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #339933; font-weight: bold;">&gt;</span> <span style="font-weight: bold;">take</span> <span style="color: red;">10</span> values
<span style="color: green;">&#91;</span><span style="color: red;">7917908265643496962</span><span style="color: #339933; font-weight: bold;">,-</span><span style="color: red;">1017158127812413512</span><span style="color: #339933; font-weight: bold;">,-</span><span style="color: red;">1196564839808993555</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">128524678767915218</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">3573078269730797745</span><span style="color: #339933; font-weight: bold;">,-</span><span style="color: red;">2026762924287790163</span><span style="color: #339933; font-weight: bold;">,-</span><span style="color: red;">5402471397730582264</span><span style="color: #339933; font-weight: bold;">,-</span><span style="color: red;">8620480566299071809</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">5987841344909700899</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">1198540087579679591</span><span style="color: green;">&#93;</span>
it <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#93;</span></pre></div></div>

<p>In this version we&#8217;re passing the next generator along as part of the accumulator of the &#8216;scanl&#8217; function but another way to do that would be to encapsulate it inside a State monad:</p>
<p>Using the State monad our random number function would read like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="haskell" style="font-family:monospace;">myRand <span style="color: #339933; font-weight: bold;">::</span> State StdGen <span style="color: #cccc00; font-weight: bold;">Int</span>
myRand <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
	gen <span style="color: #339933; font-weight: bold;">&lt;-</span> get
	<span style="color: #06c; font-weight: bold;">let</span> <span style="color: green;">&#40;</span>r<span style="color: #339933; font-weight: bold;">,</span> nextGen<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> random gen
	put nextGen
	<span style="font-weight: bold;">return</span> r</pre></td></tr></table></div>

<p>The state monad contains some state (the random number generator) and creates values (the random numbers). </p>
<p>Line 3 gets the current generator.<br />
Line 4 creates a new number and generator which it assigns to &#8216;r&#8217; and &#8216;nextGen&#8217;<br />
Line 5 updates the State monad with the new generator<br />
Line 6 returns the value that was created.</p>
<p>We change &#8216;values&#8217; to read like this:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">let</span> values <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">mapM</span> <span style="color: green;">&#40;</span>\<span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> myRand<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">repeat</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span></pre></div></div>

<p>&#8216;mapM (\_ -> myRand)&#8217; is <a href="http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/Control-Monad.html#v%3Asequence">the same as</a> doing &#8216;sequence $ map (\_ -> myRand)&#8217; and we need to do that because we want to return a State Monad with an array of Ints rather than an array of State Monads each with an Int.</p>
<p>In order to evaluate those values we call &#8216;evalState&#8217; which returns the final result from the State monad i.e. the random value created:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #339933; font-weight: bold;">&gt;</span> evalState values <span style="color: #339933; font-weight: bold;">$</span> mkStdGen <span style="color: red;">1</span>
<span style="color: green;">&#91;</span><span style="color: red;">7917908265643496962</span><span style="color: #339933; font-weight: bold;">,-</span><span style="color: red;">1017158127812413512</span><span style="color: #339933; font-weight: bold;">,-</span><span style="color: red;">1196564839808993555</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">128524678767915218</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">3573078269730797745</span><span style="color: #339933; font-weight: bold;">,-</span><span style="color: red;">2026762924287790163</span><span style="color: #339933; font-weight: bold;">,-</span><span style="color: red;">5402471397730582264</span><span style="color: #339933; font-weight: bold;">,-</span><span style="color: red;">8620480566299071809</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">5987841344909700899</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">1198540087579679591</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">5081506216124746781</span><span style="color: #339933; font-weight: bold;">,-</span><span style="color: red;">2413363174299075397</span><span style="color: #339933; font-weight: bold;">,-</span><span style="color: red;">6804412472718217891</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">4559850124463334118</span><span style="color: #339933; font-weight: bold;">,-</span><span style="color: red;">362777938400029309</span><span style="color: #339933; font-weight: bold;">,-</span><span style="color: red;">23100237439495333</span><span style="color: #339933; font-weight: bold;">,-</span><span style="color: red;">2426472460098089322</span><span style="color: #339933; font-weight: bold;">,...</span><span style="color: green;">&#93;</span></pre></div></div>

<p>In our case we want to get an infinite sequence of pairs to represent the coordinates on a plane so we need to have a random function that creates those:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">myRandPair <span style="color: #339933; font-weight: bold;">::</span> State StdGen <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>
myRandPair <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
	x <span style="color: #339933; font-weight: bold;">&lt;-</span> myRand
	y <span style="color: #339933; font-weight: bold;">&lt;-</span> myRand
	<span style="font-weight: bold;">return</span> <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span> y<span style="color: green;">&#41;</span></pre></div></div>

<p>We then change &#8216;values&#8217; accordingly:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: #06c; font-weight: bold;">let</span> values <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">mapM</span> <span style="color: green;">&#40;</span>\<span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> myRandPair<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">repeat</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
<span style="color: #339933; font-weight: bold;">&gt;</span> evalState values <span style="color: #339933; font-weight: bold;">$</span> mkStdGen <span style="color: red;">1</span>
<span style="color: green;">&#91;</span><span style="color: green;">&#40;</span><span style="color: red;">7917908265643496962</span><span style="color: #339933; font-weight: bold;">,-</span><span style="color: red;">1017158127812413512</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1196564839808993555</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">128524678767915218</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span><span style="color: red;">3573078269730797745</span><span style="color: #339933; font-weight: bold;">,-</span><span style="color: red;">2026762924287790163</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">5402471397730582264</span><span style="color: #339933; font-weight: bold;">,-</span><span style="color: red;">8620480566299071809</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span><span style="color: red;">5987841344909700899</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">1198540087579679591</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span><span style="color: red;">5081506216124746781</span><span style="color: #339933; font-weight: bold;">,-</span><span style="color: red;">2413363174299075397</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">6804412472718217891</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">4559850124463334118</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">362777938400029309</span><span style="color: #339933; font-weight: bold;">,-</span><span style="color: red;">23100237439495333</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">...</span><span style="color: green;">&#93;</span></pre></div></div>

<p>If we want to get back some different pairs then we just need to change the initial seed value that we pass to &#8216;mkStdGen&#8217;.</p>
<p><a href="http://stackoverflow.com/questions/2110535/sampling-sequences-of-random-numbers-in-haskell">jrockway explains this in more detail in his post</a>  in which he also wraps the State monad inside an alias to make the code easier to understand.</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/btm4TpYfOR8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2012/05/08/haskell-generating-random-numbers/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2012/05/08/haskell-generating-random-numbers/</feedburner:origLink></item>
		<item>
		<title>Haskell: Maximum Int value</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/f0Wba3GawYk/</link>
		<comments>http://www.markhneedham.com/blog/2012/05/07/haskell-maximum-int-value/#comments</comments>
		<pubDate>Mon, 07 May 2012 09:18:02 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=4253</guid>
		<description><![CDATA[One of the algorithms covered in Algo Class was the closest pairs algorithm &#8211; an algorithm used to determine which pair of points on a plane are closest to each other based on their Euclidean distance. My real interest lies in writing the divide and conquer version of the algorithm but I started with the [...]]]></description>
			<content:encoded><![CDATA[<p>One of the algorithms covered in <a href="https://www.coursera.org/course/algo">Algo Class</a> was the <a href="http://en.wikipedia.org/wiki/Closest_pair_of_points_problem">closest pairs algorithm</a> &#8211; an algorithm used to determine which pair of points on a plane are closest to each other based on their <a href="http://en.wikipedia.org/wiki/Euclidean_distance">Euclidean distance</a>.</p>
<p>My real interest lies in writing the divide and conquer version of the algorithm but I started with the brute force version so that I&#8217;d be able to compare my answers.</p>
<p>This is the algorithm:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">minDist = infinity
for each p in P:
  for each q in P:
    if p ≠ q and dist(p, q) &lt; minDist:
      minDist = dist(p, q)
      closestPair = (p, q)
return closestPair</pre></div></div>

<p>&#8216;infinity&#8217; in this case could be the maximum value that an Int could hold which on a 64 bit architecture would be 2<sup>63</sup> so I hardcoded that into my implementation:<br />
o</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">bfClosest <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Ord</span> a<span style="color: #339933; font-weight: bold;">,</span> <span style="color: #cccc00; font-weight: bold;">Floating</span> a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span>a<span style="color: #339933; font-weight: bold;">,</span> a<span style="color: green;">&#41;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Maybe</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>a<span style="color: #339933; font-weight: bold;">,</span> a<span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#40;</span>a<span style="color: #339933; font-weight: bold;">,</span> a<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
bfClosest pairs <span style="color: #339933; font-weight: bold;">=</span> 
  <span style="font-weight: bold;">snd</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">foldl</span> <span style="color: green;">&#40;</span>\ acc<span style="color: #339933; font-weight: bold;">@</span><span style="color: green;">&#40;</span><span style="font-weight: bold;">min</span><span style="color: #339933; font-weight: bold;">,</span> soFar<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>p1<span style="color: #339933; font-weight: bold;">,</span> p2<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> 
                <span style="color: #06c; font-weight: bold;">if</span> distance p1 p2 <span style="color: #339933; font-weight: bold;">&lt;</span> <span style="font-weight: bold;">min</span> <span style="color: #06c; font-weight: bold;">then</span> <span style="color: green;">&#40;</span>distance p1 p2<span style="color: #339933; font-weight: bold;">,</span> Just<span style="color: green;">&#40;</span>p1<span style="color: #339933; font-weight: bold;">,</span> p2<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">else</span> acc<span style="color: green;">&#41;</span> 
              <span style="color: green;">&#40;</span><span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">^</span><span style="color: red;">63</span><span style="color: #339933; font-weight: bold;">,</span> Nothing<span style="color: green;">&#41;</span> 
              <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span>pairs <span style="color: #339933; font-weight: bold;">!!</span> i<span style="color: #339933; font-weight: bold;">,</span> pairs <span style="color: #339933; font-weight: bold;">!!</span> j<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">|</span> i <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="color: green;">&#91;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">..</span><span style="font-weight: bold;">length</span> pairs <span style="color: #339933; font-weight: bold;">-</span> <span style="color: red;">1</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> j <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="color: green;">&#91;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">..</span><span style="font-weight: bold;">length</span> pairs<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span> <span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> i <span style="color: #339933; font-weight: bold;">/=</span> j<span style="color: green;">&#93;</span>
  <span style="color: #06c; font-weight: bold;">where</span> distance <span style="color: green;">&#40;</span>x1<span style="color: #339933; font-weight: bold;">,</span> y1<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>x2<span style="color: #339933; font-weight: bold;">,</span> y2<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span>  <span style="font-weight: bold;">sqrt</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>x1 <span style="color: #339933; font-weight: bold;">-</span> x2<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">^</span> <span style="color: red;">2</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">+</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>y1 <span style="color: #339933; font-weight: bold;">-</span> y2<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">^</span> <span style="color: red;">2</span><span style="color: green;">&#41;</span></pre></div></div>

<p>We&#8217;re comparing each point with all the others in the list by folding over a collection of all the combinations and then passing the pair with the smallest distance between points as part of our accumulator.</p>
<p>More by chance than anything else I was reading <a href="http://learnyouahaskell.com/types-and-typeclasses">the Learn You a Haskell chapter on types and type classes</a> and came across the <cite><a href="http://zvon.org/other/haskell/Outputprelude/maxBound_f.html">maxBound</a></cite> function which does exactly what I want:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: red;">2</span> <span style="color: #339933; font-weight: bold;">^</span> <span style="color: red;">63</span>
<span style="color: red;">9223372036854775808</span>
&nbsp;
<span style="color: #339933; font-weight: bold;">&gt;</span> <span style="font-weight: bold;">maxBound</span> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span>
<span style="color: red;">9223372036854775807</span></pre></div></div>

<p>We can&#8217;t plug that straight into the function as is because the fold inside &#8216;bfClosest&#8217; expects a float and had been automatically coercing 2<sup>63</sup> into the appropriate type.</p>
<p>We therefore use &#8216;fromIntegral&#8217; to help us out:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">bfClosest <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Ord</span> a<span style="color: #339933; font-weight: bold;">,</span> <span style="color: #cccc00; font-weight: bold;">Floating</span> a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span>a<span style="color: #339933; font-weight: bold;">,</span> a<span style="color: green;">&#41;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Maybe</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>a<span style="color: #339933; font-weight: bold;">,</span> a<span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#40;</span>a<span style="color: #339933; font-weight: bold;">,</span> a<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
bfClosest pairs <span style="color: #339933; font-weight: bold;">=</span> 
  <span style="font-weight: bold;">snd</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">foldl</span> <span style="color: green;">&#40;</span>\ acc<span style="color: #339933; font-weight: bold;">@</span><span style="color: green;">&#40;</span><span style="font-weight: bold;">min</span><span style="color: #339933; font-weight: bold;">,</span> soFar<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>p1<span style="color: #339933; font-weight: bold;">,</span> p2<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> 
                <span style="color: #06c; font-weight: bold;">if</span> distance p1 p2 <span style="color: #339933; font-weight: bold;">&lt;</span> <span style="font-weight: bold;">min</span> <span style="color: #06c; font-weight: bold;">then</span> <span style="color: green;">&#40;</span>distance p1 p2<span style="color: #339933; font-weight: bold;">,</span> Just<span style="color: green;">&#40;</span>p1<span style="color: #339933; font-weight: bold;">,</span> p2<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">else</span> acc<span style="color: green;">&#41;</span> 
              <span style="color: green;">&#40;</span><span style="font-weight: bold;">fromIntegral</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">maxBound</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> Nothing<span style="color: green;">&#41;</span> 
              <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span>pairs <span style="color: #339933; font-weight: bold;">!!</span> i<span style="color: #339933; font-weight: bold;">,</span> pairs <span style="color: #339933; font-weight: bold;">!!</span> j<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">|</span> i <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="color: green;">&#91;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">..</span><span style="font-weight: bold;">length</span> pairs <span style="color: #339933; font-weight: bold;">-</span> <span style="color: red;">1</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> j <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="color: green;">&#91;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">..</span><span style="font-weight: bold;">length</span> pairs<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span> <span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> i <span style="color: #339933; font-weight: bold;">/=</span> j<span style="color: green;">&#93;</span>
  <span style="color: #06c; font-weight: bold;">where</span> distance <span style="color: green;">&#40;</span>x1<span style="color: #339933; font-weight: bold;">,</span> y1<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>x2<span style="color: #339933; font-weight: bold;">,</span> y2<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span>  <span style="font-weight: bold;">sqrt</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>x1 <span style="color: #339933; font-weight: bold;">-</span> x2<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">^</span> <span style="color: red;">2</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">+</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>y1 <span style="color: #339933; font-weight: bold;">-</span> y2<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">^</span> <span style="color: red;">2</span><span style="color: green;">&#41;</span></pre></div></div>

<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/f0Wba3GawYk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2012/05/07/haskell-maximum-int-value/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2012/05/07/haskell-maximum-int-value/</feedburner:origLink></item>
		<item>
		<title>neo4j: What question do you want to answer?</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/gWd2DOQ_Sp4/</link>
		<comments>http://www.markhneedham.com/blog/2012/05/05/neo4j-what-question-do-you-want-to-answer/#comments</comments>
		<pubDate>Sat, 05 May 2012 13:20:41 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[neo4j]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=4248</guid>
		<description><![CDATA[Over the past few weeks I&#8217;ve been modelling ThoughtWorks project data in neo4j and I realised that the way that I&#8217;ve been doing this is by considering what question I want to answer and then building a graph to answer it. When I first started doing this the main question I wanted to answer was [...]]]></description>
			<content:encoded><![CDATA[<p>Over the past few weeks I&#8217;ve been modelling ThoughtWorks project data in <a href="http://neo4j.org/">neo4j</a> and I realised that the way that I&#8217;ve been doing this is by considering <strong>what question I want to answer</strong> and then building a graph to answer it.</p>
<p>When I first started doing this the main question I wanted to answer was &#8216;how connected are people to each other&#8217; which led to me modelling the data like this:</p>
<p><img src="http://www.markhneedham.com/blog/wp-content/uploads/2012/05/initial.png" alt="Initial" title="initial.png" border="0" width="413" height="59" /></p>
<p>The &#8216;colleagues with&#8217; relationship stored information about the project the two people had worked on together and how long they&#8217;d worked together.</p>
<p>This design was fine while that was the only question I wanted to answer but after I showed it to a few people it became clear that there were other questions we could ask which would be difficult to answer with it designed this way.</p>
<p>e.g.</p>
<ul>
<li>Which people on project X have I never worked with?</li>
<li>Which person has worked for client X for the longest?</li>
<li>Which people worked together on the same client if not the same project?</li>
</ul>
<p>I therefore need to make &#8216;client&#8217; and &#8216;project&#8217; first class entities in the graph rather than just being there implicitly which favours a design more along these lines:</p>
<p><img src="http://www.markhneedham.com/blog/wp-content/uploads/2012/05/initial1.png" alt="Initial" title="initial.png" border="0" width="459" height="203" /></p>
<p>It makes it a little more difficult to answer the initial question about connections between people but opens up the answers to other questions such as the ones detailed above.</p>
<p>I&#8217;m still getting used to this way of modelling data but it feels like you&#8217;re driven towards designing your data in a way that&#8217;s useful to you as opposed to the relational approach where you tend to model relations and then work out what you want to do with the data.</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/gWd2DOQ_Sp4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2012/05/05/neo4j-what-question-do-you-want-to-answer/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2012/05/05/neo4j-what-question-do-you-want-to-answer/</feedburner:origLink></item>
		<item>
		<title>gephi: Centring a graph around an individual node</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/jGisYgXlK30/</link>
		<comments>http://www.markhneedham.com/blog/2012/04/30/gephi-centring-a-graph-around-an-individual-node/#comments</comments>
		<pubDate>Mon, 30 Apr 2012 22:20:45 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[neo4j]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[gephi]]></category>
		<category><![CDATA[graphs]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=4235</guid>
		<description><![CDATA[I spent some time recently playing around with gephi &#8211; an open source platform for creating visualisations of graphs &#8211; to get a bit more insight into the ThoughtWorks graph which I&#8217;ve created in neo4j. I followed Max De Marxi&#8217;s blog post to create a GEFX (Graph Exchange XML Format) file to use in gephi [...]]]></description>
			<content:encoded><![CDATA[<p>I spent some time recently playing around with <a href="http://gephi.org/">gephi</a> &#8211; an open source platform for creating visualisations of graphs &#8211; to get a bit more insight into the ThoughtWorks graph which I&#8217;ve created in neo4j.</p>
<p>I <a href="http://maxdemarzi.com/2012/04/12/using-sigma-js-with-neo4j/">followed Max De Marxi&#8217;s blog post</a> to create a GEFX (Graph Exchange XML Format) file to use in gephi although I later learned that you can import directly from neo4j into gephi which I haven&#8217;t tried yet.</p>
<p>I loaded it into gephi and this is what it looks like:</p>
<div>
<img src="http://www.markhneedham.com/blog/wp-content/uploads/2012/04/full-graph.jpg" alt="Full graph" title="full-graph.jpg" border="0" width="600" height="530" />
</div>
<div style="float:right">
<img src="http://www.markhneedham.com/blog/wp-content/uploads/2012/04/filter-menu-top.jpg" alt="Filter menu top" title="filter-menu-top.jpg" border="0" width="270" height="438" />
</div>
<p>There are massive numbers of connections between almost every node so it&#8217;s pretty hard to see what&#8217;s going on.</p>
<p>I wanted to be able to centre the graph around an individual person and see just the links related to them.</p>
<p>To do that we need to make use of an &#8216;<a href="http://forum.gephi.org/viewtopic.php?t=286">ego</a> <a href="http://blog.ouseful.info/2010/05/10/getting-started-with-gephi-network-visualisation-app-%E2%80%93-my-facebook-network-part-iii-ego-filters-and-simple-network-stats/">filter</a>&#8216; so the first step is to make the filters menu visible by clicking &#8216;Window > Filters&#8217;. </p>
<p>We can then choose the ego filter which sits under &#8216;Library > Topology&#8217; and fill in the ID of the node that we want to centre the graph around as well as choose the depth of the traversal.</p>
<div>
<img src="http://www.markhneedham.com/blog/wp-content/uploads/2012/04/filter-menu-bottom.jpg" alt="Filter menu bottom" title="filter-menu-bottom.jpg" border="0" width="257" height="185" />
</div>
<p>In this case any traversal above 1 will end up traversing the majority of the graph since the average distance between nodes in this graph is just above 2.5.</p>
<p>I run the &#8216;Force Atlas&#8217; layout algorithm over it and this is what we end up with:</p>
<div>
<img src="http://www.markhneedham.com/blog/wp-content/uploads/2012/04/my-graph.jpg" alt="My graph" title="my-graph.jpg" border="0" width="545" height="479" />
</div>
<p>My graph shows some interesting clustering of nodes where the ones on the right are people I worked with in India, top left are people I worked with in Australia, bottom left people in the UK and the ones towards the lower middle are people I went to ThoughtWorks University with.</p>
<p>There are a load of other filters to choose from but the ego filter is pretty cool!</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/jGisYgXlK30" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2012/04/30/gephi-centring-a-graph-around-an-individual-node/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2012/04/30/gephi-centring-a-graph-around-an-individual-node/</feedburner:origLink></item>
		<item>
		<title>Performance: Caching per request</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/mzhvG08RF3U/</link>
		<comments>http://www.markhneedham.com/blog/2012/04/30/performance-caching-per-request/#comments</comments>
		<pubDate>Mon, 30 Apr 2012 21:45:50 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=4225</guid>
		<description><![CDATA[A couple of years ago I wrote a post describing an approach my then colleague Christian Blunden used to help improve the performance of an application where you try to do expensive things less or find another way to do them. On the application I&#8217;m currently working on we load reference data from an Oracle [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of years ago I wrote a post describing an approach my then colleague <a href="https://twitter.com/#!/christianralph">Christian Blunden</a> used to help improve the performance of an application where you try to <a href="http://www.markhneedham.com/blog/2010/07/10/performance-do-it-less-or-find-another-way/">do expensive things less or find another way to do them</a>.</p>
<p>On the application I&#8217;m currently working on we load reference data from an Oracle database into memory based on configurations provided by the user.</p>
<p>There are multiple configurations and then multiple ways that those configurations can be priced so we have two nested for loops in which we load data and then perform calculations on it.</p>
<p>When profiling the application we realised that some of the database calls being made with the same parameters and were therefore loading back the same reference data that we&#8217;d already loaded.</p>
<p>The most obvious way to solve this problem would be to move the code out of the loop and make less calls to the database that way but logically the  domain is expressed more clearly when it&#8217;s inside the loop.</p>
<p><a href="http://www.linkedin.com/pub/alex-harin/13/40b/716">Alex</a> therefore came up with an alternative approach where we wrap the database calling code in a caching decorator.</p>
<p>The caching decorator is a request scoped object so we&#8217;re only caching those results for a short amount of time which means that we <a href="http://martinfowler.com/bliki/TwoHardThings.html">don&#8217;t have to worry about cache invalidation</a> because it&#8217;s thrown away when the request has been processed.</p>
<p>I&#8217;ve previously seen this problem solving by using a Hibernate second level cache which would cache results across requests. </p>
<p>In our application there are more likely to be calls to the database with the same parameters within the same request rather than across requests.</p>
<p>The load on the system is likely to come from complex requests where many prices needed to be calculated rather than from a huge frequency of requests </p>
<p>If that changes then we always have the option of caching at both levels but at the moment our current approach seems to work pretty well.</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/mzhvG08RF3U" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2012/04/30/performance-caching-per-request/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2012/04/30/performance-caching-per-request/</feedburner:origLink></item>
	</channel>
</rss>

