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

<channel>
	<title>OJ's rants &#187; Haskell</title>
	<atom:link href="http://buffered.io/category/haskell/feed/" rel="self" type="application/rss+xml" />
	<link>http://buffered.io</link>
	<description>What would OJ do?</description>
	<lastBuildDate>Tue, 06 Jul 2010 20:32:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Haskell 101 at the Brisbane Functional Programming Meetup</title>
		<link>http://buffered.io/2009/08/17/haskell-101-at-the-brisbane-functional-programming-meetup/</link>
		<comments>http://buffered.io/2009/08/17/haskell-101-at-the-brisbane-functional-programming-meetup/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 07:03:36 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://buffered.io/?p=747</guid>
		<description><![CDATA[I just thought I'd let the world know that tomorrow night, at 6pm AEST, the Brisbane Functional Programming Group is having another get-together. The topics for the meeting are: Introduction to Functional Programming - by Brad Clow Haskell 101 - by me! There are still spots available if you're keen to come along. It should [...]]]></description>
			<content:encoded><![CDATA[<p>I just thought I'd let the world know that tomorrow night, at 6pm AEST, the <a href="http://www.meetup.com/Brisbane-Functional-Programming-Group-BFG/" title="Brisbane Functional Programming Group ">Brisbane Functional Programming Group</a> is having another <a href="http://www.meetup.com/Brisbane-Functional-Programming-Group-BFG/calendar/11039537/" title="Intro to FP &#038; Haskell 101">get-together</a>. The topics for the meeting are:</p>
<ul>
<li>Introduction to Functional Programming - by Brad Clow</li>
<li>Haskell 101 - by me!</li>
</ul>
<p>There are still spots available if you're keen to <a href="http://www.meetup.com/Brisbane-Functional-Programming-Group-BFG/calendar/11039537/" title="Intro to FP &#038; Haskell 101">come along</a>. It should be fun!</p>
<p>I'm always up for some public scrutiny, so come along and bring your rotten tomatoes.</p>
<p>If you can't make it, I'll post the slides for the talk on this site, and on Meetup.com, when the event has finished so you can see what was covered. Feel free to ping me or ask questions.</p>
<p><strong>Update 19th Aug '09</strong><br />
The talk seemed to go quite well. I had to motor through the material quite quickly due to the late start, but on the whole I think it was well received. I haven't had any negative feedback so far <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_wink.png' alt=';)' class='wp-smiley' /> </p>
<p>Slides/content are now <a href="http://buffered.io/wp-content/uploads/2009/08/Haskell101-20090818.zip" title="Slides and source for Haskell 101">up for grabs</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2009/08/17/haskell-101-at-the-brisbane-functional-programming-meetup/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>The Value of a Technical Community</title>
		<link>http://buffered.io/2009/07/29/the-value-of-a-technical-community/</link>
		<comments>http://buffered.io/2009/07/29/the-value-of-a-technical-community/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 21:30:29 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[Being in the Industry]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Mercurial]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[anger]]></category>
		<category><![CDATA[community]]></category>
		<category><![CDATA[irc]]></category>
		<category><![CDATA[value]]></category>

		<guid isPermaLink="false">http://buffered.io/?p=737</guid>
		<description><![CDATA[This post was inspired by an experience I had recently in an IRC channel. To protect the innocent, I'll refrain from naming and shaming the channel and individual as those details aren't important. Communities are arguably the life blood of technology. Without a good community the likelihood of the technology catching on is drastically reduced. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://buffered.io/wp-content/uploads/2009/07/community.jpg" title="Cricket Team Holding Hands --- Image by © Royalty-Free/Corbis" rel="lightbox[737]"><img src="http://buffered.io/wp-content/uploads/2009/07/community-150x150.jpg" alt="Cricket Team Holding Hands --- Image by © Royalty-Free/Corbis" title="Cricket Team Holding Hands --- Image by © Royalty-Free/Corbis" width="150" style="float: left; margin-right: 5px; margin-bottom: 5px;" /></a>This post was inspired by an experience I had recently in an IRC channel. To protect the innocent, I'll refrain from naming and shaming the channel and individual as those details aren't important.</p>
<p>Communities are arguably the life blood of technology. Without a good community the likelihood of the technology catching on is drastically reduced. Those communities which lack individuals that are happy to put time and effort in to help get others up to speed tend to stagnate. Any community which involves members that are abrasive, rude and downright offensive is destined to suffer long term. </p>
<p>The level of suffering will vary depending on quite a few things. Those things include existing user base, the age of the technology and the number of different ways that a given <acronym title="New person">n00b</acronym> can get information about that technology.</p>
<p><a href="http://en.wikipedia.org/wiki/Internet_Relay_Chat" title="IRC">IRC</a> is a very common and popular means for members of communities to interact. It provides a real-time element that you don't get with blogs, forums and other media like <a href="Twitter" title="http://twitter.com/">Twitter</a>. It satisfies every geek's desire to get the answer and get it <strong>now</strong>. Experiencing support in real time via IRC can be extremely helpful and rewarding. It can work wonders for the popularity and image of the community and the technology itself.</p>
<p>If you're interested in seeing some active and extremely helpful communities, you should take a look at <a href="irc://chat.freenode.net/haskell" title="#haskell on freenode">#haskell</a> and <a href="irc://chat.freenode.net/mercurial" title="#mercurial on freenode">#mercurial</a>. Both of these channels are generally very active. The people that spend time in them are both helpful and friendly. Both of these channels indicate to me that there is a very healthy and vibrant community behind each of these pieces of technology.</p>
<p><a href="http://buffered.io/wp-content/uploads/2009/07/anger.jpg" title="ARGH!" rel="lightbox[737]"><img src="http://buffered.io/wp-content/uploads/2009/07/anger.jpg" width="200" alt="ARGH!" title="ARGH!" style="float: right; margin-left: 5px; margin-bottom: 5px;"/></a><br />
Unfortunately, this kind of communication also has a downside. The real-time nature of IRC allows people to vent anger on newcomers without any means of moderation. The net result of this is that other individuals become to scared to offer their view or provide support for fear of receiving the same treatment. This goes for members with any level of skill or knowledge. There are some community channels out there containing hundreds of people, and yet there is no conversation. When questions are asked, nobody responds.</p>
<p>After my recent experience in a channel that fist this description, it's easy to see why. In less that half an hour of asking for clarification on certain points, I received a barrage of abuse which, in my view, was <strong>totally unjustified</strong>.</p>
<p>But let's say, for the sake of argument, that it was justified. That for some reason I annoyed someone in such a way that bombarding me with many 4 and 5-letter expletives was acceptable. Do <em>you</em> think that it paints a good picture of the community?</p>
<p><strong>I think <em>not</em>.</strong></p>
<p>In less than half an hour I went from excited to deflated. I lost interest in the piece of technology and lost faith in the community around it. Was my response justified or understandable?</p>
<p>I'm not sure about the answer to this <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> </p>
<p>I feel that it's the job of the community to moderate other members of their community. If people get abusive then other members should step in and prevent it. In my case this wasn't possible due to the fact that the person in question was abusing me via a private chat so that the channel couldn't witness his venting. I'm guessing this wasn't the first time that he's done this to someone, nor will it be the last. I wonder how many others have suffered the same fate? If the silence of the channel is anything to go by, I'm guessing quite a few.</p>
<p>I guess what I'm trying to say here is this: don't underestimate the value and importance of your community. It's one of the most frequented faces of your technology. If it's bitter, unwelcoming and unsupportive, you will not only lose valuable newcomers who come looking for information and support, you will also lose future users due to the poor reputation. Ultimately, your tech will suffer.</p>
<p>If you're part of a community, do your bit to police it. Help those who need help. Stop those people who are abusive.</p>
<p>.. and if you don't have anything nice to say, then <em>don't say anything at all</em>!</p>
<p>
<!-- Begin Google Adsense code -->
<p><center>
<script type="text/javascript"><!--
google_ad_client = "pub-5662448744756930";
/* 468x60, created 2/11/09 */
google_ad_slot = "8047929723";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></center></p>
<!-- End Google Adsense code -->
</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2009/07/29/the-value-of-a-technical-community/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Point-Free style: What is it good for?</title>
		<link>http://buffered.io/2009/06/27/point-free-style-what-is-it-good-for/</link>
		<comments>http://buffered.io/2009/06/27/point-free-style-what-is-it-good-for/#comments</comments>
		<pubDate>Sat, 27 Jun 2009 11:49:43 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Point Free]]></category>
		<category><![CDATA[style]]></category>

		<guid isPermaLink="false">http://buffered.io/?p=724</guid>
		<description><![CDATA[If you're not interested in what inspired this post, then skip this section and jump to the more interesting bits. A little bit of history... Recently I've been delving into Haskell quite a bit. It's part of my apparently never-ending quest to learn as much as I can about as many languages as I can [...]]]></description>
			<content:encoded><![CDATA[<p>If you're not interested in what inspired this post, then skip this section and <a href="#MoreInteresting" title="The purpose of the post">jump</a> to the more interesting bits.</p>
<h3>A little bit of history...</h3>
<p>Recently I've been delving into <a href="http://haskell.org/" title="Haskell">Haskell</a> quite a bit. It's part of my apparently never-ending quest to learn as much as I can about as many languages as I can (well, those that appeal to me at least :)). While I love playing around with a language, toying with ideas, writing small programs, reading books, blog posts, etc it's not really the same as having an on-call expert to help and guide you. </p>
<p> While I'm aware of, and frequently visit, the <a href=http://www.haskell.org/haskellwiki/IRC_channel" title="Haskell IRC">Haskell IRC channell</a> I find that the level of understanding there is so great that my piddly <a href="http://en.wikipedia.org/wiki/Newbie" title="Newbie">noob</a> questions tend to get lost amongst the bombardment of much more advanced &#038; interesting discussions. In short, while I find it a great place to go, it's not a great place to find someone who's happy to help guide me through the maze and go over topics in quite a bit of detail while I annoy them with questions.</p>
<p>Haskell is one of those languages where having a mentor is really beneficial. So I set about finding myself one. Thankfully, I found a rather helpful chap based in the UK, <del>(he shall remain anonymous, as I don't want to violate the man's privacy)</del>, <a href="http://twitter.com/peter_marks" title="Peter Marks">Peter Marks</a>, who has been very forthcoming with information. He's humoured me and been incredibly patient so far, and I'm very grateful for his time.</p>
<p>One of the questions that I asked him was:<br />
<blockquote cite="OJ">
<p>Why is it that everyone seems to strive to get their code into <a href="http://www.haskell.org/haskellwiki/Pointfree" title="Point Free">Point-Free</a> style? I can see how a lot of the implementations are more concise, but many of those lose readability.</p>
<p>What is so special about it?</p>
</blockquote>
<p>The discussion that followed was really insightful. That is what has inspired me to write this post.</p>
<p>Please note, any errors in this post are totally my own. They are not the fault of my mentor <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> </p>
<h3 id="MoreInteresting">The purpose of the post: Why aim for Point-Free style?</h3>
<p>So it is just me, or does anyone else out there feel that there's a bit of a "thing" going on for Point-Free style? Sometimes I share my terrible code with people and I get shunned when it's not Point-Free and it could be.</p>
<p>Anyone?</p>
<p>I hope it's not just me <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> Let's start with taking a quick look at what Point-Free style actually is.</p>
<h4>So what is Point-Free style?</h4>
<p>If we take a look at the <a href=http://en.wikipedia.org/wiki/Point-free_programming" title="Wikipedia">Wikipedia</a> we can see that..<br />
<blockquote>
<p>Tacit [point-free] programming is a programming paradigm in which a function definition does not include information regarding its arguments, using combinators and function composition (but not ?-abstraction) instead of variables.</p>
</blockquote>
<p>Simple eh? <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> In essence, it basically means that your function definition doesn't reference any of its arguments/variables. For a crass definition, think <em>point == argument</em> and it should make sense.</p>
<p>So we're writing functions without directly referencing its arguments. For those not familiar with Haskell or other languages that support this, let's take a look at an example:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">-- sum : take a list of numbers and add them all up to get a total</span>
<span style="color: #5d478b; font-style: italic;">-- start with the base-case: an empty list</span>
<span style="font-weight: bold;">sum</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">0</span>
<span style="font-weight: bold;">sum</span> <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> x <span style="color: #339933; font-weight: bold;">+</span> <span style="font-weight: bold;">sum</span> xs</pre></div></div>

<p>We can see that the above function is written in such a way that the arguments passed into the function are actually referenced in the body of the code. This is how a standard imperative programmer would write this function if he/she was new to Haskell. If we instead used a <a href="http://www.haskell.org/haskellwiki/Fold" title="Fold">fold</a>, we could define it like so:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="font-weight: bold;">sum</span> xs <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">foldr</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">+</span><span style="color: green;">&#41;</span> <span style="color: red;">0</span> xs</pre></div></div>

<p>This does exactly the same thing as the previous definition, but as you can see the grunt work is done by the fold function. Now that we have this definition, we can easily move to Point-Free style:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="font-weight: bold;">sum</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">foldr</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">+</span><span style="color: green;">&#41;</span> <span style="color: red;">0</span></pre></div></div>

<p>Here we can see that no reference is made to the arguments of the function. Since we haven't referenced any "points", we have a Point-Free implementation.</p>
<p>Awesome. Cool. Sweet. Nifty.</p>
<p>But what does it give me? Why is it better?</p>
<h4>Why use Point-Free style?</h4>
<p>This section is based totally on my own opinion and is not an official definition <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> I think that Point-Free style fits in the same category as many other coding patterns and styles, and that it's usually down to the individual programmer to determine the when and the why. So take my view with a pinch of salt.</p>
<p>In short, Point-Free style let's you <em>focus on the <strong>how</strong> rather than the <strong>what</strong></em>.</p>
<p>It might be just me, but imperative programs seem to have a large focus on the data. The code which operates on the data is lost within a plethora of code that isn't necessarily specific to what the program needs to do. I've found that functional programming tends to be very different, at least in Haskell. Haskell lets me focus on what it is I need to do, and I feel that Point-Free is another step in the same direction. Is this good? I think so <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> But I'll let you decide.</p>
<p>As well as the focus on the <strong>what</strong> I've found that aiming for a Point-Free solution can aid in helping you understand your problem better. This claim sounds like fluff, so let's go through an example and hopefully you'll see what I mean.</p>
<h4>Where Point-Free helped me get a better understanding</h4>
<p>Haskell developers use the <a href="http://www.haskell.org/haskellwiki/Function_composition" title="Function composition">composition operator</a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/src/GHC-Base.html#." title="(.) implementation in Prelude">(.)</a> a lot. It actually aids in creating Point-Free style. I love the irony here. We add points (.) to remove points (arguments).</p>
<p>Anyway, the composition operator's definition, according to <a href="http://www.haskell.org/onlinereport/standard-prelude.html" title="Prelude">Prelude</a> is:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: green;">&#40;</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="color: green;">&#40;</span>b <span style="color: #339933; font-weight: bold;">-&gt;</span> c<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>a <span style="color: #339933; font-weight: bold;">-&gt;</span> b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> a <span style="color: #339933; font-weight: bold;">-&gt;</span> c
<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">.</span><span style="color: green;">&#41;</span> f g x <span style="color: #339933; font-weight: bold;">=</span> f <span style="color: green;">&#40;</span>g x<span style="color: green;">&#41;</span></pre></div></div>

<p>This operator takes two functions and produces a function which is composed of the two. This function takes the output of one function and sends it through as the input to another function, returning the result of that call.</p>
<p>This is really handy. We can do some interesting things like:</p>
<pre>
ghci> let doubleAndAdd5 = (+5) . (*2)
ghci> doubleAndAdd5 20
45
ghci> :m +Data.List
ghci> let sumColumns = map sum . transpose
ghci> sumColumns [[1,2,3],[4,5,6],[7,8,9]]
[12,15,18]
</pre>
<p>Very handy indeed. While handy, it doesn't necessarily allow us to do everything we might want to do. One example is handling cases where we want to compose a function where the right-hand function takes two arguments instead of one.</p>
<p>So if we wanted to create a function that would take two values, add them together and double the result, all while using Point-Free style, we'd like to do something like this:</p>
<pre>
ghci> let addAndDouble = (*2) . (+)

<interactive>:1:20:
    No instance for (Num (a -> a))
      arising from a use of `*' at <interactive>:1:20-21
    Possible fix: add an instance declaration for (Num (a -> a))
    In the first argument of `(.)', namely `(* 2)'
    In the expression: (* 2) . (+)
    In the definition of `addAndDouble': addAndDouble = (* 2) . (+)
ghci>
</pre>
<p>As you can see, ghci doesn't like it. And rightly so! This is because the composition operator's signature is:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: green;">&#40;</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="color: green;">&#40;</span>b <span style="color: #339933; font-weight: bold;">-&gt;</span> c<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>a <span style="color: #339933; font-weight: bold;">-&gt;</span> b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> a <span style="color: #339933; font-weight: bold;">-&gt;</span> c</pre></div></div>

<p>That is:</p>
<ol>
<li>It takes an argument which is a function which takes a value of type <em>b</em> which returns a value of type <em>c</em></li>
<li>It takes a function which takes a value of type <em>a</em> which returns returns a value of type <em>b</em></li>
<li>It returns a new function which takes a value of type <em>a</em> and returns a value of type <em>c</em></li>
</ol>
<p>This doesn't work in our case, as we want our right-hand function to take two arguments. That is, we want a type signature that looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">foo <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span>b <span style="color: #339933; font-weight: bold;">-&gt;</span> c<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>a <span style="color: #339933; font-weight: bold;">-&gt;</span> a1 <span style="color: #339933; font-weight: bold;">-&gt;</span> b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> a <span style="color: #339933; font-weight: bold;">-&gt;</span> a1 <span style="color: #339933; font-weight: bold;">-&gt;</span> c</pre></div></div>

<p>So everywhere we had <em>(a -> b)</em>, what we really want is <em>(a -> a1 -> b)</em>. Given that we don't have an operator that does that for us, let's define one. We'll start with the definition of the (.) operator and work towards a function that does what we need.</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">-- here is the composition operator again</span>
<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">.</span><span style="color: green;">&#41;</span> f g x <span style="color: #339933; font-weight: bold;">=</span> f <span style="color: green;">&#40;</span>g x<span style="color: green;">&#41;</span>
<span style="color: #5d478b; font-style: italic;">-- here's our new operator's definition</span>
<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">.^</span><span style="color: green;">&#41;</span> f g x y <span style="color: #339933; font-weight: bold;">=</span> f <span style="color: green;">&#40;</span>g x y<span style="color: green;">&#41;</span></pre></div></div>

<p>Simple! Let's see what ghci says about it:</p>
<pre>
ghci> let (.^) f g x y = f (g x y)
ghci> :t (.^)
a :: (t2 -> t3) -> (t -> t1 -> t2) -> t -> t1 -> t3
</pre>
<p>Looks good! This is exactly what we need. Now that we have a working function, let's aim to write this in Point-Free style. We do this by breaking down the function slowly and eliminating arguments by moving them to the far right hand side of the function definition.</p>
<p>The first argument to get rid of is 'y', as this is the last argument passed in:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">-- start by moving y to the right</span>
<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">.^</span><span style="color: green;">&#41;</span> f g x y <span style="color: #339933; font-weight: bold;">=</span> f <span style="color: green;">&#40;</span>g x y<span style="color: green;">&#41;</span>
<span style="color: #5d478b; font-style: italic;">-- becomes</span>
<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">.^</span><span style="color: green;">&#41;</span> f g x y <span style="color: #339933; font-weight: bold;">=</span> f <span style="color: #339933; font-weight: bold;">.</span> <span style="color: green;">&#40;</span>g x<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$</span> y</pre></div></div>

<p>These are functionally equivalent, and now that 'y' is out on it's own, we can drop it from our function definition:</p>
<pre>ghci> let (.^) f g x = f . (g x)
ghci> :t (.^)
(.^) :: (b -> c) -> (t -> a -> b) -> t -> a -> c
ghci>
</pre>
<p>Excellent, we're a step closer. Now we need to do the same with 'x'. This takes a little more fiddling:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">-- we need to take the original definition</span>
<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">.^</span><span style="color: green;">&#41;</span> f g x <span style="color: #339933; font-weight: bold;">=</span> f <span style="color: #339933; font-weight: bold;">.</span> <span style="color: green;">&#40;</span>g x<span style="color: green;">&#41;</span>
<span style="color: #5d478b; font-style: italic;">-- and change it so that it uses prefix notation instead of infix</span>
<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">.^</span><span style="color: green;">&#41;</span> f g x <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: green;">&#41;</span> f <span style="color: green;">&#40;</span>g x<span style="color: green;">&#41;</span>
<span style="color: #5d478b; font-style: italic;">-- which is the same as</span>
<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">.^</span><span style="color: green;">&#41;</span> f g x <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">.</span><span style="color: green;">&#41;</span> f<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>g x<span style="color: green;">&#41;</span>
<span style="color: #5d478b; font-style: italic;">-- what we're doing is calling g with x and applying the result to f</span>
<span style="color: #5d478b; font-style: italic;">-- and hence we can compose the composition of f with the call to g</span>
<span style="color: #5d478b; font-style: italic;">-- giving us the following</span>
<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">.^</span><span style="color: green;">&#41;</span> f g x <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">.</span><span style="color: green;">&#41;</span> f<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">.</span> g <span style="color: #339933; font-weight: bold;">$</span> x
<span style="color: #5d478b; font-style: italic;">-- finally leaving us with</span>
<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">.^</span><span style="color: green;">&#41;</span> f g <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">.</span><span style="color: green;">&#41;</span> f<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">.</span> g</pre></div></div>

<p>Phew <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> I hope you can see the progression. We've managed to move x out of the picture, so now we're down to a fairly crazy looking definition. Let's see what ghci has to say:</p>
<pre>ghci> let (.^) f g = ((.) f) . g
ghci> :t (.^)
(.^) :: (b -> c) -> (a1 -> a -> b) -> a1 -> a -> c</pre>
<p>So we've got rid of two variables, but there are still two more to go! Remember, 'f' and 'g' are still variables, they just carry functions. So let's get rid of 'g':</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">-- start with what we had before</span>
<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">.^</span><span style="color: green;">&#41;</span> f g <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">.</span><span style="color: green;">&#41;</span> f<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">.</span> g
<span style="color: #5d478b; font-style: italic;">-- change to prefix notation again</span>
<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">.^</span><span style="color: green;">&#41;</span> f g <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: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">.</span><span style="color: green;">&#41;</span> f<span style="color: green;">&#41;</span> g
<span style="color: #5d478b; font-style: italic;">-- and drop g</span>
<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">.^</span><span style="color: green;">&#41;</span> f <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: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">.</span><span style="color: green;">&#41;</span> f<span style="color: green;">&#41;</span></pre></div></div>

<p>This is looking rather crazy <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> Again, let's make sure we haven't done anything stupid:</p>
<pre>ghci> let (.^) f = (.) ((.) f)
ghci> :t (.^)
(.^) :: (b -> c) -> (a1 -> a -> b) -> a1 -> a -> c</pre>
<p>Excellent. We've still got what we need. One more point needs to be dropped, so let's get rid of 'f':</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">-- start with what we had before</span>
<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">.^</span><span style="color: green;">&#41;</span> f <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: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">.</span><span style="color: green;">&#41;</span> f<span style="color: green;">&#41;</span>
<span style="color: #5d478b; font-style: italic;">-- which means that we're composing a composition with a</span>
<span style="color: #5d478b; font-style: italic;">-- function composed of f and something else</span>
<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">.^</span><span style="color: green;">&#41;</span> f <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: 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: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$</span> f
<span style="color: #5d478b; font-style: italic;">-- and we finally drop f</span>
<span style="color: green;">&#40;</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="color: green;">&#40;</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="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">.</span><span style="color: green;">&#41;</span></pre></div></div>

<p>Isn't that just crazy! Let's again check ghci:</p>
<pre>ghci> let (.^) = (.) . (.)
ghci> :t (.^)
(.^) :: (b -> c) -> (a1 -> a -> b) -> a1 -> a -> c</pre>
<p>So there we have it, our operator completely in Point-Free style.</p>
<p>This is where the penny dropped for me. The whole exercise of moving through to Point-Free made me really understand what it was I was doing in the first place. The final definition makes it very clear. We're composing two separate compositions.<br />
Let's see if our function behaves itself using the example we listed above.</p>
<pre>
ghci> let addAndDouble = (*2) .^ (+)
ghci> :t addAndDouble
addAndDouble :: Integer -> Integer -> Integer
ghci> addAndDouble 10 15
50
ghci> addAndDouble 21 3
48
</pre>
<p>It does exactly what we need it too.</p>
<h3>Conclusion</h3>
<p>To sum up, Point-Free helps you tidy your code into more concise implementations which tend to aid you in understanding what it is you are trying to do. I feel it really helps you focus on what you're doing as opposed to what you're doing it to. It's down to you to determine whether you feel this is a good thing or not!</p>
<p>It is ultimately down to the developer to dictate when it should be used. There are definitely cases where the resulting function might not actually help in making things clearer. But on the whole, Point-Free style seems to help me understand what it is I'm doing (or, arguably, not doing).</p>
<p>Of course, you could just get sick and tired of trying to come up with variable names, in which case Point-Free is the bomb <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> </p>
<p>Feedback of any kind is always welcome. Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2009/06/27/point-free-style-what-is-it-good-for/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>Data Crunching in Haskell</title>
		<link>http://buffered.io/2009/06/25/data-crunching-in-haskell/</link>
		<comments>http://buffered.io/2009/06/25/data-crunching-in-haskell/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 14:00:44 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://buffered.io/?p=719</guid>
		<description><![CDATA[A few days ago I was having a chat to a friend of mine about a little data parsing problem. He had the need to parse a multi-dimensional array to pull out some values. That array was guaranteed to be square, but not necessarily in contiguous memory. He needed to parse each "column" of the [...]]]></description>
			<content:encoded><![CDATA[<p>A few days ago I was having a chat to a <a href="http://shiftperception.com/blog" title="Shifty">friend of mine</a> about a little data parsing problem. He had the need to parse a multi-dimensional array to pull out some values. That array was guaranteed to be square, but not necessarily in contiguous memory. He needed to parse each "column" of the array, calculate a total, and then determine the biggest and smallest of those totals.</p>
<p>A sample of the data might look something like this:</p>
<pre>
data = ({150,200,45,57,95,2,45,32,15,10,5,2,2,4},
         12,20,45,37,10,5,2,2,10,95,2,45,32,7},
         32,15,10,5,2,23,24,15,20,45,57,95,0,45})
</pre>
<p>So the first step would be to add 150, 12 and 32 and store the value. Then 200, 20 and 15, and store the value. Do this for all of the columns, then get a maximum and a minimum. </p>
<p>This little algorithm was going to be part of his project, and hence needed to be implemented in <a href="http://www.adobe.com/devnet/actionscript/articles/actionscript3_overview.html" title="ActionScript 3.0">AS3</a>. So I picked his brains about the AS3 syntax, because I have absolutely no clue given that I've never worked with any version of ActionScript in the past.</p>
<p>Together, we came up with the following solution:</p>
<pre>var columnTotal:Number;
var biggest:Number;
var smallest:Number;
biggest = smallest = sum(vData, 0);

for(var i = 1; i < _scope.period_mcs.length; ++i)
{
  columnTotal = sum(vData, i);
  biggest = Math.max(biggest, columnTotal);
  smallest = Math.min(smallest, columnTotal);
}

// helper function
function sum(var data:Array, var index:Integer):Number
{
  var total:Number = 0;
  for(int i = 0; i < data.length; ++i)
  {
    total += data[i][index];
  }
  return total;
}</pre>
<p>Does it work? Yes, it sure does. Is it optimal? Yes, and no <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> I was lazy and used the Math.max and Math.min functions instead of doing the obvious...</p>
<pre>
if(biggest < columnTotal) biggest = columnTotal;
</pre>
<p>So if we did that, to reduce the need for function calls and unnecessary assignments, we end up with this:</p>
<pre>var columnTotal:Number;
var biggest:Number;
var smallest:Number;
biggest = smallest = sum(vData, 0);

for(var i = 1; i < _scope.period_mcs.length; ++i)
{
  columnTotal = sum(vData, i);
  if(biggest < columnTotal)
  {
    biggest = columnTotal;
  }
  else if(smallest > columnTotal)
  {
    smallest = columnTotal;
  }
}

// helper function
function sum(var data:Array, var index:Integer):Number
{
  var total:Number = 0;
  for(int i = 0; i < data.length; ++i)
  {
    total += data[i][index];
  }
  return total;
}</pre>
<p>I can't see many ways to improve on this without going overboard with optimisation. Any AS3 guru's are more than welcome to prove me wrong!</p>
<p>So after thinking about this in an imperative language, I couldn't help but have a look at what the functional version might look like. Of course, my current chosen Functional toy is <a href="http://haskell.org/" title="Haskell">Haskell</a> and so I fired up <a href="http://vim.org/" title="VIM">VIM</a> and <a href="http://www.haskell.org/ghc/" title="GHC">GHCI</a> and had a bit of a play.</p>
<p>First, I put the data into Haskell's list format:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">vals <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span><span style="color: red;">150</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">200</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">45</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">57</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">95</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;">45</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">32</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">15</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">10</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">5</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;">2</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;">12</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">20</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">45</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">37</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">10</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">5</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;">2</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">10</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">95</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;">45</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">32</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;">32</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">15</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">10</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">5</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;">23</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">24</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">15</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">20</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">45</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">57</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">95</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">45</span><span style="color: green;">&#93;</span><span style="color: green;">&#93;</span></pre></div></div>

<p>That was easy enough. The next step was to break the problem down so that I could use some of the built in functions of Haskell's <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html" title="Prelude">Prelude</a> libraries. My thought processes were:</p>
<ol>
<li>Summing the column of the array could be done using <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sum" title="sum">sum</a>, but I'd need to change the list so that rows become columns, and vice-versa.</li>
<li>To switch rows and columns, I could use the <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html#v:transpose" title="Data.List.transpose">transpose</a> function.</li>
<li>Then all I'd need to do is use <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:maximum" title="maximum">maximum</a> and <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:minimum" title="minimum">minimum</a> to get the right values out.</li>
</ol>
<p>Being a bit of a primitive Haskeller, my first pass was something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">extremes n <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>b<span style="color: #339933; font-weight: bold;">,</span> s<span style="color: green;">&#41;</span>
  <span style="color: #06c; font-weight: bold;">where</span>
    l <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">map</span> <span style="font-weight: bold;">sum</span> <span style="color: #339933; font-weight: bold;">$</span> transpose n
    b <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">maximum</span> l
    s <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">minimum</span> l</pre></div></div>

<p>The function <em>extremes</em> is the function which takes the data (list of lists) and spits out a tuple of <em>(max, min)</em>.</p>
<p>Before the end of the conversation with my designer colleague in arms, I pinged him my version of the solution in Haskell and needless to say he was a little surprised as how concise it was. I was sure to point out that there is no doubt a better way of representing this solution with regards to speed and conciseness.</p>
<p>The first thing that I thought could be improved would be using a custom <a href="http://www.haskell.org/haskellwiki/Fold" title="Fold">fold</a> to get the max and min while parsing the transposed list. This would allow us to calculate the values in a single pass and hence be a little better with regards to performance. That would obviously sacrifice a little bit of the conciseness we're looking for.</p>
<p>When the conversation ended, I jumped onto IRC and spoke to some more seasoned Haskellers. The first suggested improvement that popped out of that chat was to use <a href="http://www.haskell.org/arrows/" title="Arrows">arrows</a> to remove the need for the where clause. That solution looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">extremes <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">maximum</span> <span style="color: #339933; font-weight: bold;">&amp;&amp;</span>&amp; <span style="font-weight: bold;">minimum</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">map</span> <span style="font-weight: bold;">sum</span> <span style="color: #339933; font-weight: bold;">.</span> transpose</pre></div></div>

<p>Nifty <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> Of all the other options, this proved to be the most readable and concise, though not the best performing.</p>
<p>The next most notable solution included the fold which calculated the min and max in a single parse, not a double parse:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">extremes <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">foldl1</span> <span style="color: green;">&#40;</span>\<span style="color: green;">&#40;</span>a<span style="color: #339933; font-weight: bold;">,</span> b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="font-weight: bold;">max</span> a <span style="color: #339933; font-weight: bold;">***</span> <span style="font-weight: bold;">min</span> b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">.</span> join <span style="font-weight: bold;">zip</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">map</span> <span style="font-weight: bold;">sum</span> <span style="color: #339933; font-weight: bold;">.</span> transpose</pre></div></div>

<p>Folds really are fantastic aren't they. Again we're using arrows here to do a bit of heavy lifting and that keeps things looking a little nicer.</p>
<p>So after this little session, my designer friend was aware of how easy it can be to crunch certain types of data using a functional language, like Haskell. It made me think again about how it'd be nice to just be able to plug in whichever language we wanted whenever we felt it would do the job better than whatever the current tool is.</p>
<p>So how would you improve it? <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> </p>
<p>
<!-- Begin Google Adsense code -->
<p><center>
<script type="text/javascript"><!--
google_ad_client = "pub-5662448744756930";
/* 468x60, created 2/11/09 */
google_ad_slot = "8047929723";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></center></p>
<!-- End Google Adsense code -->
</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2009/06/25/data-crunching-in-haskell/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Project Euler #10</title>
		<link>http://buffered.io/2008/07/30/project-euler-10/</link>
		<comments>http://buffered.io/2008/07/30/project-euler-10/#comments</comments>
		<pubDate>Wed, 30 Jul 2008 11:27:31 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://buffered.io/?p=439</guid>
		<description><![CDATA[WARNING! This post contains a spoiler for Problem #10 listed at Project Euler. Do not read the rest of this post if you're planning to attempt to solve the problem yourself. Problem #10 takes us back into the realm of the relatively boring - prime numbers (again). The question is as follows: The sum of [...]]]></description>
			<content:encoded><![CDATA[<p><strong>WARNING!</strong> This post contains a spoiler for Problem #10 listed at <a href="http://projecteuler.net/" title="Project Euler">Project Euler</a>. Do not read the rest of this post if you're planning to attempt to solve the problem yourself. </p>
<p><a title="Project Euler #10" href="http://projecteuler.net/index.php?section=problems&amp;id=10">Problem #10</a> takes us back into the realm of the relatively boring - prime numbers (again). The question is as follows:<br />
<blockquote>
<p>The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.</p>
<p>Find the sum of all the primes below two million.</p>
</blockquote>
<p>Exciting stuff! Since we've already got ourselves a relatively nifty prime number generator from a <a title="Project Euler #7" href="http://buffered.io/2008/06/15/project-euler-7/">previous post</a>, we can simply reuse this, along with the sum function that comes in <a title="Prelude" href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html">Prelude</a>, to generate the answer.</p>
<p>Here is what the code looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">merge <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: green;">&#41;</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>a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span>
merge xs<span style="color: #339933; font-weight: bold;">@</span><span style="color: green;">&#40;</span>x:xt<span style="color: green;">&#41;</span> ys<span style="color: #339933; font-weight: bold;">@</span><span style="color: green;">&#40;</span>y:yt<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span>
  <span style="color: #06c; font-weight: bold;">case</span> <span style="font-weight: bold;">compare</span> x y <span style="color: #06c; font-weight: bold;">of</span>
    LT <span style="color: #339933; font-weight: bold;">-&gt;</span> x : <span style="color: green;">&#40;</span>merge xt ys<span style="color: green;">&#41;</span>
    EQ <span style="color: #339933; font-weight: bold;">-&gt;</span> x : <span style="color: green;">&#40;</span>merge xt yt<span style="color: green;">&#41;</span>
    GT <span style="color: #339933; font-weight: bold;">-&gt;</span> y : <span style="color: green;">&#40;</span>merge xs yt<span style="color: green;">&#41;</span>
&nbsp;
diff <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: green;">&#41;</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>a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span>
diff xs<span style="color: #339933; font-weight: bold;">@</span><span style="color: green;">&#40;</span>x:xt<span style="color: green;">&#41;</span> ys<span style="color: #339933; font-weight: bold;">@</span><span style="color: green;">&#40;</span>y:yt<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span>
  <span style="color: #06c; font-weight: bold;">case</span> <span style="font-weight: bold;">compare</span> x y <span style="color: #06c; font-weight: bold;">of</span>
    LT <span style="color: #339933; font-weight: bold;">-&gt;</span> x : <span style="color: green;">&#40;</span>diff xt ys<span style="color: green;">&#41;</span>
    EQ <span style="color: #339933; font-weight: bold;">-&gt;</span> diff xt yt
    GT <span style="color: #339933; font-weight: bold;">-&gt;</span> diff xs yt
&nbsp;
primes<span style="color: #339933; font-weight: bold;">,</span> nonprimes <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>
primes    <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: #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;">&#40;</span>diff <span style="color: green;">&#91;</span><span style="color: red;">7</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">9</span> <span style="color: #339933; font-weight: bold;">..</span><span style="color: green;">&#93;</span> nonprimes<span style="color: green;">&#41;</span>
nonprimes <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">foldr1</span> f <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">map</span> g <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">tail</span> primes
  <span style="color: #06c; font-weight: bold;">where</span>
    f <span style="color: green;">&#40;</span>x:xt<span style="color: green;">&#41;</span> ys <span style="color: #339933; font-weight: bold;">=</span> x : <span style="color: green;">&#40;</span>merge xt ys<span style="color: green;">&#41;</span>
    g p         <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span> n <span style="color: #339933; font-weight: bold;">*</span> p <span style="color: #339933; font-weight: bold;">|</span> n <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="color: green;">&#91;</span>p<span style="color: #339933; font-weight: bold;">,</span> p <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: green;">&#93;</span><span style="color: green;">&#93;</span>
&nbsp;
main <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
main <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">print</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">sum</span> <span style="color: green;">&#40;</span>takeWhile <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">&lt;</span><span style="color: red;">2000000</span><span style="color: green;">&#41;</span> primes<span style="color: green;">&#41;</span></pre></div></div>

<p>I don't really have much else to say about this. I won't bother with the performance information because it's not really doing anything too exciting that hasn't already been discussed.</p>
<p>Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2008/07/30/project-euler-10/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>A Better &#8216;nub&#8217;</title>
		<link>http://buffered.io/2008/07/28/a-better-nub/</link>
		<comments>http://buffered.io/2008/07/28/a-better-nub/#comments</comments>
		<pubDate>Mon, 28 Jul 2008 07:10:18 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://buffered.io/?p=416</guid>
		<description><![CDATA[During my Haskell travels I have found myself using the nub function quite regularly. For those too lazy to click the link: nub removes duplicates from a list of items. eg: Prelude&#62; nub &#91;1,1,3,3,5,5,6,6,6,1&#93; &#91;1,3,5,6&#93; Fairly simple stuff. Until recently I hadn't bothered pondering the internal implementation of this function because I hadn't really been [...]]]></description>
			<content:encoded><![CDATA[<p>During my Haskell travels I have found myself using the <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html#v%3Anub" title="Data.List nub">nub</a> function quite regularly. For those too lazy to click the link: <em>nub</em> removes duplicates from a list of items. eg:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">Prelude<span style="color: #339933; font-weight: bold;">&gt;</span> nub <span style="color: green;">&#91;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">5</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">5</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">6</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">6</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">6</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">1</span><span style="color: green;">&#93;</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;">3</span><span style="color: #339933; font-weight: bold;">,</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></pre></div></div>

<p>Fairly simple stuff. Until recently I hadn't bothered pondering the internal implementation of this function because I hadn't really been too worried about performance. That's no longer the case. I recently cracked open the hood of <em>nub</em>, and was rather surprised by what I saw. </p>
<p>Since the source of <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html" title="Prelude">Prelude</a> is in the public domain, I'm safe to show you the content:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">nub                     <span style="color: #339933; font-weight: bold;">::</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: 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>a<span style="color: green;">&#93;</span>
nub                      <span style="color: #339933; font-weight: bold;">=</span> nubBy <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">==</span><span style="color: green;">&#41;</span>
&nbsp;
nubBy                   <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span>a <span style="color: #339933; font-weight: bold;">-&gt;</span> a <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Bool</span><span style="color: green;">&#41;</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>a<span style="color: green;">&#93;</span>
nubBy eq <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>
nubBy eq <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span>          <span style="color: #339933; font-weight: bold;">=</span> x : nubBy eq <span style="color: green;">&#40;</span><span style="font-weight: bold;">filter</span> <span style="color: green;">&#40;</span>\y <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="font-weight: bold;">not</span> <span style="color: green;">&#40;</span>eq x y<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> xs<span style="color: green;">&#41;</span></pre></div></div>

<p>So <em>nub</em> uses <em>nubBy</em> and passes <em>(==)</em> as the filter for the items. This is all well and good, but the result is an O(n<sup>2</sup>) level of complexity! In this day and age that's pretty poor for a simple filtering mechanism, especially when we have more powerful mechanisms built-in to most libraries.</p>
<p>Since seeing this I had added a "write my own nub" task to my TODO list, but I haven't yet got round to it. So you can imagine my delight when I found <a href="http://www.hvergi.net/2008/07/playing-with-haskells-lazy-lists/#comment-50" title="Playing with Haskell's lazy lists - Comment by Jedai">this comment</a> by Jedai over at <a href="http://www.hvergi.net/" title="hvergi.net">hvergi.net</a>. He'd pointed out that <em>nub</em> was indeed inefficient, and showed an alternative implementation:</p>
<blockquote cite="Jedai"><p>Note that nub only demands that the list element be part of the Eq typeclass. As a result it is very inefficient and a better solution must always be prefered whenever the nature of the elements allows it.</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">import</span> <span style="color: #06c; font-weight: bold;">qualified</span> Data<span style="color: #339933; font-weight: bold;">.</span>Set <span style="color: #06c; font-weight: bold;">as</span> S
nub' <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: green;">&#41;</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>a<span style="color: green;">&#93;</span>
nub' <span style="color: #339933; font-weight: bold;">=</span> go S<span style="color: #339933; font-weight: bold;">.</span>empty
  <span style="color: #06c; font-weight: bold;">where</span> go <span style="color: #339933; font-weight: bold;">_</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
        go s <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">|</span> S<span style="color: #339933; font-weight: bold;">.</span>member x s <span style="color: #339933; font-weight: bold;">=</span> go s xs
                    <span style="color: #339933; font-weight: bold;">|</span> <span style="font-weight: bold;">otherwise</span>    <span style="color: #339933; font-weight: bold;">=</span> x : go <span style="color: green;">&#40;</span>S<span style="color: #339933; font-weight: bold;">.</span>insert x s<span style="color: green;">&#41;</span> xs</pre></div></div>

<p>La différence de performance est énorme.</p>
</blockquote>
<p>You got that right! Thanks to Jedai (and Arnar via hvergi.net) I now have a much faster <em>nub</em> implementation. Cheers guys!</p>
<p>It just goes to show that even these days you can't always trust the libraries. There is no harm is taking a look under the hood to see what's going on! You don't have to reinvent the wheel, but there's nothing wrong with making the wheel smoother <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2008/07/28/a-better-nub/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Project Euler #9</title>
		<link>http://buffered.io/2008/07/25/project-euler-9/</link>
		<comments>http://buffered.io/2008/07/25/project-euler-9/#comments</comments>
		<pubDate>Fri, 25 Jul 2008 12:08:57 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://buffered.io/?p=410</guid>
		<description><![CDATA[WARNING! This post contains a spoiler for Problem #9 listed at Project Euler. Do not read the rest of this post if you're planning to attempt to solve the problem yourself. Problem #9 takes a turn back into the math theory and forces you to think about Pythagorean triplets. It goes as follows: A Pythagorean [...]]]></description>
			<content:encoded><![CDATA[<p><strong>WARNING!</strong> This post contains a spoiler for Problem #9 listed at <a href="http://projecteuler.net/" title="Project Euler">Project Euler</a>. Do not read the rest of this post if you're planning to attempt to solve the problem yourself. </p>
<p><a href="http://projecteuler.net/index.php?section=problems&#038;id=9">Problem #9</a> takes a turn back into the math theory and forces you to think about Pythagorean triplets. It goes as follows:<br />
<blockquote>
<p>
A Pythagorean triplet is a set of three natural numbers, a  b  c, for which,<br />
&nbsp;&nbsp;&nbsp;&nbsp;a<sup>2</sup> + b<sup>2</sup> = c<sup>2</sup></p>
<p>For example, 3<sup>2</sup> + 4<sup>2</sup> = 9 + 16 = 25 = 5<sup>2</sup>.</p>
<p>There exists exactly one Pythagorean triplet for which a + b + c = 1000.<br />
Find the product abc.
</p>
</blockquote>
<p>The first solution to this problem that I came up with was just atrocious <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> It was long, bloated and extremely messy. But it worked. If you really want to see it then ask me nicely and I'll post it.</p>
<p>For now I'm going to show you my second attempt, which is a little nicer on the eyes.</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">main <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
main <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">print</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">head</span> <span style="color: green;">&#91;</span> a <span style="color: #339933; font-weight: bold;">*</span> b <span style="color: #339933; font-weight: bold;">*</span> c
                    <span style="color: #339933; font-weight: bold;">|</span> b <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="color: green;">&#91;</span>1<span style="color: #339933; font-weight: bold;">..</span>400<span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span>
                      a <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="color: green;">&#91;</span>b<span style="color: #339933; font-weight: bold;">..</span>400<span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span>
                      <span style="color: #06c; font-weight: bold;">let</span> c <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">1000</span> <span style="color: #339933; font-weight: bold;">-</span> a <span style="color: #339933; font-weight: bold;">-</span> b<span style="color: #339933; font-weight: bold;">,</span>
                      c<span style="color: #339933; font-weight: bold;">^</span><span style="color: red;">2</span> <span style="color: #339933; font-weight: bold;">==</span> a<span style="color: #339933; font-weight: bold;">^</span><span style="color: red;">2</span> <span style="color: #339933; font-weight: bold;">+</span> b<span style="color: #339933; font-weight: bold;">^</span><span style="color: red;">2</span> <span style="color: green;">&#93;</span></pre></div></div>

<p>It's a mini brute-forcer which uses two variables to count up to 400. I chose 400 as the end number because it seemed like a stupid idea to go as far as 500 when brute forcing since the first of the numbers that fit the criteria wouldn't be anywhere near that high. For each combination of numbers I pick what's left over and check to see if it fits the rules of a Pythagorean triplet. In this case there is only one that does (as per the question).</p>
<p>We can see below that the solution isn't particularly fantastic as far as performance goes. It's not terrible, but approaching .2 seconds for a problem like this is starting to get on the sluggish side.</p>
<pre>	Fri Jul 25 21:28 2008 Time and Allocation Profiling Report  (Final)

	   main +RTS -p -RTS

	total time  =        0.18 secs   (9 ticks @ 20 ms)
	total alloc =  29,337,532 bytes  (excludes profiling overheads)

COST CENTRE   MODULE  %time %alloc

CAF           Main    100.0  100.0

                                           individual    inherited
COST CENTRE   MODULE      no.    entries  %time %alloc   %time %alloc

MAIN          MAIN          1           0   0.0    0.0   100.0  100.0
 CAF          Main        152          16 100.0  100.0   100.0  100.0
 CAF          GHC.Handle   88           4   0.0    0.0     0.0    0.0</pre>
<p>Another way of brute-force solving this is to abuse a bit of math. We know that each Pythagorean triplet is representative of the sides of a triangle. Given that the sides of a triangle scale evenly when the entire triangle is scaled, we can assume that if we were able to find a triplet whose sum is a factor of 1000, then we could use this factor to generate a, b and c. It turns out that the 5th Pythagorean triplet (8, 15, 17) adds up to 40, and since 40 * 25 == 1000, we can simply multiple each of the values by 25 to get (200, 375, 425).</p>
<p>While this is interesting, brute-force methods aren't never really the nicest way of solving a given problem. In the case of Project Euler it's rare to find a problem that doesn't have some form of trickery that can be abused to get the answer in a much more efficient manner. The theory that should be used is that each Pythagorean triplet can be <a href="http://en.wikipedia.org/wiki/Pythagorean_triple#Generating_a_triple">written in the following form</a>:
<pre>k [ m^2 - n^2, 2mn, m^2 + n^2 ]</pre>
<p>This info can be abused to come up with some code to generate the answer rather quickly. At this point, I become tired of the problem and decided not to implement a version that uses this theory. If you decide to write one, please ping me!</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2008/07/25/project-euler-9/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Another Quick Coding Challenge</title>
		<link>http://buffered.io/2008/07/25/another-quick-coding-challenge/</link>
		<comments>http://buffered.io/2008/07/25/another-quick-coding-challenge/#comments</comments>
		<pubDate>Thu, 24 Jul 2008 18:46:35 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[Challenges]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[challenge]]></category>
		<category><![CDATA[functional]]></category>

		<guid isPermaLink="false">http://buffered.io/?p=406</guid>
		<description><![CDATA[I was about to head to bed when I stumbled across another interesting coding challenge. Since I had another half hour or so to kill I thought I'd give it a shot! The challenge was first posted here. The problem is as follows: Write a counter function that counts from 1 to max but only [...]]]></description>
			<content:encoded><![CDATA[<p>I was about to head to bed when I stumbled across another interesting coding challenge. Since I had another half hour or so to kill I thought I'd give it a shot! </p>
<p>The challenge was first posted <a href="http://beust.com/weblog/archives/000491.html">here</a>. The problem is as follows:</p>
<blockquote><p>Write a counter function that counts from 1 to max but only returns numbers whose digits don't repeat.</p>
<p>For example, part of the output would be:</p>
<p>    * 8, 9, 10, 12 (11 is not valid)<br />
    * 98, 102, 103 (99, 100 and 101 are not valid)<br />
    * 5432, 5436, 5437 (5433, 5434 and 5435 are not valid)</p>
<p>Also:</p>
<p>    * Display the biggest jump (in the sequences above, it's 4: 98 -&gt; 102)<br />
    * Display the total count of numbers<br />
    * Give these two values for max=10000
</p>
</blockquote>
<p>So without further ado, here's my solution:</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>List
&nbsp;
noDups <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">String</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Bool</span>
noDups l <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">length</span> l <span style="color: #339933; font-weight: bold;">==</span> <span style="font-weight: bold;">length</span> <span style="color: green;">&#40;</span>nub l<span style="color: green;">&#41;</span>
&nbsp;
items <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>
items <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">filter</span> <span style="color: green;">&#40;</span>noDups <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">show</span><span style="color: green;">&#41;</span> <span style="color: green;">&#91;</span>1<span style="color: #339933; font-weight: bold;">..</span>10000<span style="color: green;">&#93;</span>
&nbsp;
main <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
main <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">print</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">maximum</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">zipWith</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">-</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">tail</span> items<span style="color: green;">&#41;</span> items<span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="font-weight: bold;">length</span> items<span style="color: green;">&#41;</span></pre></div></div>

<p>It's probably not as concise as it could be, and I'm sure a more experienced Haskeller would be able to tidy it up a little bit. Despite that, it still performs quite well:</p>
<pre>Thu Jul 24 21:34 2008 Time and Allocation Profiling Report  (Final)

	   main +RTS -p -RTS

	total time  =        0.00 secs   (0 ticks @ 20 ms)
	total alloc =   4,121,500 bytes  (excludes profiling overheads)

COST CENTRE   MODULE   %time %alloc

CAF           Main       0.0  100.0

                                            individual    inherited
COST CENTRE   MODULE       no.    entries  %time %alloc   %time %alloc

MAIN          MAIN           1           0   0.0    0.0     0.0  100.0
 CAF          Main         152          14   0.0  100.0     0.0  100.0
 CAF          GHC.Handle    88           4   0.0    0.0     0.0    0.0</pre>
<p>Thoughts? How could I make this better/faster/more elegant? I'm keen to get some insight from a more experienced Haskell coder <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2008/07/25/another-quick-coding-challenge/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Project Euler #8</title>
		<link>http://buffered.io/2008/07/24/project-euler-8/</link>
		<comments>http://buffered.io/2008/07/24/project-euler-8/#comments</comments>
		<pubDate>Thu, 24 Jul 2008 10:55:56 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://buffered.io/?p=399</guid>
		<description><![CDATA[WARNING! This post contains a spoiler for Problem #8 listed at Project Euler. Do not read the rest of this post if you're planning to attempt to solve the problem yourself. Problem #8 was thankfully a great deal more interesting than the last problem. It goes as follows.. Find the greatest product of five consecutive [...]]]></description>
			<content:encoded><![CDATA[<p><strong>WARNING!</strong> This post contains a spoiler for Problem #8 listed at <a href="http://projecteuler.net/" title="Project Euler">Project Euler</a>. Do not read the rest of this post if you're planning to attempt to solve the problem yourself. </p>
<p><a href="http://projecteuler.net/index.php?section=problems&#038;id=8">Problem #8</a> was thankfully a great deal more interesting than <a href="http://buffered.io/2008/06/15/project-euler-7/" title="Project Euler #7">the last problem</a>. It goes as follows..</p>
<blockquote><p>Find the greatest product of five consecutive digits in the 1000-digit number.</p>
<pre lang="">   73167176531330624919225119674426574742355349194934
   96983520312774506326239578318016984801869478851843
   85861560789112949495459501737958331952853208805511
   12540698747158523863050715693290963295227443043557
   66896648950445244523161731856403098711121722383113
   62229893423380308135336276614282806444486645238749
   30358907296290491560440772390713810515859307960866
   70172427121883998797908792274921901699720888093776
   65727333001053367881220235421809751254540594752243
   52584907711670556013604839586446706324415722155397
   53697817977846174064955149290862569321978468622482
   83972241375657056057490261407972968652414535100474
   82166370484403199890008895243450658541227588666881
   16427171479924442928230863465674813919123162824586
   17866458359124566529476545682848912883142607690042
   24219022671055626321111109370544217506941658960408
   07198403850962455444362981230987879927244284909188
   84580156166097919133875499200524063689912560717606
   05886116467109405077541002256983155200055935729725
   71636269561882670428252483600823257530420752963450</pre>
</p>
</blockquote>
<p>I'm keeping with my <a href="http://haskell.org/" title="Haskell">Haskell</a> theme and abusing some of the nice feature of Functional Programming to nail this one.</p>
<p>Here's my solution:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">-- ord is required to convert chars to ints</span>
<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;">Char</span> <span style="color: green;">&#40;</span>ord<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- a string representation of the large number</span>
number <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">String</span>
number <span style="color: #339933; font-weight: bold;">=</span>
     <span style="background-color: #3cb371;">&quot;73167176531330624919225119674426574742355349194934&quot;</span>
  <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;96983520312774506326239578318016984801869478851843&quot;</span>
  <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;85861560789112949495459501737958331952853208805511&quot;</span>
  <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;12540698747158523863050715693290963295227443043557&quot;</span>
  <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;66896648950445244523161731856403098711121722383113&quot;</span>
  <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;62229893423380308135336276614282806444486645238749&quot;</span>
  <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;30358907296290491560440772390713810515859307960866&quot;</span>
  <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;70172427121883998797908792274921901699720888093776&quot;</span>
  <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;65727333001053367881220235421809751254540594752243&quot;</span>
  <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;52584907711670556013604839586446706324415722155397&quot;</span>
  <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;53697817977846174064955149290862569321978468622482&quot;</span>
  <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;83972241375657056057490261407972968652414535100474&quot;</span>
  <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;82166370484403199890008895243450658541227588666881&quot;</span>
  <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;16427171479924442928230863465674813919123162824586&quot;</span>
  <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;17866458359124566529476545682848912883142607690042&quot;</span>
  <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;24219022671055626321111109370544217506941658960408&quot;</span>
  <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;07198403850962455444362981230987879927244284909188&quot;</span>
  <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;84580156166097919133875499200524063689912560717606&quot;</span>
  <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;05886116467109405077541002256983155200055935729725&quot;</span>
  <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;71636269561882670428252483600823257530420752963450&quot;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- converts the number to a list of Int values</span>
numberList <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>
numberList <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">map</span> <span style="color: green;">&#40;</span>\n <span style="color: #339933; font-weight: bold;">-&gt;</span> ord n <span style="color: #339933; font-weight: bold;">-</span> ord '<span style="color: red;">0</span>'<span style="color: green;">&#41;</span> number
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- breaks up the number list into chunks of 5 digits</span>
chunks <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</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: green;">&#93;</span>
chunks <span style="color: #339933; font-weight: bold;">=</span> c' numberList <span style="color: red;">5</span> <span style="color: red;">995</span>
  <span style="color: #06c; font-weight: bold;">where</span>
    c' <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">_</span> <span style="color: red;">0</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
    c' l n c <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">take</span> n l<span style="color: green;">&#41;</span> : c' <span style="color: green;">&#40;</span><span style="font-weight: bold;">tail</span> l<span style="color: green;">&#41;</span> n <span style="color: green;">&#40;</span>c <span style="color: #339933; font-weight: bold;">-</span> <span style="color: red;">1</span><span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- main function iterates over the list of chunks,</span>
<span style="color: #5d478b; font-style: italic;">-- calculates the product of each of the chunks and</span>
<span style="color: #5d478b; font-style: italic;">-- then determines the biggest result</span>
main <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
main <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">print</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">foldr1</span> <span style="font-weight: bold;">max</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">map</span> <span style="font-weight: bold;">product</span> chunks<span style="color: green;">&#41;</span></pre></div></div>

<p>The comments are pretty descriptive of what goes on. It turns out that the solution isn't that shabby in the performance stakes either:</p>
<pre lang="">Thu Jul 24 20:46 2008 Time and Allocation Profiling Report  (Final)

	   main +RTS -p -RTS

	total time  =        0.00 secs   (0 ticks @ 20 ms)
	total alloc =     571,224 bytes  (excludes profiling overheads)

COST CENTRE   MODULE  %time %alloc

CAF           Main      0.0   99.8

                                           individual    inherited
COST CENTRE   MODULE        no.  entries  %time %alloc   %time %alloc

MAIN          MAIN            1         0   0.0    0.0     0.0  100.0
 CAF          Main          152        15   0.0   99.8     0.0   99.8
 CAF          GHC.Handle     88         4   0.0    0.2     0.0    0.2</pre>
<p>I still love how concise and easy the solutions to problems like this are in Haskell.</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2008/07/24/project-euler-8/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Project Euler #7</title>
		<link>http://buffered.io/2008/06/15/project-euler-7/</link>
		<comments>http://buffered.io/2008/06/15/project-euler-7/#comments</comments>
		<pubDate>Sat, 14 Jun 2008 18:53:01 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://buffered.io/?p=366</guid>
		<description><![CDATA[WARNING! This post contains a spoiler for Problem #7 listed at Project Euler. Do not read the rest of this post if you're planning to attempt to solve the problem yourself. Problem #7 was as boring as batshit (to be quite frank). The only reason I'm posting about it is to keep The Head happy [...]]]></description>
			<content:encoded><![CDATA[<p><strong>WARNING!</strong> This post contains a spoiler for Problem #7 listed at <a href="http://projecteuler.net/" title="Project Euler">Project Euler</a>. Do not read the rest of this post if you're planning to attempt to solve the problem yourself. </p>
<p><a href="http://projecteuler.net/index.php?section=problems&#038;id=7">Problem #7</a> was as boring as batshit (to be quite frank). The only reason I'm posting about it is to keep <a href="http://berkshirehunt.com/" title="Berkshire Hunt">The Head</a> happy <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_wink.png' alt=';)' class='wp-smiley' /> </p>
<p>The reason I thought it was boring was because it was just the age-old problem of "how fast can you generate primes":<br />
<blockquote>
<p>
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.</p>
<p>What is the 10001st prime number?</p>
</blockquote>
<p>A simple search of the web reveals a million different ways of solving this problem -- mostly written by people smarter than me <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> </p>
<p>So did I cheat? No I didn't. I wrote the standard <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes" title="Sieve of Eratosthenes">Sieve of Eratosthenes</a> implementation in Haskell and got the answer in a respectable amount of time. Exciting stuff :|</p>
<p>I browsed around the web a little bit after solving the problem because I was interested to see how others might have solved it using Haskell. Yes, I am aware that there is a <a href="" title="">whole page dedicated to it</a> but that wasn't enough.</p>
<p>After a bit of a search, I stumbled across this little doozy. If you can make heads or tails of it you're a better man than I <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_wink.png' alt=';)' class='wp-smiley' /> It's quite a mind job to get your head around how this code works. This is an exercise I leave to the reader.</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">merge <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: green;">&#41;</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>a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span>
merge xs<span style="color: #339933; font-weight: bold;">@</span><span style="color: green;">&#40;</span>x:xt<span style="color: green;">&#41;</span> ys<span style="color: #339933; font-weight: bold;">@</span><span style="color: green;">&#40;</span>y:yt<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span>
  <span style="color: #06c; font-weight: bold;">case</span> <span style="font-weight: bold;">compare</span> x y <span style="color: #06c; font-weight: bold;">of</span>
    LT <span style="color: #339933; font-weight: bold;">-&gt;</span> x : <span style="color: green;">&#40;</span>merge xt ys<span style="color: green;">&#41;</span>
    EQ <span style="color: #339933; font-weight: bold;">-&gt;</span> x : <span style="color: green;">&#40;</span>merge xt yt<span style="color: green;">&#41;</span>
    GT <span style="color: #339933; font-weight: bold;">-&gt;</span> y : <span style="color: green;">&#40;</span>merge xs yt<span style="color: green;">&#41;</span>
&nbsp;
diff <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: green;">&#41;</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>a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span>
diff xs<span style="color: #339933; font-weight: bold;">@</span><span style="color: green;">&#40;</span>x:xt<span style="color: green;">&#41;</span> ys<span style="color: #339933; font-weight: bold;">@</span><span style="color: green;">&#40;</span>y:yt<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span>
  <span style="color: #06c; font-weight: bold;">case</span> <span style="font-weight: bold;">compare</span> x y <span style="color: #06c; font-weight: bold;">of</span>
    LT <span style="color: #339933; font-weight: bold;">-&gt;</span> x : <span style="color: green;">&#40;</span>diff xt ys<span style="color: green;">&#41;</span>
    EQ <span style="color: #339933; font-weight: bold;">-&gt;</span> diff xt yt
    GT <span style="color: #339933; font-weight: bold;">-&gt;</span> diff xs yt
&nbsp;
primes<span style="color: #339933; font-weight: bold;">,</span> nonprimes <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>
primes    <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: #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;">&#40;</span>diff <span style="color: green;">&#91;</span><span style="color: red;">7</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">9</span> <span style="color: #339933; font-weight: bold;">..</span><span style="color: green;">&#93;</span> nonprimes<span style="color: green;">&#41;</span>
nonprimes <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">foldr1</span> f <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">map</span> g <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">tail</span> primes
  <span style="color: #06c; font-weight: bold;">where</span>
    f <span style="color: green;">&#40;</span>x:xt<span style="color: green;">&#41;</span> ys <span style="color: #339933; font-weight: bold;">=</span> x : <span style="color: green;">&#40;</span>merge xt ys<span style="color: green;">&#41;</span>
    g p         <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span> n <span style="color: #339933; font-weight: bold;">*</span> p <span style="color: #339933; font-weight: bold;">|</span> n <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="color: green;">&#91;</span>p<span style="color: #339933; font-weight: bold;">,</span> p <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: green;">&#93;</span><span style="color: green;">&#93;</span>
&nbsp;
main <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
main <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">print</span> <span style="color: #339933; font-weight: bold;">$</span> primes <span style="color: #339933; font-weight: bold;">!!</span> <span style="color: red;">10000</span></pre></div></div>

<p>Enjoy <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> </p>
<p>PS. Jon - are ya happy now? <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_grin.png' alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2008/06/15/project-euler-7/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
