<?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, 23 May 2012 08:25:38 +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: Using monoids when sorting by multiple parameters</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/TZd1O2q67N8/</link>
		<comments>http://www.markhneedham.com/blog/2012/05/23/haskell-using-monoids-when-sorting-by-multiple-parameters/#comments</comments>
		<pubDate>Wed, 23 May 2012 06:44:41 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=4317</guid>
		<description><![CDATA[On the project I&#8217;ve been working on we had a requirement to sort a collection of rows by 4 different criteria such that if two items matched for the first criteria we should consider the second criteria and so on. If we wrote that code in Haskell it would read a bit like this: data [...]]]></description>
			<content:encoded><![CDATA[<p>On the project I&#8217;ve been working on we had a requirement to sort a collection of rows by 4 different criteria such that if two items matched for the first criteria we should consider the second criteria and so on.</p>
<p>If we wrote that code in Haskell it would read a bit like this:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">data</span> Row <span style="color: #339933; font-weight: bold;">=</span> Row <span style="color: green;">&#123;</span> shortListed <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Bool</span><span style="color: #339933; font-weight: bold;">,</span> cost <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Float</span><span style="color: #339933; font-weight: bold;">,</span> distance1 <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: #339933; font-weight: bold;">,</span> distance2 <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span> <span style="color: green;">&#125;</span> <span style="color: #06c; font-weight: bold;">deriving</span> <span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Show</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: #cccc00; font-weight: bold;">Eq</span><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;">import</span> Data<span style="color: #339933; font-weight: bold;">.</span><span style="color: #cccc00; font-weight: bold;">Ord</span>
<span style="color: #06c; font-weight: bold;">import</span> Data<span style="color: #339933; font-weight: bold;">.</span>List
&nbsp;
compareRow <span style="color: #339933; font-weight: bold;">::</span> Row <span style="color: #339933; font-weight: bold;">-&gt;</span> Row <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Ordering</span>
compareRow x y <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">if</span> comparing <span style="color: green;">&#40;</span><span style="font-weight: bold;">not</span> <span style="color: #339933; font-weight: bold;">.</span> shortListed<span style="color: green;">&#41;</span> x y <span style="color: #339933; font-weight: bold;">==</span> EQ <span style="color: #06c; font-weight: bold;">then</span> 
                    <span style="color: #06c; font-weight: bold;">if</span> comparing cost x y <span style="color: #339933; font-weight: bold;">==</span> EQ <span style="color: #06c; font-weight: bold;">then</span>
                      <span style="color: #06c; font-weight: bold;">if</span> comparing distance1 x y <span style="color: #339933; font-weight: bold;">==</span> EQ <span style="color: #06c; font-weight: bold;">then</span>
                        comparing distance2 x y
                      <span style="color: #06c; font-weight: bold;">else</span>
                        comparing distance1 x y
                    <span style="color: #06c; font-weight: bold;">else</span>
                      comparing cost x y
                  <span style="color: #06c; font-weight: bold;">else</span> 
                    comparing <span style="color: green;">&#40;</span><span style="font-weight: bold;">not</span> <span style="color: #339933; font-weight: bold;">.</span> shortListed<span style="color: green;">&#41;</span> x y</pre></div></div>

<p>We can then sort a list of rows 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> <span style="color: #06c; font-weight: bold;">let</span> shortListedRow <span style="color: #339933; font-weight: bold;">=</span> Row <span style="color: green;">&#123;</span>shortListed <span style="color: #339933; font-weight: bold;">=</span> True<span style="color: #339933; font-weight: bold;">,</span> cost <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">10.0</span><span style="color: #339933; font-weight: bold;">,</span> distance1 <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">20</span><span style="color: #339933; font-weight: bold;">,</span> distance2 <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">30</span> <span style="color: green;">&#125;</span>
&nbsp;
<span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: #06c; font-weight: bold;">let</span> nonShortListedRow <span style="color: #339933; font-weight: bold;">=</span> Row <span style="color: green;">&#123;</span>shortListed <span style="color: #339933; font-weight: bold;">=</span> False<span style="color: #339933; font-weight: bold;">,</span> cost <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">10.0</span><span style="color: #339933; font-weight: bold;">,</span> distance1 <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">20</span><span style="color: #339933; font-weight: bold;">,</span> distance2 <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">30</span> <span style="color: green;">&#125;</span>
&nbsp;
<span style="color: #339933; font-weight: bold;">&gt;</span> sortBy compareRow <span style="color: green;">&#91;</span>nonShortListedRow<span style="color: #339933; font-weight: bold;">,</span> shortListedRow<span style="color: green;">&#93;</span>
<span style="color: green;">&#91;</span>Row <span style="color: green;">&#123;</span>shortListed <span style="color: #339933; font-weight: bold;">=</span> True<span style="color: #339933; font-weight: bold;">,</span> cost <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">10.0</span><span style="color: #339933; font-weight: bold;">,</span> distance1 <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">20</span><span style="color: #339933; font-weight: bold;">,</span> distance2 <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">30</span><span style="color: green;">&#125;</span><span style="color: #339933; font-weight: bold;">,</span>
 Row <span style="color: green;">&#123;</span>shortListed <span style="color: #339933; font-weight: bold;">=</span> False<span style="color: #339933; font-weight: bold;">,</span> cost <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">10.0</span><span style="color: #339933; font-weight: bold;">,</span> distance1 <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">20</span><span style="color: #339933; font-weight: bold;">,</span> distance2 <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">30</span><span style="color: green;">&#125;</span><span style="color: green;">&#93;</span></pre></div></div>

<p>It works but it&#8217;s messy and we couldn&#8217;t see what abstraction we should be using to simplify the code.</p>
<p>I was continuing with my reading of <a href="http://learnyouahaskell.com/functors-applicative-functors-and-monoids">Functors, Applicative Functors and Monoids</a> yesterday and got to the section on Monoids which showed an example for simplifying this type of code.</p>
<p>The definition of a Monoid from the Haskell source code is:</p>
<blockquote><p>
Types with an associative binary operation that has an identity
</p></blockquote>
<p>But I prefer <a href="http://blog.sigfpe.com/2009/01/haskell-monoids-and-their-uses.html">Dan Piponi&#8217;s definition</a>:</p>
<blockquote><p>
In Haskell, a monoid is a type with a rule for how two elements of that type can be combined to make another element of the same type. </p>
<p>To be a monoid there also needs to be an element that you can think of as representing &#8216;nothing&#8217; in the sense that when it&#8217;s combined with other elements it leaves the other element unchanged.
</p></blockquote>
<p>In our case we have a bunch of things of type &#8216;Ordering&#8217; and we want to combine them all together and end up with a final &#8216;Ordering&#8217; which takes them all into account.</p>
<p>For example if we were comparing the following two rows:</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> row1 <span style="color: #339933; font-weight: bold;">=</span> Row <span style="color: green;">&#123;</span>shortListed <span style="color: #339933; font-weight: bold;">=</span> True<span style="color: #339933; font-weight: bold;">,</span> cost <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">10.0</span><span style="color: #339933; font-weight: bold;">,</span> distance1 <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span> distance2 <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">30</span> <span style="color: green;">&#125;</span>
<span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: #06c; font-weight: bold;">let</span> row2 <span style="color: #339933; font-weight: bold;">=</span> Row <span style="color: green;">&#123;</span>shortListed <span style="color: #339933; font-weight: bold;">=</span> True<span style="color: #339933; font-weight: bold;">,</span> cost <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">10.0</span><span style="color: #339933; font-weight: bold;">,</span> distance1 <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">100</span><span style="color: #339933; font-weight: bold;">,</span> distance2 <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">30</span> <span style="color: green;">&#125;</span>
<span style="color: #339933; font-weight: bold;">&gt;</span> compareRow row1 row2
LT</pre></div></div>

<p>When we compare their shortListed value we get back &#8216;EQ&#8217;, so we compare their cost value and get back &#8216;EQ&#8217; and finally we compare their distance1 value which gives back &#8216;LT&#8217; which is our final value.</p>
<p>We can make use of the Ordering Monoid to do this rather than all the nested if statements.</p>
<p>Monoid is a <a href="http://www.markhneedham.com/blog/2012/05/22/scalahaskell-a-simple-example-of-type-classes/">type class</a> defined like so:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">class</span> Monoid a <span style="color: #06c; font-weight: bold;">where</span>
  mempty  <span style="color: #339933; font-weight: bold;">::</span> a
  mappend <span style="color: #339933; font-weight: bold;">::</span> a <span style="color: #339933; font-weight: bold;">-&gt;</span> a <span style="color: #339933; font-weight: bold;">-&gt;</span> a
  mconcat <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> a
  mconcat <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">foldr</span> mappend mempty</pre></div></div>

<p>&#8216;mempty&#8217; represents the identity value for a monoid i.e. the &#8216;nothing&#8217; in Dan Piponi&#8217;s definition. If we combine anything with this then we should get that thing back.</p>
<p>The most interesting function here is &#8216;mappend&#8217; which we use to combine together two elements of a type. Each instance of Monoid needs to define this function for themselves.</p>
<p>The Ordering Monoid is defined like so:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">instance</span> Monoid <span style="color: #cccc00; font-weight: bold;">Ordering</span> <span style="color: #06c; font-weight: bold;">where</span>
  mempty         <span style="color: #339933; font-weight: bold;">=</span> EQ
  LT `mappend` <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">=</span> LT
  EQ `mappend` y <span style="color: #339933; font-weight: bold;">=</span> y
  GT `mappend` <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">=</span> GT</pre></div></div>

<p>What makes this work for us is that we always keep the value on the left unless it&#8217;s &#8216;EQ&#8217; in which case we take the value on the right. </p>
<p>Therefore as soon as one of our comparisons returns a non &#8216;EQ&#8217; value that will be the value that eventually gets returned.</p>
<p>e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; GT `mappend` LT `mappend` EQ
GT</pre></div></div>

<p>Our &#8216;row1&#8242;/&#8217;row2&#8242; comparison would look like this using &#8216;mappend&#8217;:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; EQ `mappend` EQ `mappend` LT
LT</pre></div></div>

<p>We can then change our &#8216;compareRow&#8217; function:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">compareRow x y <span style="color: #339933; font-weight: bold;">=</span> comparing <span style="color: green;">&#40;</span><span style="font-weight: bold;">not</span> <span style="color: #339933; font-weight: bold;">.</span> shortListed<span style="color: green;">&#41;</span> x y `mappend` comparing cost x y `mappend` comparing distance1 x y `mappend` comparing distance2 x y</pre></div></div>

<p>We can simplify this further by making use of &#8216;mconcat&#8217; which folds over a list of monoids applying &#8216;mappend&#8217; each time.</p>
<p>For example we could replace our &#8216;row1&#8242;/&#8217;row2&#8242; comparison with the following:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; mconcat [EQ, EQ, LT]
LT</pre></div></div>

<p>And &#8216;compareRow&#8217; now reads like this:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">compareRow x y <span style="color: #339933; font-weight: bold;">=</span> mconcat <span style="color: green;">&#91;</span>comparing <span style="color: green;">&#40;</span><span style="font-weight: bold;">not</span> <span style="color: #339933; font-weight: bold;">.</span> shortListed<span style="color: green;">&#41;</span> x y<span style="color: #339933; font-weight: bold;">,</span>  comparing cost x y<span style="color: #339933; font-weight: bold;">,</span> comparing distance1 x y<span style="color: #339933; font-weight: bold;">,</span> comparing distance2 x y<span style="color: green;">&#93;</span></pre></div></div>

<p>We&#8217;re still repeating the &#8216;comparing&#8217; bit of code every time so I extracted that into a function:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">by <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Ord</span> a <span style="color: #339933; font-weight: bold;">=&gt;</span> <span style="color: green;">&#40;</span>b <span style="color: #339933; font-weight: bold;">-&gt;</span> a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> b <span style="color: #339933; font-weight: bold;">-&gt;</span> b <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Ordering</span>
by fn x y <span style="color: #339933; font-weight: bold;">=</span> comparing fn x y</pre></div></div>

<p>We then need to apply those functions to x and y to get our collection of monoids we can pass to mconcat:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">compareRow x y <span style="color: #339933; font-weight: bold;">=</span> mconcat <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">map</span> <span style="color: green;">&#40;</span>\fn <span style="color: #339933; font-weight: bold;">-&gt;</span> fn x y<span style="color: green;">&#41;</span> <span style="color: green;">&#91;</span>by <span style="color: green;">&#40;</span><span style="font-weight: bold;">not</span> <span style="color: #339933; font-weight: bold;">.</span> shortListed<span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span> by cost<span style="color: #339933; font-weight: bold;">,</span> by distance1<span style="color: #339933; font-weight: bold;">,</span> by distance2<span style="color: green;">&#93;</span></pre></div></div>

<p><del datetime="2012-05-23T08:24:29+00:00">One problem with this code is that we&#8217;re now comparing by all the parameters when we can actually stop once we&#8217;ve found a non equality.</del></p>
<p><del datetime="2012-05-23T08:25:17+00:00">I&#8217;m sure there&#8217;s a clever way to short circuit this but I&#8217;m not sure what it is at the moment!</del></p>
<p>As BeRwet points out in the comments what I wrote here is not actually the case, it is lazily evaluated!</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/TZd1O2q67N8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2012/05/23/haskell-using-monoids-when-sorting-by-multiple-parameters/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2012/05/23/haskell-using-monoids-when-sorting-by-multiple-parameters/</feedburner:origLink></item>
		<item>
		<title>Scala/Haskell: A simple example of type classes</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/bNuMyN0H2zc/</link>
		<comments>http://www.markhneedham.com/blog/2012/05/22/scalahaskell-a-simple-example-of-type-classes/#comments</comments>
		<pubDate>Tue, 22 May 2012 10:26:49 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=4313</guid>
		<description><![CDATA[I never really understood type classes when I was working with Scala but I recently came across a video where Dan Rosen explains them pretty well. Since the last time I worked in Scala I&#8217;ve been playing around with Haskell where type classes are much more common &#8211; for example if we want to compare [...]]]></description>
			<content:encoded><![CDATA[<p>I never really understood type classes when I was working with Scala but I recently came across <a href="http://marakana.com/s/scala_typeclasses,1117/index.html">a video where Dan Rosen explains them pretty well</a>. </p>
<p>Since the last time I worked in Scala I&#8217;ve been playing around with Haskell where type classes are much more common &#8211; for example if we want to compare two values we need to make sure that their type extends the &#8216;Eq&#8217; type class.</p>
<p><a href="http://learnyouahaskell.com/making-our-own-types-and-typeclasses">Learn Me a Haskell&#8217;s chapter on type classes</a> defines them like so:</p>
<blockquote><p>
Typeclasses are like interfaces. </p>
<p>A typeclass defines some behaviour (like comparing for equality, comparing for ordering, enumeration) and then types that can behave in that way are made instances of that typeclass. </p>
<p>The behaviour of typeclasses is achieved by defining functions or just type declarations that we then implement. So when we say that a type is an instance of a typeclass, we mean that we can use the functions that the typeclass defines with that type.
</p></blockquote>
<p>In that chapter we then go on <a href="http://learnyouahaskell.com/making-our-own-types-and-typeclasses#a-yes-no-typeclass">to create a &#8216;YesNo&#8217; type class</a> which defines Boolean semantics for all different types. </p>
<p>We start by defining the type class like so:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">class</span> YesNo a <span style="color: #06c; font-weight: bold;">where</span>
  yesno <span style="color: #339933; font-weight: bold;">::</span> a <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Bool</span></pre></div></div>

<p>Any type which extends that type class can call this &#8216;yesno&#8217; function and find out the truthyness of its value.</p>
<p>e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">instance</span> YesNo <span style="color: #cccc00; font-weight: bold;">Int</span> <span style="color: #06c; font-weight: bold;">where</span>
  yesno <span style="color: red;">0</span> <span style="color: #339933; font-weight: bold;">=</span> False
  yesno <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">=</span> True</pre></div></div>

<p>If we call that:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #339933; font-weight: bold;">&gt;</span> yesno <span style="color: green;">&#40;</span><span style="color: red;">0</span> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#41;</span>
False
&nbsp;
<span style="color: #339933; font-weight: bold;">&gt;</span> yesno <span style="color: green;">&#40;</span><span style="color: red;">1</span> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#41;</span>
True</pre></div></div>

<p>We get the expected result, but if we try to call it for a type which hasn&#8217;t defined an instance of &#8216;YesNo&#8217;:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #339933; font-weight: bold;">&gt;</span> yesno <span style="background-color: #3cb371;">&quot;mark&quot;</span>
&nbsp;
    No <span style="color: #06c; font-weight: bold;">instance</span> for <span style="color: green;">&#40;</span>YesNo <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Char</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span>
      arising from a use <span style="color: #06c; font-weight: bold;">of</span> `yesno'
    Possible fix: add an <span style="color: #06c; font-weight: bold;">instance</span> declaration for <span style="color: green;">&#40;</span>YesNo <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Char</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span>
    In the expression: yesno <span style="background-color: #3cb371;">&quot;mark&quot;</span>
    In an equation for `it': it <span style="color: #339933; font-weight: bold;">=</span> yesno <span style="background-color: #3cb371;">&quot;mark&quot;</span></pre></div></div>

<p>In Scala we can use traits and implicits to achieve the same effect. First we define the &#8216;YesNo&#8217; trait:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">trait</span> YesNo<span style="color: #F78811;">&#91;</span>A<span style="color: #F78811;">&#93;</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> yesno<span style="color: #F78811;">&#40;</span>value<span style="color: #000080;">:</span>A<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:</span> Boolean
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>Then we define an implicit value in a companion object which creates an instance of the &#8216;YesNo&#8217; type class for Ints:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">object</span> YesNo <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">implicit</span> <span style="color: #0000ff; font-weight: bold;">val</span> intYesNo <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> YesNo<span style="color: #F78811;">&#91;</span>Int<span style="color: #F78811;">&#93;</span> <span style="color: #F78811;">&#123;</span> 
    <span style="color: #0000ff; font-weight: bold;">def</span> yesno<span style="color: #F78811;">&#40;</span>value<span style="color: #000080;">:</span>Int<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> 
      value <span style="color: #0000ff; font-weight: bold;">match</span> <span style="color: #F78811;">&#123;</span> <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #F78811;">0</span> <span style="color: #000080;">=&gt;</span> <span style="color: #0000ff; font-weight: bold;">false</span><span style="color: #000080;">;</span>  <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #000080;">_</span> <span style="color: #000080;">=&gt;</span> <span style="color: #0000ff; font-weight: bold;">true</span> <span style="color: #F78811;">&#125;</span> <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>We then need to call our &#8216;yesno&#8217; function and the idea is that if we&#8217;ve defined a type class instance for the type we call it with it will return us a boolean value and if not then we&#8217;ll get a compilation error.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">object</span> YesNoWriter <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> write<span style="color: #F78811;">&#91;</span>A<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>value<span style="color: #000080;">:</span>A<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">implicit</span>  conv<span style="color: #000080;">:</span> YesNo<span style="color: #F78811;">&#91;</span>A<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:</span> Boolean <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    conv.<span style="color: #000000;">yesno</span><span style="color: #F78811;">&#40;</span>value<span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>If we call that:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; YesNoWriter.write(1)
res1: Boolean = true
&nbsp;
&gt; YesNoWriter.write(0)
res2: Boolean = false</pre></div></div>

<p>It works as expected, but if we try to call it with a type which wasn&#8217;t defined for the &#8216;YesNo&#8217; type class we run into trouble:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; YesNoWriter.write(&quot;mark&quot;)
:10: error: could not find implicit value for parameter conv: YesNo[java.lang.String]
       YesNoWriter.write(&quot;mark&quot;)</pre></div></div>

<p>We can also define YesNoWriter like this by making use of <a href="http://stackoverflow.com/questions/3855595/scala-identifier-implicitly">context bounds</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">object</span> YesNoWriter <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> write<span style="color: #F78811;">&#91;</span>A<span style="color: #000080;">:</span>YesNo<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>value<span style="color: #000080;">:</span>A<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:</span> Boolean <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    implicitly<span style="color: #F78811;">&#91;</span>YesNo<span style="color: #F78811;">&#91;</span>A<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#93;</span>.<span style="color: #000000;">yesno</span><span style="color: #F78811;">&#40;</span>value<span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>I think this pattern is preferred when we might just be tunnelling the implicit parameter through to another method but we can still use it here and use the &#8216;implicitly&#8217; method to get access to the implicit value.</p>
<p>I&#8217;m still not entirely sure about the use of implicits but in this case they provide another way to implement polymorphism without having to use inheritance. </p>
<p><a href="http://marakana.com/s/scala_typeclasses,1117/index.html">Dan Rosen goes into much more detail about type classes and implicits</a> and I <a href="http://www.markhneedham.com/blog/2011/07/19/scala-rolling-with-implicit/">wrote about how we were using them on a project I worked on last year in an earlier blog post</a> if you want to learn more.</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/bNuMyN0H2zc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2012/05/22/scalahaskell-a-simple-example-of-type-classes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2012/05/22/scalahaskell-a-simple-example-of-type-classes/</feedburner:origLink></item>
		<item>
		<title>Haskell: My first attempt with QuickCheck and HUnit</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/WCsYSXZff0w/</link>
		<comments>http://www.markhneedham.com/blog/2012/05/20/haskell-my-first-attempt-with-quickcheck-and-hunit/#comments</comments>
		<pubDate>Sun, 20 May 2012 19:09:52 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=4306</guid>
		<description><![CDATA[As I mentioned in a blog post a few days I&#8217;ve started learning QuickCheck with the test-framework package as suggested by David Turner. I first needed to install test-framework and some dependencies using cabal: &#62; cabal install test-framework &#62; cabal install test-framework-quickcheck &#62; cabal install test-framework-hunit I thought it&#8217;d be interesting to try and write [...]]]></description>
			<content:encoded><![CDATA[<p>As I <a href="http://www.markhneedham.com/blog/2012/05/16/haskell-writing-a-custom-equality-operator/">mentioned in a blog post a few days</a> I&#8217;ve started learning QuickCheck with the <a href="http://batterseapower.github.com/test-framework/">test-framework</a> package as suggested by David Turner.</p>
<p>I first needed to install test-framework and some dependencies using <cite><a href="http://www.haskell.org/cabal/">cabal</a></cite>:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #339933; font-weight: bold;">&gt;</span> cabal install test<span style="color: #339933; font-weight: bold;">-</span>framework
<span style="color: #339933; font-weight: bold;">&gt;</span> cabal install test<span style="color: #339933; font-weight: bold;">-</span>framework<span style="color: #339933; font-weight: bold;">-</span>quickcheck
<span style="color: #339933; font-weight: bold;">&gt;</span> cabal install test<span style="color: #339933; font-weight: bold;">-</span>framework<span style="color: #339933; font-weight: bold;">-</span>hunit</pre></div></div>

<p>I thought it&#8217;d be interesting to try and write some tests around the <a href="http://www.markhneedham.com/blog/2012/02/28/haskell-creating-a-sliding-window-over-a-collection/">windowed function that I wrote a few months ago</a>:</p>
<p><cite>Windowed.hs</cite></p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">module</span> Windowed <span style="color: green;">&#40;</span>windowed<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
&nbsp;
windowed <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span><span style="color: green;">&#93;</span>
windowed size <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
windowed size ls<span style="color: #339933; font-weight: bold;">@</span><span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> 
  <span style="color: #06c; font-weight: bold;">if</span> <span style="font-weight: bold;">length</span> ls <span style="color: #339933; font-weight: bold;">&gt;=</span> size <span style="color: #06c; font-weight: bold;">then</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">take</span> size ls<span style="color: green;">&#41;</span> : windowed size xs 
  <span style="color: #06c; font-weight: bold;">else</span> windowed size x</pre></div></div>

<p>I wrote my first property like so:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">import</span> Windowed
&nbsp;
<span style="color: #06c; font-weight: bold;">import</span> Test<span style="color: #339933; font-weight: bold;">.</span>Framework <span style="color: green;">&#40;</span>defaultMain<span style="color: #339933; font-weight: bold;">,</span> testGroup<span style="color: green;">&#41;</span>
<span style="color: #06c; font-weight: bold;">import</span> Test<span style="color: #339933; font-weight: bold;">.</span>Framework<span style="color: #339933; font-weight: bold;">.</span>Providers<span style="color: #339933; font-weight: bold;">.</span>QuickCheck <span style="color: green;">&#40;</span>testProperty<span style="color: green;">&#41;</span>
<span style="color: #06c; font-weight: bold;">import</span> Test<span style="color: #339933; font-weight: bold;">.</span>QuickCheck
&nbsp;
main <span style="color: #339933; font-weight: bold;">=</span> defaultMain tests
&nbsp;
tests <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span>
  testGroup <span style="background-color: #3cb371;">&quot;windowed&quot;</span> <span style="color: green;">&#91;</span>
    testProperty <span style="background-color: #3cb371;">&quot;should not window if n is bigger than list size&quot;</span> prop<span style="color: #339933; font-weight: bold;">_</span>nothing<span style="color: #339933; font-weight: bold;">_</span>if<span style="color: #339933; font-weight: bold;">_</span>n<span style="color: #339933; font-weight: bold;">_</span>too<span style="color: #339933; font-weight: bold;">_</span>large
  <span style="color: green;">&#93;</span>
<span style="color: green;">&#93;</span> 
&nbsp;
prop<span style="color: #339933; font-weight: bold;">_</span>nothing<span style="color: #339933; font-weight: bold;">_</span>if<span style="color: #339933; font-weight: bold;">_</span>n<span style="color: #339933; font-weight: bold;">_</span>too<span style="color: #339933; font-weight: bold;">_</span>large n xs <span style="color: #339933; font-weight: bold;">=</span> n <span style="color: #339933; font-weight: bold;">&gt;</span> <span style="font-weight: bold;">length</span> xs <span style="color: #339933; font-weight: bold;">==&gt;</span>  windowed n xs <span style="color: #339933; font-weight: bold;">==</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span></pre></div></div>

<p>And tried to compile the file:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">ghc -package test-framework -package test-framework-quickcheck -threaded WindowedQC.hs -o Windowed</pre></div></div>

<p>Which resulted in this compilation error:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">WindowedQC.hs:16:17:
    No instance for (QuickCheck-1.2.0.1:Test.QuickCheck.Testable
                       (Gen Prop))
      arising from a use of `testProperty'
    Possible fix:
      add an instance declaration for
      (QuickCheck-1.2.0.1:Test.QuickCheck.Testable (Gen Prop))</pre></div></div>

<p>According to the <a href="http://batterseapower.github.com/test-framework/">test-framework home page</a> this error happens when we have more than one version of QuickCheck installed so we need to tell &#8216;ghc&#8217; which one to use:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">ghc -package test-framework -package test-framework-quickcheck -package QuickCheck-1.2.0.1 -threaded WindowedQC.hs -o Windowed</pre></div></div>

<p>That solved the first compilation problem but I still had another:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">WindowedQC.hs:16:17:
    Ambiguous type variable `a0' in the constraints:
      (Arbitrary a0) arising from a use of `testProperty'
                     at WindowedQC.hs:12:17-28
      (Show a0) arising from a use of `testProperty'
                at WindowedQC.hs:12:17-28
      (Eq a0) arising from a use of `prop_nothing_if_n_too_large'
              at WindowedQC.hs:12:80-106
    Probable fix: add a type signature that fixes these type variable(s)</pre></div></div>

<p>The way to solve this problem is to <a href="https://raw.github.com/batterseapower/test-framework/master/example/Test/Framework/Example.lhs">explicitly state which types will be passed to the property</a> like so:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">prop<span style="color: #339933; font-weight: bold;">_</span>nothing<span style="color: #339933; font-weight: bold;">_</span>if<span style="color: #339933; font-weight: bold;">_</span>n<span style="color: #339933; font-weight: bold;">_</span>too<span style="color: #339933; font-weight: bold;">_</span>large n xs <span style="color: #339933; font-weight: bold;">=</span> n <span style="color: #339933; font-weight: bold;">&gt;</span> <span style="font-weight: bold;">length</span> xs <span style="color: #339933; font-weight: bold;">==&gt;</span>  windowed n xs <span style="color: #339933; font-weight: bold;">==</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
    <span style="color: #06c; font-weight: bold;">where</span> types <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>xs <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><span style="color: #339933; font-weight: bold;">,</span> n <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#41;</span></pre></div></div>

<p>I eventually ended up with the following:</p>
<p><cite>WindowedQC.hs</cite></p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">import</span> Windowed
&nbsp;
<span style="color: #06c; font-weight: bold;">import</span> Test<span style="color: #339933; font-weight: bold;">.</span>Framework <span style="color: green;">&#40;</span>defaultMain<span style="color: #339933; font-weight: bold;">,</span> testGroup<span style="color: green;">&#41;</span>
<span style="color: #06c; font-weight: bold;">import</span> Test<span style="color: #339933; font-weight: bold;">.</span>Framework<span style="color: #339933; font-weight: bold;">.</span>Providers<span style="color: #339933; font-weight: bold;">.</span>QuickCheck <span style="color: green;">&#40;</span>testProperty<span style="color: green;">&#41;</span>
<span style="color: #06c; font-weight: bold;">import</span> Test<span style="color: #339933; font-weight: bold;">.</span>QuickCheck
&nbsp;
<span style="color: #06c; font-weight: bold;">import</span> Test<span style="color: #339933; font-weight: bold;">.</span>Framework<span style="color: #339933; font-weight: bold;">.</span>Providers<span style="color: #339933; font-weight: bold;">.</span>HUnit
<span style="color: #06c; font-weight: bold;">import</span> Test<span style="color: #339933; font-weight: bold;">.</span>HUnit
&nbsp;
main <span style="color: #339933; font-weight: bold;">=</span> defaultMain tests
&nbsp;
tests <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span>
  testGroup <span style="background-color: #3cb371;">&quot;windowed&quot;</span> <span style="color: green;">&#91;</span>
    testProperty <span style="background-color: #3cb371;">&quot;should not window if n is bigger than list size&quot;</span> prop<span style="color: #339933; font-weight: bold;">_</span>nothing<span style="color: #339933; font-weight: bold;">_</span>if<span style="color: #339933; font-weight: bold;">_</span>n<span style="color: #339933; font-weight: bold;">_</span>too<span style="color: #339933; font-weight: bold;">_</span>large<span style="color: #339933; font-weight: bold;">,</span>
    testProperty <span style="background-color: #3cb371;">&quot;should create sub arrays the size of window&quot;</span> prop<span style="color: #339933; font-weight: bold;">_</span>size<span style="color: #339933; font-weight: bold;">_</span>of<span style="color: #339933; font-weight: bold;">_</span>sub<span style="color: #339933; font-weight: bold;">_</span>arrays<span style="color: #339933; font-weight: bold;">_</span>is<span style="color: #339933; font-weight: bold;">_</span>n<span style="color: #339933; font-weight: bold;">,</span>
    testProperty <span style="background-color: #3cb371;">&quot;should group adjacent items into windows&quot;</span> prop<span style="color: #339933; font-weight: bold;">_</span>groups<span style="color: #339933; font-weight: bold;">_</span>adjacent<span style="color: #339933; font-weight: bold;">_</span>items<span style="color: #339933; font-weight: bold;">,</span>                
    testCase <span style="background-color: #3cb371;">&quot;should window a simple list&quot;</span> test<span style="color: #339933; font-weight: bold;">_</span>should<span style="color: #339933; font-weight: bold;">_</span>window<span style="color: #339933; font-weight: bold;">_</span>simple<span style="color: #339933; font-weight: bold;">_</span>list
  <span style="color: green;">&#93;</span>
<span style="color: green;">&#93;</span> 
&nbsp;
prop<span style="color: #339933; font-weight: bold;">_</span>groups<span style="color: #339933; font-weight: bold;">_</span>adjacent<span style="color: #339933; font-weight: bold;">_</span>items n xs <span style="color: #339933; font-weight: bold;">=</span> n <span style="color: #339933; font-weight: bold;">&lt;</span> <span style="font-weight: bold;">length</span> xs <span style="color: #339933; font-weight: bold;">==&gt;</span>  
                                  <span style="font-weight: bold;">not</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">null</span> xs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">==&gt;</span>
                                  n <span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: red;">0</span> <span style="color: #339933; font-weight: bold;">==&gt;</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">last</span> <span style="color: #339933; font-weight: bold;">$</span>  <span style="font-weight: bold;">last</span> <span style="color: #339933; font-weight: bold;">$</span> windowed n xs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">==</span> <span style="font-weight: bold;">last</span> xs
  <span style="color: #06c; font-weight: bold;">where</span> types <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>xs <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><span style="color: #339933; font-weight: bold;">,</span> n <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#41;</span>			
&nbsp;
prop<span style="color: #339933; font-weight: bold;">_</span>nothing<span style="color: #339933; font-weight: bold;">_</span>if<span style="color: #339933; font-weight: bold;">_</span>n<span style="color: #339933; font-weight: bold;">_</span>too<span style="color: #339933; font-weight: bold;">_</span>large n xs <span style="color: #339933; font-weight: bold;">=</span> n <span style="color: #339933; font-weight: bold;">&gt;</span> <span style="font-weight: bold;">length</span> xs <span style="color: #339933; font-weight: bold;">==&gt;</span>  windowed n xs <span style="color: #339933; font-weight: bold;">==</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
  <span style="color: #06c; font-weight: bold;">where</span> types <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>xs <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><span style="color: #339933; font-weight: bold;">,</span> n <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#41;</span>
&nbsp;
prop<span style="color: #339933; font-weight: bold;">_</span>size<span style="color: #339933; font-weight: bold;">_</span>of<span style="color: #339933; font-weight: bold;">_</span>sub<span style="color: #339933; font-weight: bold;">_</span>arrays<span style="color: #339933; font-weight: bold;">_</span>is<span style="color: #339933; font-weight: bold;">_</span>n n xs <span style="color: #339933; font-weight: bold;">=</span>  n <span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: red;">0</span> <span style="color: #339933; font-weight: bold;">==&gt;</span> <span style="font-weight: bold;">all</span> <span style="color: green;">&#40;</span>\subArray <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="font-weight: bold;">length</span> subArray <span style="color: #339933; font-weight: bold;">==</span> n<span style="color: green;">&#41;</span>  <span style="color: green;">&#40;</span>windowed n xs<span style="color: green;">&#41;</span>  
  <span style="color: #06c; font-weight: bold;">where</span> types <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>xs <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><span style="color: #339933; font-weight: bold;">,</span> n <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#41;</span>
&nbsp;
test<span style="color: #339933; font-weight: bold;">_</span>should<span style="color: #339933; font-weight: bold;">_</span>window<span style="color: #339933; font-weight: bold;">_</span>simple<span style="color: #339933; font-weight: bold;">_</span>list <span style="color: #339933; font-weight: bold;">=</span> 
  windowed <span style="color: red;">2</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;">10</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;">&#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: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#91;</span><span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">3</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#91;</span><span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">4</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#91;</span><span style="color: red;">4</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">5</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#91;</span><span style="color: red;">5</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">6</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#91;</span><span style="color: red;">6</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">7</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#91;</span><span style="color: red;">7</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">8</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#91;</span><span style="color: red;">8</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">9</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#91;</span><span style="color: red;">9</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">10</span><span style="color: green;">&#93;</span><span style="color: green;">&#93;</span></pre></div></div>

<p>I initially only wrote QuickCheck properties but I wasn&#8217;t satisfied that just passing the properties I&#8217;ve written would guarantee the function is working as expected.</p>
<p>The first property does a simple check that the last item from the windowed function is the same as the last item in the list assuming that the list isn&#8217;t empty, that we&#8217;re using a positive n value and our window size is smaller than the list size. </p>
<p>The second checks that we end up with an empty list if we try to window to a size bigger than the list and the third checks that each of the sub arrays is the correct size.</p>
<p>I wasn&#8217;t sure how to write a QuickCheck property that would test the values of the individual sub arrays so I ended up writing a <a href="http://hunit.sourceforge.net/">HUnit</a> test instead. </p>
<p>If you know how I could achieve the same thing using QuickCheck please let me know.</p>
<p>We can then run all the tests like so:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; ./Windowed                                                                                                                    
windowed:
&nbsp;
  should not window if n is bigger than list size: [OK, passed 100 tests]
&nbsp;
  should create sub arrays the size of window: [OK, passed 100 tests]
&nbsp;
  should group adjacent items into windows: [OK, passed 100 tests]
&nbsp;
  should window a simple list: [OK]
&nbsp;
          Properties  Test Cases  Total      
  Passed  3           1           4          
  Failed  0           0           0</pre></div></div>

<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/WCsYSXZff0w" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2012/05/20/haskell-my-first-attempt-with-quickcheck-and-hunit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2012/05/20/haskell-my-first-attempt-with-quickcheck-and-hunit/</feedburner:origLink></item>
		<item>
		<title>Building an API: Test Harness UI</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/dxSUIzEVvgs/</link>
		<comments>http://www.markhneedham.com/blog/2012/05/19/building-an-api-test-harness-ui/#comments</comments>
		<pubDate>Sat, 19 May 2012 20:03:02 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=4301</guid>
		<description><![CDATA[On the project I&#8217;ve been working on we&#8217;re building an API to be used by other applications in the organisation but when we started none of those applications were ready to integrate with us and therefore drive the API design. Initially we tried driving the API through integration style tests but we realised that taking [...]]]></description>
			<content:encoded><![CDATA[<p>On the project I&#8217;ve been working on we&#8217;re building an API to be used by other applications in the organisation but when we started none of those applications were ready to integrate with us and therefore <a href="http://martinfowler.com/articles/consumerDrivenContracts.html">drive the API design</a>.</p>
<p>Initially we tried driving the API through integration style tests but we realised that taking this approach made it quite difficult for us to imagine how an application would use it. </p>
<p>It also meant that we didn&#8217;t have a good way to show our business stakeholders the work we&#8217;d done &#8211; showing off pieces of JSON going back and forth probably wouldn&#8217;t have gone down too well!</p>
<p>We therefore decided to create our own test harness UI which we would use to show case what we&#8217;d developed so far and also use to help drive the design of the API.</p>
<p>Obviously it would be better if we were able to drive the API out from a real application but given that wasn&#8217;t possible the test harness approach seems to work reasonably well.</p>
<p>The main problem that we ran into was not knowing how much effort we should be putting into the test harness UI given that its primary purpose was to show the progress being made on the UI.</p>
<p>We started out not writing many tests or paying much attention to how it looked since it was assumed to be a throwaway UI.</p>
<p>Over time it&#8217;s become more complicated and since we use it to display progress to management stakeholders we&#8217;ve spent time making it more presentable.</p>
<p>Despite that I&#8217;d still say the effort we&#8217;ve put in is worthwhile because it gives us a way to show progress to non technical people. </p>
<p>From my experience if you can&#8217;t do that they&#8217;ll start to get nervous and wonder if you know what you&#8217;re doing which isn&#8217;t a good place to be!</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/dxSUIzEVvgs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2012/05/19/building-an-api-test-harness-ui/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2012/05/19/building-an-api-test-harness-ui/</feedburner:origLink></item>
		<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>4</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>
	</channel>
</rss>

