<?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; Functional Programming</title>
	<atom:link href="http://buffered.io/category/functional-programming/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>BFPG June Meetup</title>
		<link>http://buffered.io/2010/06/25/bfpg-june-meetup/</link>
		<comments>http://buffered.io/2010/06/25/bfpg-june-meetup/#comments</comments>
		<pubDate>Fri, 25 Jun 2010 10:05:40 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[bfpg]]></category>
		<category><![CDATA[meetup]]></category>

		<guid isPermaLink="false">http://buffered.io/?p=835</guid>
		<description><![CDATA[The time has come for another meeting of the Functional Programming minds! This months BFPG Meetup, starting 6pm Monday 28th, is the first one at our new venue: Microsoft HQ, Waterfront Place, Brisbane. For details on the location, take a look at the BFPG about page. This month we have three speakers, and I am [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://buffered.io/wp-content/uploads/2010/06/Home.png" alt="Erlang Logo" style="float:right; margin-left:5px; margin-bottom: 5px;"/>The time has come for another meeting of the Functional Programming minds! This months <a href="http://www.meetup.com/Brisbane-Functional-Programming-Group-BFG/calendar/12636552/" title="June BFPG Meetup">BFPG Meetup</a>, starting 6pm Monday 28th, is the first one at our new venue: Microsoft HQ, Waterfront Place, Brisbane. For details on the location, take a look at the <a href="http://bfpg.org/about/" title="About BFPG">BFPG about page</a>.</p>
<p><img src="http://buffered.io/wp-content/uploads/2010/06/ruby.png" alt="Erlang Logo" style="float:left; margin-right:5px; margin-bottom: 5px;"/>This month we have three speakers, and I am one of them! In my talk, "Erlang - Why should you care?", I will be giving a fairly non-technical introduction to <a href="http://erlang.org/" title="Erlang">Erlang</a>. I'll be covering off the features of the language, some of its design goals and its history. I aiming to show that this language is not one that was spawned out of academia and that it has been applied in the real-world ever since its conception. If I have time, I'll also be giving a small demonstration of one of the neatest features of the technology.</p>
<p><img src="http://buffered.io/wp-content/uploads/2010/06/haskell-logo-variation.png" alt="Erlang Logo" style="float:right; margin-left:5px; margin-bottom: 5px;"/><br />
I'll be posting the slides and demonstration code here on this blog as well as <a href="http://bfpg.org/" title="BFPG">BFPG's home</a> after the meetup has taken place.</p>
<p>Myself and my comrades, <a href="http://adams.id.au/blog/" title="Nosewheelie">Tom</a> and <a href="http://blog.tmorris.net/" title="Tony's blog">Tony</a>, will aim to provide a fun and interesting evening at our new venue. Based on the RSVPs so far, it looks like we are going to have one of the biggest turn-outs to date. This is probably due to the exposure we got during last month's <a href="http://buffered.io/2010/05/22/dave-thomas-at-yow-nights-brisbane/" title="Dave Thomas at YOW! Nights Brisbane">talk by Dave Thomas</a>. There are still plenty of spots left, so if you're keen to come along, <a href="http://www.meetup.com/Brisbane-Functional-Programming-Group-BFG/calendar/12636552/" title="Sign up for BFPG June 10">go and sign up</a>. Bring your friends, your friend's friends and your mum.</p>
<p>I hope to see you there!</p>
<hr/>
<p><strong>Update (6th July 2010):</strong> We had a fantastic turn-out of just short of 50 people (a record breaker for the group so far!). My talk seemed to be fairly well-received. No rotten tomatoes were thrown and the crowd seemed engaged through most of the talk. It felt good! I think many people wanted to see more code, but since that wasn't the focus of the talk they were a little disappointed. If people are keen for more Erlang goodness at future meetups, then I'll be happy to dive a little deeper.</p>
<p>As promised, here are the slides:</p>
<ol>
<li><a href="https://docs.google.com/leaf?id=0B4v3XvOkiwsCZjNhZTk3OTYtZTgxNy00MjBmLWJiZmYtMzZjMzU3N2E3MDgz&#038;hl=en&#038;authkey=CIaRg_sF">PPTX</a></li>
<li><a href="https://docs.google.com/a/buffered.io/fileview?id=0B4v3XvOkiwsCMjlhMWRjNGItYmZkOC00ODRlLTkzYTUtOTI3ZWZhY2MxOGYx&#038;hl=en&#038;authkey=CM7rtpEO">PDF (Google Docs)</a></li>
<li><a href="https://docs.google.com/a/buffered.io/present/edit?id=0AYv3XvOkiwsCZGdmamd0eGNfN2dnaDh3emhw&#038;hl=en&#038;authkey=CJKBg-MB">PPT (Google Docs)</a></li>
</ol>
<p>If you have issues reading the slide deck, or would like it in another format, then please <a href="http://buffered.io/contact-me">drop me a line</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2010/06/25/bfpg-june-meetup/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Dave Thomas at YOW! Nights Brisbane</title>
		<link>http://buffered.io/2010/05/22/dave-thomas-at-yow-nights-brisbane/</link>
		<comments>http://buffered.io/2010/05/22/dave-thomas-at-yow-nights-brisbane/#comments</comments>
		<pubDate>Sat, 22 May 2010 13:30:20 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[Being in the Industry]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[NoSQL]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://buffered.io/?p=830</guid>
		<description><![CDATA[Two nights ago I was fortunate enough to attend a YOW! Nights conference held at the local Microsoft Office here in Brisbane. The speaker for the session was none other than Dave Thomas, the man behind the likes of ObjectMentor. Not only is this guy incredibly switched on, he is thought-provoking and very entertaining to [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://buffered.io/wp-content/uploads/2010/05/haskell-logo-variation.png" alt="Haskell Logo" title="Haskell Logo" width="128" height="128" style="float:right;margin-left:5px;margin-bottom:5px" />Two nights ago I was fortunate enough to attend a <a href="http://yownightbrisbanemay.eventbrite.com/" title="YOW! Nights Brisbane - May 20th">YOW! Nights</a> conference held at the local Microsoft Office here in Brisbane. The speaker for the session was none other than <a href="http://www.davethomas.net/" title="Dave Thomas">Dave Thomas</a>, the man behind the likes of <a href="http://objectmentor.com/" title="Object Mentor">ObjectMentor</a>. Not only is this guy incredibly switched on, he is thought-provoking and very entertaining to boot. I've been to a few of these sessions in the past but I've never seen one as packed out as this one. It was great to see such a turn-out, which no doubt was boosted by the presence of both the <a href="http://www.qmsdnug.org/" title="QMSDNUG">Queensland MSDN Users Group</a> and the <a href="http://bfpg.org/" title="BFPG">Brisbane Functional Programming Group</a>. </p>
<p>The talk was covering <a href="http://en.wikipedia.org/wiki/Functional_programming" title="Functional Programming">Functional Programming</a> and the <a href="http://en.wikipedia.org/wiki/NoSQL" title="NoSQL">NoSQL</a> movement (which, as Dave himself puts it, should be called <em>NoRDB</em>). These are two areas of computing that are currently key interests of mine, so it won't come as a surprise to find that I thoroughly enjoyed the talk. I certainly wasn't the only one. Dave did such a great job of covering the positives of these two movements that we're already seeing more and more people signing up for membership to <a href="http://bfpg.org/" title="BFPG">BFPG</a>.</p>
<p>I'm not going to go into the gory details about what Dave covered in his talk as I don't want to spoil it for other people if he plans on doing it again elsewhere. There are a few things I would like to mention though:</p>
<ul>
<li>Functional Programming is here to stay. If you're not familiar with it, or haven't yet taken a look at it, I suggest you get started!</li>
<li>If you're looking to get involved with FP and you're in or around the Brisbane area, be sure to come to one of our <a href="http://bfpg.org/" title="BFPG">BFPG</a> meetups.</li>
<li>Don't fall into the trap of assuming that if your system needs a back-end data store that an <a href="http://en.wikipedia.org/wiki/Relational_database_management_system" title="Relational Database Management System">RDBMS</a> is the only option, because it's not!</li>
</ul>
<p>This talk has inspired me to not only continue checking out the likes of <a href="http://haskell.org/" title="Haskell">Haskell</a>, <a href="http://erlang.org/" title="Erlang">Erlang</a> and <a href="http://riak.basho.com/" title="Riak">Riak</a>, it's pointed me at some languages that I never knew existed. Languages which are insanely powerful, very dense, yet still extremely useful. Some examples are <a href="http://en.wikipedia.org/wiki/J_(programming_language)" title="J">J</a>, <a href="http://en.wikipedia.org/wiki/K_(programming_language)" title="K">K</a> and <a href="http://en.wikipedia.org/wiki/Q_(programming_language_from_Kx_Systems)" title="Q">Q</a>. <a href="http://www.kx.com/products/database.php" title="KDB+">KDB+</a> in particular is very interesting as it ties in with the use of another type of technology that I was aware of but knew nothing about: <a href="http://en.wikipedia.org/wiki/Column-oriented_DBMS" title="Column Database">Column Databases</a>. Crazy stuff.</p>
<p>After seeing so many people excited by the prospect of Functional Programming after Dave's talk had concluded, I decided to set up a new website for <a href="http://bfpg.org/" title="BFPG">BFPG</a> so that we had a proper home rather than just a spot on <a href="www.meetup.com/Brisbane-Functional-Programming-Group-BFG/" title="BFPG @ Meetup.com">Meetup.com</a>. Check it out and feel free to send through some feedback if you have any, or <a href="http://bfpg.org/contact/" title="Contact BFPG">drop us a line</a> if you have any ideas for talks or would like to present something yourself.</p>
<p>That's it for today's brain-fart <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/2010/05/22/dave-thomas-at-yow-nights-brisbane/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Katacast: Shopping Checkout in Erlang</title>
		<link>http://buffered.io/2010/01/04/katacast-shopping-checkout-in-erlang/</link>
		<comments>http://buffered.io/2010/01/04/katacast-shopping-checkout-in-erlang/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 07:47:05 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[Erlang]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[codekata]]></category>
		<category><![CDATA[katacast]]></category>
		<category><![CDATA[rake]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://buffered.io/?p=785</guid>
		<description><![CDATA[I've recently discovered the joys of CodeKatas. If you're a geek looking for a way to sharpen your saw then I highly recommend taking a look at these and trying a few out. They're great fun and they're a good way of getting your feet wet with new languages (especially if you're tired of Project [...]]]></description>
			<content:encoded><![CDATA[<p>I've recently discovered the joys of <a href="http://codekata.pragprog.com/" title="CodeKata">CodeKatas</a>. If you're a geek looking for a way to <a href="http://en.wikipedia.org/wiki/The_Seven_Habits_of_Highly_Effective_People" title="The Seven Habits of Highly Effective People">sharpen your saw</a> then I highly recommend taking a look at these and trying a few out. They're great fun and they're a good way of getting your feet wet with new languages (especially if you're tired of <a href="http://projecteuler.net/" title="Project Euler">Project Euler</a>). </p>
<p>I can thank <a href="http://twitter.com/gabriellelana" title="Gabrielle on Twitter">Gabrielle Lana</a> for this discovery, as it was his <a href="http://www.vimeo.com/8206748" title="String Calculator in Erlang">attempt at one of the katas</a> that got me into it. He's also the reason I looked into <a href="http://www.katacasts.com/" title="Katacasts">Katacasts</a>, which are simply screencasted recordings of people attempting to solve Code Katas.</p>
<p>This post is about my first Katacast, capturing my attempt at <a href="http://codekata.pragprog.com/2007/01/kata_nine_back_.html" title="Checkout">Code Kata #9</a> using <a href="http://erlang.org/" title="Erlang">Erlang</a> and <a href="http://vim.org/" title="VIM">VIM</a> with <a href="http://rake.rubyforge.org/" title="Ruby Make">Rake</a> as my build system. I've put the video up on <a href="http://vimeo.com/8513414" title="Katacast for Codekata #9 by OJ Reeves">Vimeo</a> for the world to see/share. It's also embedded below for your convenience.</p>
<p>Before watching, I'd like to point out a few things about the video, and things that I have learned from the experience:</p>
<ul>
<li>I captured the video on my Linux Mint install. For some reason, capturing quality video in Linux is proving to be a bit of a pain for me. As a result, the video quality isn't that great. This partly due to the compression of the video, and partly due to the software failing to pick up some screen updates.</li>
<li>I tried really hard to add a backing sound-track but all of my efforts failed in one way or another. If I did manage to get one to work it was either WAY too big (ie. > 1GB in size) or the quality was absolutely terrible.</li>
<li>There were points during the process where I had to alt-tab out to a browser to do a bit of research on something that I didn't know (such as an Erlang syntax quirk). This resulted in a few points where I don't appear to be doing anything. In future, I'll remember to hit pause on the recording when this happens.</li>
<li>I captured it in a non-standard resolution. I'm not sure if that's going to make it look bad or not. Feedback here is appreciated. I think in future I'll capture at a 800x600 resolution.</li>
<li>This video is best viewed in full-screen mode.</li>
</ul>
<p>So please excuse the lack of quality in this first Katacast. I'll be sure to improve when I next create one. That being said, on with the show!</p>
<div style="margin-left: auto; margin-right: auto; width: 400px;"><object width="400" height="300"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=8513414&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=8513414&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="300"></embed></object><br />
<a href="http://vimeo.com/8513414">Katacast for Codekata #9</a> from <a href="http://vimeo.com/thecolonial">OJ Reeves</a> on <a href="http://vimeo.com">Vimeo</a>.</div>
<p>As always, feedback/criticism/etc is greatly appreciated. I'd love to hear what you guys do and use to capture and edit your screencasts, particularly in Linux.</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2010/01/04/katacast-shopping-checkout-in-erlang/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Future is Erlang</title>
		<link>http://buffered.io/2009/10/31/the-future-is-erlang/</link>
		<comments>http://buffered.io/2009/10/31/the-future-is-erlang/#comments</comments>
		<pubDate>Sat, 31 Oct 2009 10:28:10 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Erlang]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Mercurial]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Webmachine]]></category>

		<guid isPermaLink="false">http://buffered.io/?p=769</guid>
		<description><![CDATA[For quite a while I've been using my spare cycles to chew over a problem. This problem is not one that hasn't been solved before, but one that I feel can be solved in a much better way. From the bit of research that I've done, I can see at least three areas which can [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://buffered.io/wp-content/uploads/2009/10/erlang.png" alt="erlang" title="erlang" style="float:left;margin-right:5px;margin-bottom:5px;" />For quite a while I've been using my spare cycles to chew over a problem. This problem is not one that hasn't been solved before, but one that I feel can be solved in a much better way. From the bit of research that I've done, I can see at least three areas which can be improved on dramatically. So I've decided, after a long period of deliberation, to go ahead and build my solution and release it to the world as a service!</p>
<p>I know, this kind of thing happens thousands of times a day. Some arbitrary geek decided to build the next killer app and expects to make a crapload of cash out of it. While that would be nice, the ultimate goal isn't the cash. The goal for me is to build something that gets used, and for me to begin to master an area of technology that I've had a quiet passion for for a long period of time now.</p>
<p>My plan is simple. While learning the technologies that I will be using I'll be building another application and blogging about it so that all of you can learn with me. Meanwhile, I shall use my learnings to build this other application at the same time.</p>
<p>The final result should be a series of posts which help other people learn the technology stack, give better coverage and visibility to languages and technologies that a lot of the geek world doesn't know about (and really should!), and for me to carve myself a bit of a niche here on the East coast of Ozland. I am hoping to be able to consult my services out to other businesses as a result.</p>
<p>For those that are interested in following along, here is the full tech stack and toolset that I'll be using:</p>
<ul>
<li><a href="http://erlang.org/" title="Erlang">Erlang</a> - A fantastic cross-platform functional programming language that has some amazing features.</li>
<li><a href="http://bitbucket.org/justin/webmachine/" title="Webmachine HTTP toolkit">Webmachine</a> - A HTTP toolkit which sits on top of <a href="http://code.google.com/p/mochiweb/" title="Mochiweb">Mochiweb</a> that makes it easy to build well-behaved HTTP applications.</li>
<li><a href="http://nitrogenproject.com/" title="Nitrogen Framework">Nitrogen</a> - An Erlang-based framework that makes it easy to build websites.</li>
<li><a href="http://riak.basho.com/" title="Riak">Riak</a> or <a href="http://couchdb.apache.org/" title="CouchDB">CouchDB</a> - Both of these amazing pieces of tech are potential candidates for the back-end storage. I'm not yet sure which one I'll go with.</li>
<li><a href="http://jquery.com/" title="jQuery">jQuery</a> - A great JavaScript library for the front-end.</li>
<li><a href="http://vim.org/" title="VIM text editor">VIM</a> - My favourite text editor <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> </li>
<li><a href="http://www.xubuntu.org/" title="Xubuntu">Xubuntu</a> - Ubuntu <a href="http://ubuntu.com/" title="Ubuntu 9.10">Karmic Koala</a> with <a href="http://www.xfce.org/" title="XFCE">XFCE</a> as the window manager.</li>
<li><a href="http://mercurial-scm.org/" title="Mercurial SCM">Mercurial</a> - My favourite version control system.</li>
<li><a href="http://trac.edgewall.org/" title="The Trac Project">Trac</a> - A project tracking tool.</li>
<li><a href="http://www.virtualbox.org/" title="VirtualBox">VirtualBox</a> - My choice for virtualisation. I tend to run Windows 7 as my primary OS, so most of my dev will be in a VM. When I get my desktop machine back up and running (with all three monitors!) it'll most likely multiboot Win 7 and Xubuntu.
<li><a href="http://wave.google.com/" title="Google Wave">Google Wave</a> - I'll be using this for communications with some really switched on people in the Erlang community who have kindly offered to help me with questions and whatnot specific to some of the areas of tech I've mentioned above.</li>
</ul>
<p>During the course of my blogging/learning I'll be focussing on Webmachine, Riak/CouchDB, Erlang and Nitrogen. All the other tools will probably get mentioned along the way, but the primary goal is to focus on these things.</p>
<p>I'm really excited! I'm currently in the process of documenting my goals and the design for the main application. Once that's done, I'll get started with the fun stuff. I've got a lot to do and it should be a fun ride. I hope you enjoy following along!</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2009/10/31/the-future-is-erlang/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<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>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>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>
	</channel>
</rss>
